I'm newish to imports so it's possible my approach is patchy but i'm really struggling to piece together how this is supposed to be done.
So I have a github repo I am working on that currently looks something like this:
Github-repo-name
/src
/repo-name
__init__.py
/packages
__init__.py
mypackage1/
__init__.py
p1_module1.py
mypackage2/
__init__.py
p2_module1.py
main.py
/scripts
do_something.py
/datasets
I'm trying to import mypackage1 into do_something.py in order to generate some data to put in datasets/ but was having lots of trouble when I ran the do_something.py script in the src directory.
I had this line in do_something.py:
import repo-name.packages.mypackage1
and was hitting: ModuleNotFoundError: No module named repo-name
I tried relative imports to no more success
from ..repo-name.packages import mypackage1
ImportError: attempted relative import with no known parent package
I couldn't make head or tail of this issue so I instead tried adding my packages to the path in do_something.py
file_path = pathlib.Path(__file__)
path = file_path.resolve().parents[2]
sys.path.append(str(path))
import repo-name.packages.mypackage1
Success!
Except now i'm wondering if this really what I should be doing and if this isn't a bit hacky. The idea of needing to add the path at runtime seems a bit messy so i'd rather have it all the time. I've read I can use PYTHONPATH for this purpose which would be great if it were just me using this repo, but I intend for this to be shared and reproduceable. I thought about maybe writing a shell script to set the PYTHONPATH but i'm not sure that's much better.
So my question is what is the right/better/a good way of doing what i'm trying to do?
Oliver.
I don't know what is the best way, but I'm trying to show you how our team works.
When developing,
i) register PYTHONPATH
in ~/.bashrc
ii) sys.path.append(path/to/root/repo)
on the top of the code
Our team usually does it as an initial way.
To distinguish whether they receive the authority of repo or not, you can check the below.
# Suppose you created a setup.py(to make `.whl` file) on `repo-name`.
if __name__ == '__main__':
from repo-name.packages.mypackage1 import *
else:
from ..repo-name.packages.mypackage1 import *
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.