Skip to content Skip to sidebar Skip to footer

Python Packages: Relative Imports

I'm working on a Python application consisting of a core and multiple independent modules using the core. I'm having difficulty setting up relative imports of packages. app |- __

Solution 1:

In short, you can only use relative imports from packages that are, themselves, imported.

For example, if you had:

$ cat run.py
from app.module1 import main
main.main()
$ python run.py

Then you could use a relative import in app/module1/main.py (although it would need to be from ..core import foo, because core/ is one level above main.py).

Solution 2:

import sys
abs_filepath = '/home/n/Documents/IMPORTANT/deep_learning/drori_2018/    final_proj/Ryans_branch/StackGAN/'
# insert your absolute filepath above as abs_filepath = '/path/to/targ/dir'
sys.path.append(abs_filepath)

Please correct it if there are problems with doing the import this way

Other Answers:

Also please see here for a thorough answer about what's going on.

Post a Comment for "Python Packages: Relative Imports"