When you start python from a directory, that directory is added to your PYTHONPATH
so modules are importable from that directory and below, provided you've got an __init__.py
in each directory, including the top level that you're running python from. See here:
~/Development/imports $ tree
.
├── __init__.py
├── mod1
│ ├── __init__.py
│ ├── a.py
├── mod2
│ ├── __init__.py
│ ├── b.py
├── top.py
So when we start python from ~/Development/imports/
, we can access top
mod1.a
and mod2.b
:
~/Development/imports $ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import top
>>> import mod1.a
>>> import mod2.b
>>> import sys
But when we start python from inside mod1
, we're not allowed to go outside of mod1
back to top
or to mod2
:
~/Development/imports/mod1 $ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> from .. import top
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
>>> from ..mod2 import b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
Relative imports from ..mod2 import b
only work from a module below the top level module that you started from, because they are all implicitly in the python path.
You can't escape outside of the module you start from unless that particular path is added to PYTHONPATH
or sys.path
:
~/Development/imports/mod1 $ PYTHONPATH=../ python
Python 2.7.8 (default, Nov 3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import top
>>> import top.mod2.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mod2.b
>>> import sys
>>> sys.path.append('~/Development/imports/mod2/')
>>> from mod2 import b
>>>
So, you need to ensure that all of your directories have an __init__.py
file in them. You also need to ensure that you're starting python from the right location, usually the top directory. You can't start python half way down the directory structure and expect to get back up to the top, or sideways to another directory/module.