Python - 找不到模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37233140/
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 - Module Not Found
提问by user962206
I am a beginner with Python. Before I start, here's my Python folder structure
我是 Python 的初学者。在开始之前,这是我的 Python 文件夹结构
-project
----src
------model
--------order.py
------hello-world.py
Under src
I have a folder named model
which has a Python file called order.py
which contents follow:
在src
我下面有一个名为的文件夹model
,其中包含一个名为以下order.py
内容的 Python 文件:
class SellOrder(object):
def __init__(self,genericName,brandName):
self.genericName = genericName
self.brandName = brandName
Next my hello-world.py
is inside the src
folder, one level above order.py
:
接下来我hello-world.py
在src
文件夹内,上一层order.py
:
import model.order.SellOrder
order = SellOrder("Test","Test")
print order.brandName
Whenever I run python hello-world.py
it results in the error
每当我运行python hello-world.py
它都会导致错误
Traceback (most recent call last):
File "hello-world.py", line 1, in <module>
import model.order.SellOrder
ImportError: No module named model.order.SellOrder
Is there anything I missed?
有什么我错过的吗?
采纳答案by RafazZ
All modules in Python have to have a certain directory structure. You can find details here.
Python 中的所有模块都必须具有特定的目录结构。您可以在此处找到详细信息。
Create an empty file called __init__.py
under the model
directory, such that your directory structure would look something like that:
__init__.py
在该model
目录下创建一个名为的空文件,这样您的目录结构将如下所示:
.
└── project
└── src
├── hello-world.py
└── model
├── __init__.py
└── order.py
Also in your hello-world.py
file change the import statement to the following:
同样在您的hello-world.py
文件中,将导入语句更改为以下内容:
from model.order import SellOrder
That should fix it
那应该解决它
P.S.: If you are placing your model
directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path
.
PS:如果您将model
目录放置在其他位置(不在同一目录分支中),则必须使用sys.path
.
回答by Cristóbal Sifón
you need a file named __init__.py
(two underscores on each side) in every folder in the hierarchy, so one in src/
and one in model/
. This is what python looks for to know that it should access a particular folder. The files are meant to contain initialization instructions but even if you create them empty this will solve it.
您需要__init__.py
在层次结构中的每个文件夹中都有一个名为(每边两个下划线)的文件,因此src/
一个在model/
. 这是 python 寻找知道它应该访问特定文件夹的内容。这些文件旨在包含初始化说明,但即使您将它们创建为空,这也能解决问题。
回答by Walker Sutton
You need to make sure the module is installed for allversions of python
您需要确保为所有版本的python安装了该模块
You can check to see if a module is installed for python by running:
您可以通过运行以下命令来检查是否为 python 安装了模块:
pip uninstall moduleName
PIP卸载MODULENAME
If it is installed, it will ask you if you want to delete it or not. My issue was that it was installed for python, but not for python3. To check to see if a module is installed for python3, run:
如果已安装,它会询问您是否要删除它。我的问题是它是为 python 安装的,而不是为 python3 安装的。要检查是否为 python3 安装了模块,请运行:
python3 -m pip uninstall moduleName
python3 -m PIP卸载MODULENAME
After doing this, if you find that a module is not installed for one or both versions, use these two commands to install the module.
执行此操作后,如果您发现某个模块没有为一个或两个版本安装,请使用这两个命令安装该模块。
- pip install moduleName
- python3 -m pip install moduleName
- PIP安装MODULENAME
- python3 -m PIP安装MODULENAME
回答by Afolabi Kolawole
After trying to add the path using:
尝试使用以下方法添加路径后:
pip show
on command prompt and using
在命令提示符下并使用
sys.path.insert(0, "/home/myname/pythonfiles")
and didn't work. Also got SSL error when trying to install the module again using conda this time instead of pip.
并没有工作。这次尝试使用 conda 而不是 pip 再次安装模块时也会出现 SSL 错误。
I simply copied the module that wasn't found from the path "Mine was in
我只是复制了从路径中找不到的模块“我的在
C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages
so I copied it to 'C:\Users\user\Anaconda3\Lib\site-packages'
所以我把它复制到 'C:\Users\user\Anaconda3\Lib\site-packages'
回答by lpkej
I had same error. For those who run python scripts on different servers, please check if the python path is correctly specified in shebang. For me on each server it was located in different dirs.
我有同样的错误。对于在不同服务器上运行python脚本的人,请检查shebang中是否正确指定了python路径。对我来说,它在每台服务器上都位于不同的目录中。
回答by Noah Bjorling
you need to import the function so the program know what that is here is example:
您需要导入该函数,以便程序知道这里是什么,例如:
import os
import pyttsx3
i had the same problem first then i import the function and it work so i would really recommend to try it
我首先遇到了同样的问题,然后我导入了该函数并且它可以工作,所以我真的建议您尝试一下