Python 错误“ModuleNotFoundError:”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41910139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python error ' ModuleNotFoundError:'
提问by Rohil
I am getting this error
我收到此错误
Traceback (most recent call last):
File "Exporter.py", line 3, in <module>
import sys,getopt,got,datetime,codecs
File "C:\Users\Rohil\Desktop\GetOldTweets-python-master\got\__init__.py", line 1, in <module>
import models
ModuleNotFoundError: No module named 'models'
my directory tree is:
我的目录树是:
C:\Users\Rohil\Desktop\GetOldTweets-python-master\got
this contains 2 folders: manager and models and 1 __init__.py file with the code :
这包含 2 个文件夹:manager 和 models 以及 1 个 __init__.py 文件,代码如下:
import models
import manager
i am executing a file with the path: C:\Users\Rohil\Desktop\GetOldTweets-python-master\Exporter.py
我正在执行一个带有路径的文件: C:\Users\Rohil\Desktop\GetOldTweets-python-master\Exporter.py
I can't figure out what the issue is. can anyone assist me?
我无法弄清楚是什么问题。有人可以帮助我吗?
回答by blue_note
Set the environment variable PYTHONPATH=C:\Users\Rohil\Desktop\GetOldTweets-python-master\got
(how exactly, depends on your operating system)
设置环境变量PYTHONPATH=C:\Users\Rohil\Desktop\GetOldTweets-python-master\got
(具体如何,取决于您的操作系统)
回答by Gaurav Singh
Hi Please follow below step, you will resolve this problem. If you have created directory and sub-directory then follow below steps and please keep in mind all directory must have "init.py" to get it recognized as a directory.
您好,请按照以下步骤操作,您将解决此问题。如果您已创建目录和子目录,请按照以下步骤操作,请记住所有目录都必须具有“init.py”才能将其识别为目录。
"import sys" and run "sys.path" , you will be able to see all path that is being search by python.You must be able to see your current working directory.
Now import sub-directory and respective module that you want to use using import follow this command: "import subdir.subdir.modulename as abc" and now you can use the methods in that module.SameIssue
"import sys" 并运行 "sys.path" ,您将能够看到python正在搜索的所有路径。您必须能够看到您当前的工作目录。
现在使用 import 导入子目录和要使用的相应模块,请遵循以下命令:“import subdir.subdir.modulename as abc”,现在您可以使用该模块中的方法。同一期
as you can see in this screenshot I have one parent directory and two sub-directories and under second sub-directories i have module==CommonFunction and you see right side after execution of sys.path I can see my working directory.
正如您在此屏幕截图中看到的,我有一个父目录和两个子目录,在第二个子目录下我有 module==CommonFunction 并且您在执行 sys.path 后看到右侧我可以看到我的工作目录。
回答by Sanjay Makwana
if create d or using or custom python package
如果创建 d 或使用或自定义 python 包
check our dir. correct.
检查我们的目录。正确的。
** For python 3.7 user **
** 对于 python 3.7 用户 **
from. import module_name
回答by Marlon Teixeira
If you are using python3 but are installing the API through 'pip install 'such and such'. It's not gonna work on python3 console. Therefore, you'd better use 'sudo python3 -m pip install 'such and such''. Then it's gonna work! (at least for ubuntu stuff)
如果您使用的是 python3,但正在通过“pip install 'such and such”安装 API。它不会在 python3 控制台上工作。因此,您最好使用 'sudo python3 -m pip install 'such and such''。然后它会起作用!(至少对于 ubuntu 的东西)
:)
:)
回答by Ujjwal
- Does the
models
folder has an__init__.py
file inside it ? Only then, it will be recognized as a module by python andimport models
would make sense.
- 是否
models
夹有一个__init__.py
里面的文件?只有这样,它才会被python识别为一个模块并且import models
有意义。
So,
所以,
- Create an empty
__init__.py
file in themodels
subfolder and then the code should work without any issues.
__init__.py
在models
子文件夹中创建一个空文件,然后代码应该可以正常工作。
You should also look at this answer.
你也应该看看这个答案。