QBoard » Advanced Visualizations » Viz - Python » Python Module not found error when using as Package

Python Module not found error when using as Package

  • I have two python files namely abc.py and xyz.py

    abc.py contains two function viz func_1() and func_2()

    My directory structure is as follows
    db_ops /
       __init__.py
      abc.py
      xyz.py​


    Now in xyz.py I have the following import method

    from db_ops.abc import func_1
    

    When I run xyz.py I am getting error as

    ModuleNotFoundError: No module named 'db_ops'
    


    Am I missing out anything?

    P.S. I am using Windows 10


      July 31, 2021 4:31 PM IST
    0
  • For me when I created a file and saved it as python file, I was getting this error during importing. I had to create a filename with the type ".py" , like filename.py and then save it as a python file. post trying to import the file worked for me.
      August 12, 2021 1:49 PM IST
    0
  • You don't need a package for this, since all your code is in the same directory.
    Simply use from abc import func_1 since xyz and abc are in the same directory
      August 12, 2021 3:40 PM IST
    0
  • Ok so I found the answer for the problem I was facing. We need to add the dir path where abc.py and xyz.py are located. Assuming we have the broad dir structure as follows

    etl_works/
    db_ops 
    __init__.py/
     __init__.py
     abc.py
     xyz.py​


    And etl_works directory is as follows

    'D:/my_py/etl_works'
    ​


    Then we need to add the above to the sys.path i.e.

    import sys
    sys.path.append('D:/my_py/etl_works')​


    And then we can use import db_ops or from db_ops import abc Caveat: The above method is not a permanent solution. Every time we close the program or kernel, the path.append method gets reversed. To circumvent this, we simply add the above dir path to PYTHONPATH (on in Windows 10 system, we add the path to Environment and system properties) Hope this helps to any newbies like me!!
      November 20, 2021 12:21 PM IST
    0