[英]ImportError: No module named utils.read
I have a main.py
file in the folder project
and read.py
in the folder ./project/utils
. 我有一个
main.py
在文件夹中的文件project
和read.py
在文件夹./project/utils
。 In the main.py
, I called 在
main.py
,我打电话
import sys, os
sys.path.append('./utils/')
from utils.read import function1
However, when I use the python main.py
command, I got the error ImportError: No module named utils.read
. 但是,当我使用
python main.py
命令时,出现错误ImportError: No module named utils.read
。 What should I change? 我应该改变什么? Thanks all
谢谢大家
i think you need to add __init__.py
我认为您需要添加
__init__.py
in your directory.. 在您的目录中。
make sure you have __init__.py
in utils
folder.. then only python will understand it is package folder contains py 确保您在
utils
文件夹中有__init__.py
。然后只有python会理解它是package文件夹中包含py
__init__.py
specifies that the folder is actually a package. __init__.py
指定该文件夹实际上是一个程序包。 a package in python is a directory containing .py files (modules). python中的软件包是一个包含.py文件(模块)的目录。 In every folder which is a package (that is, a folder containing multiple .py files) you should define
__init__.py
. 在包的每个文件夹中(即,一个包含多个.py文件的文件夹),您应该定义
__init__.py
。 It can be empty, or you can put some statements to execute when the package is imported (or modules from the package). 它可以为空,或者可以在导入包(或包中的模块)时放入一些语句来执行。
For exmaple, let's take this directory tree: /dev/package/greeter.py and current working directory is /dev . 例如,让我们以以下目录树为例 : /dev/package/greeter.py ,当前工作目录为/ dev 。
>>> from package import greeter
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
from package import greeter
ImportError: No module named package
import pakcage
results in the same error. import pakcage
导致相同的错误。 When adding __init__.py into the package folder , it works. 将__init__.py添加到package文件夹时 ,它可以工作。 My init is simple as
我的初始化很简单
print 'init executed'
>>> from package import greeter
init executed
>>>
One common functionality to put in __init__.py
is the __all__
variable. __all__
变量是__init__.py
中的一种常用功能。 You can read more about it here Can someone explain __all__ in Python? 您可以在此处了解更多信息。 有人可以用Python解释__all__吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.