简体   繁体   中英

python import relative path 'No module found error'

I am in folder2 trying to execute trigger.py

I used:

from ..folder1.subfolder1.function1 import *
from ..folder1.test.py

got error attempted relative import with no known parent package

在此处输入图片说明

If you're running out of folder2 , then for all practical purposes, trigger.py is a module on its own, not part of a package hierarchy, and can't use relative imports to folder1 (because they're unrelated).

If you can, make your working directory the parent directory of Project , and invoke your script with:

python3 -m Project.folder2.trigger

which runs a module as the main entry point via import syntax, without losing the information on the package structure.

If the whole project is actually installed to system or user site-packages , your working directory won't matter (it's only needed if it's not in sys.path automatically, because the working directory itself is included in sys.path ), and the invocation will work anywhere.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM