英文:
Python project giving me ModuleNotFoundError: No module named 'teste'
问题
I have two folders each one with a file.py
inside (root project > src/
with main.py
and teste/
with t.py
).
I'm trying to import the t.py
in my main.py
, but I'm having the following error:
> ModuleNotFoundError: No module named 'teste'
The code of my main.py
is:
import teste.t as t_
t_.hi()
The hi()
just prints a "Hi" on the console. I checked if Python is on the path and is; I tried to use the SETPYTHONPATH=
. or the folder path, everything.
I also tried to use the sys.append
and nothing works.
Why?
英文:
I have two folders each one with a file.py
inside (root project > src/
with main.py
and teste/
with t.py
).
I'm trying to import the t.py
in my main.py
, but I'm having the following error:
> ModuleNotFoundError: No module named 'teste'
The code of my main.py
is:
import teste.t as t_
t_.hi()
The hi()
just prints a "Hi" on the console. I checked if Python is on the path and is; I tried to use the SETPYTHONPATH=
. or the folder path, everything.
I also tried to use the sys.append
and nothing works.
Why?
答案1
得分: 1
Your python process wants to import "teste". But it cannot find a file named "teste.py" and also not a directory called "teste" with a file called "init.py" inside.
Whenever you want to import something, make sure that the file to import is reachable by python. The easiest way for python to see such a file is if it is in the same directory as the file that you started using the command "python myfile.py".
There's another error: you wrote "import teste.t as t_" - which is a bit unusual, but okay. BUT in the next line you call "t.hi()" - but "t" isn't anywhere declared. Did you mean "t_.hi()"?
Nevertheless, first make sure that Python finds a directory called "teste"...
英文:
Your python process wants to import "teste
". But it cannot find a file named "teste.py
" and also not a directory called "teste
" with a file called "__init__.py
" inside.
Whenever you want to import something, make sure that the file to import is reachable by python. The easiest way for python to see such a file is if it is in the same directory as the file that you started using the command "python myfile.py
".
There's another error: you wrote "import teste.t as t_
" - which is a bit unusual, but okay. BUT in the next line you call "t.hi()
" - but "t
" isn't anywhere declared. Did you mean "t_.hi()
"?
Nevertheless, first make sure that Python finds a directory called "teste
"...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论