Python can't import my package
Question: I have the following directory structure:

    
    
    myapp
    ├── a
    │   ├── amodule.py
    │   └── __init__.py
    ├── b
    │   ├── bmodule.py
    │   ├── __init__.py
    └── __init__.py
    

In `a/amodule.py` I have this snippet which calls a simple function in
`b/bmodule.py`

    
    
    from  myapp.b import bmodule
    
    b.myfunc()
    

But when i run `python a/amodule.py` I get this error:

    
    
     File "a/amodule.py", line 1, in <module>
        from  myapp.b import bmodule
    ImportError: No module named 'myapp'
    

What am I doing wrong?


Answer: you need to put your project root onto your python path

you can set the PYTHONPATH environmental variable

or you can alter `sys.path` before importing

or you can use an IDE like pycharm that will do this kind of thing for you

(although it will probably be `from b import blah`)

there is likely other ways to resolve this issue as well

watch out for circular imports ...

(in python 3 you can also do relative imports... although I am not a big fan
of this feature)

    
    
    from ..b import blah
    

the best way to allow

    
    
    from myapp.b import whatever
    

would be to edit your `.bashrc` file to always add your parent path to the
PYTHONPATH

    
    
    export PYTHONPATH=$PYTHONPATH;/home/lee/Code
    

now every time you log into the system python will treat your `Code` folder as
a default place to look for import modules, regardless of where the file is
executed from

