Importing a package from a subdirectory or relative path
Here's my directory setup:
mydir
βββ script1.py
βββ shared
βββ otherstuff
βββ script2.py
βββ pkg
βββ box.py
βββ __init__.py
script2.py
begin with
import pkg
and it works great. When I include the same line in script1.py
I get:
Traceback (most recent call last):
File "script1.py", line 1, in <module>
import pkg
Is there a good way to get a syntax that's easy to work with script1.py
? I've been reading about PYTHONPATH
and sys.path
for the last hour, but I'm trying to make some basic functionality available to my repo and I can't believe it will require a change PYTHONPATH
every time I want to run the script.
What am I missing here? What's the best way to get pkg
in script1.py
?
source to share