If I have the following directory structure:
parent/
- __init__.py
- file1.py
- child/
- __init__.py
- file2.py
In file 2, how would I import file 1?
Update:
>>> import sys
>>> sys.path.append(sys.path.append('/'.join(os.getcwd().split('/')[:-2])))
>>> import parent
>>> ImportError: No module named parent
Here's something I made to import anything. Of course, you have to still copy this script around to local directories, import it, and
use
the path you want.You need to specify parent and it needs to be on the sys.path
There is a whole section about Modules on the Python Docs:
Python 2: http://docs.python.org/tutorial/modules.html
Python 3: http://docs.python.org/py3k/tutorial/modules.html
In both see section 6.4.2 for specific imports of parent packages (and others too)
You still need to mention the parent, since they're in different namespaces: