I’m currently working on a personal project in Python where I have many different packages to organize different pieces of similar code together. I’ve come into a situation where I need to access a piece of code from a different directory, but run into errors when I try running the code from the terminal. (Windows Command Prompt)
Here’s a simpler example of the problem I’m running into:
parentPackage/
__init__.py
pack1/
__init__.py
file1.py
pack2/
__init__.py
file2.py
file3.py
Files 1 and 2 contain functions called foo() and foo2(), respectively, and I need to access both functions inside of file 3.
Using PyCharm, I can run file 3 perfectly using the following imports:
from parentPackage.pack1.file1 import foo
from file2 import foo2
However, the command terminal gives me errors when doing this:
ModuleNotFoundError: No module named 'parentPackage' (line 1)
Using relative imports, I can get something that the compiler accepts but that still fails at runtime:
from ..pack1.file1 import foo
from file2 import foo2
ImportError: attempted relative import with no known parent package (line 1)
Any suggestions on how to access the functions I need from file 3?
AlexM_CS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.