Search code examples
pythonpython-3.xpython-2.7python-importimporterror

Python Import gives packge not found even after __init__


Test
|   
|   __init__.py
|   
\---Mypackage
    |   qqq.py
    |   __init__.py
    |   
    +---effects
    |       abc.py
    |       
    \---Sound
        |   __init__.py
        |   
        \---formats
                xyz.py

When I try to import in xyz.py “from Mypackage.effects import abc”

I get,

C:\Test>python C:\Test\Mypackage\Sound\formats\xyz.py
Traceback (most recent call last):
  File "C:\Test\Mypackage\Sound\formats\xyz.py", line 1, in <module>
    from Mypackage.effects import abc
ModuleNotFoundError: No module named 'Mypackage'

Solution

  • you will need to add the following lines in xyz.py

    import sys
    sys.path.append('path_to_your_Test_folder')
    from Mypackage.effects import abc
    

    and then run this script like the way you showed in your question.

    Note: Above solution is tested using python3.5 and ubuntu 16.04