You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Program execute display like this error: ImportError: No module named 'xxx' also you used like this code: from .xxx import xxx or from ..xxx import xxx.
Example:
module/
__init__.py
A/
__init__.py
a.py
deffunc():
print("A.a")
B/
__init__.py
b.py
from ..A.aimportfuncfunc()
main.py
frommodule.Bimport*
This code will display error when running python3 b.py. I think maybe Python3 Interpreter not support this grammar.
And code will display error when running python3 main.py. if you use sys.paths.append("../") or sys.paths.append("./") like this code, still a mistake. Because program when load module abspath is main.py file, not b.py file. And C++ /C language is not such.
Solve Method
__file__ return current file path
os.path.abspath(path) return file abspath
os.path.dirname() return folder abspath
os.path.join() join path
load one level catalog path: os.path.abspath(os.path.join(os.path.dirname(__file__), "../")), load two level catalog path: os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")), etc.
The text was updated successfully, but these errors were encountered:
Python3.x load module path error
Program execute display like this error:
ImportError: No module named 'xxx'
also you used like this code:from .xxx import xxx
orfrom ..xxx import xxx
.Example:
module/
__init__.py
A/
__init__.py
a.py
B/
__init__.py
b.py
main.py
This code will display error when running
python3 b.py
. I think maybe Python3 Interpreter not support this grammar.And code will display error when running
python3 main.py
. if you usesys.paths.append("../")
orsys.paths.append("./")
like this code, still a mistake. Because program when load module abspath is main.py file, not b.py file. AndC++ /C
language is not such.Solve Method
__file__
return current file pathos.path.abspath(path)
return file abspathos.path.dirname()
return folder abspathos.path.join()
join pathload one level catalog path:
os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
, load two level catalog path:os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))
, etc.The text was updated successfully, but these errors were encountered: