Python 项目出现 ModuleNotFoundError: No module named ‘teste’。

huangapple go评论60阅读模式
英文:

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"...

huangapple
  • 本文由 发表于 2023年5月15日 04:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定