Here is my project directory in Intellij
parent/
A/
test.py
In test.py, I have to import a module from parent level of parent folder.
import module_needed
but module_needed is in this structure:
parent_a/
parent_b/
py/
module_needed
application/
parent/
A/
test.py
I tried to add moduled_needed's path to sys.path in test.py. But still cannot find module. Am I doing wrong? I am using Intellij, is this related to Intellij?
Python import system is quite straightforward. It looks for the packages in folders from sys.path
. You have to add folder that contains module_needed
in this case py
(full path), not path to module_needed
itself. If it's not there you can add the folder either from the code, directly appending to sys.path
or with PYTHONPATH
enviroment variable.
Also try printing sys.path
, because Intellij might add project root to python path. Then the import statement would be:
import parent_a.parent_b.py.module_needed
without any modifications.
It might be useful to add the project root, then you won't have to add every single folder separately.
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.