Python 错误“ImportError:没有命名的模块”
Python 安装在本地目录中。
我的目录树如下所示:
(local directory)/site-packages/toolkit/interface.py
我的代码在这里:
(local directory)/site-packages/toolkit/examples/mountain.py
要运行该示例,我编写了 python mountain.py
,并且在我的代码中:
from toolkit.interface import interface
我收到错误:
Traceback (most recent call last):
File "mountain.py", line 28, in ?
from toolkit.interface import interface
ImportError: No module named toolkit.interface
我已经检查了 < code>sys.path 那里有目录 /site-packages
。 另外,我在工具包文件夹中有文件 __init__.py.bin
来向 Python 表明这是一个包。 我的示例目录中还有一个 __init__.py.bin
。
我不知道为什么Python在sys.path
中找不到该文件。 有任何想法吗? 会不会是权限问题? 我需要一些执行权限吗?
Python is installed in a local directory.
My directory tree looks like this:
(local directory)/site-packages/toolkit/interface.py
My code is in here:
(local directory)/site-packages/toolkit/examples/mountain.py
To run the example, I write python mountain.py
, and in the code I have:
from toolkit.interface import interface
And I get the error:
Traceback (most recent call last):
File "mountain.py", line 28, in ?
from toolkit.interface import interface
ImportError: No module named toolkit.interface
I have already checked sys.path
and there I have the directory /site-packages
. Also, I have the file __init__.py.bin
in the toolkit folder to indicate to Python that this is a package. I also have a __init__.py.bin
in the examples directory.
I do not know why Python cannot find the file when it is in sys.path
. Any ideas? Can it be a permissions problem? Do I need some execution permission?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
根据您对 orip 帖子的评论,我猜发生了这样的事情:
__init__.py
。__init__.py
(现在称为__init__.py.bin
)意味着 python 不将工具包理解为一个包。__init__.py
并且一切正常......?Based on your comments to orip's post, I guess this is what happened:
__init__.py
on windows.__init__.py
(now called__init__.py.bin
) means python doesn't understand toolkit as a package.__init__.py
in the appropriate directory and everything works... ?当我在 LPTHW 做这个练习时,我遇到了非常相似的事情; 我永远无法让 Python 识别出我调用的目录中有文件。 但我最终让它发挥作用。 我所做的以及我建议的就是尝试以下操作:(
注意:从您最初的帖子中,我假设您正在使用基于 *NIX 的计算机并从命令行运行某些内容,因此此建议是针对该情况量身定制的由于我运行 Ubuntu,这就是我所做的)
将目录 (cd) 更改为文件所在目录的上方目录。 在本例中,您尝试运行
mountain.py
文件,并尝试调用toolkit.interface.py
模块,它们位于不同的目录中。 在这种情况下,您将转到包含这两个文件的路径的目录(或者换句话说,这两个文件的路径共享的最近的目录)。 在本例中是toolkit
目录。当您位于
toolkit
目录中时,在命令行中输入以下代码行:导出 PYTHONPATH=.
这会将您的 PYTHONPATH 设置为“.”,这基本上意味着您的 PYTHONPATH 现在将在您当前所在的目录中查找任何被调用的文件(更重要的是,在子目录分支中< /em> 您所在的目录。因此,它不仅会查找当前目录,还会查找当前目录中的所有目录。
在上述步骤中设置 PYTHONPATH 后,从当前目录(
toolkit
目录)运行模块。 Python 现在应该找到并加载您指定的模块。I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the
mountain.py
file, and trying to call thetoolkit.interface.py
module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is thetoolkit
directory.When you are in the
toolkit
directory, enter this line of code on your command line:export PYTHONPATH=.
This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).
After you've set your PYTHONPATH in the step above, run your module from your current directory (the
toolkit
directory). Python should now find and load the modules you specified.吗
有
__init__.py
? 要使 import 遍历目录,每个目录都必须有一个
__init__.py
文件。Does
have a
__init__.py
?To make import walk through your directories every directory must have a
__init__.py
file.确保 PYTHONPATH 配置正确,尤其是它具有以下格式:(
注意开头的
.:
,以便它也可以在当前目录中搜索。)在 *nix 上,还要 也可能位于其他位置,具体取决于版本:
On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:
(Mind the
.:
at the beginning, so that it can search on the current directory, too.)It may also be in other locations, depending on the version:
您正在阅读这个答案,表明您的 __init__.py 位于正确的位置,您已经安装了所有依赖项,但仍然收到
ImportError
。我遇到了类似的问题,除了我的程序在使用 PyCharm 运行时运行良好,但当我从终端运行它时出现上述错误。 经过进一步挖掘,我发现
PYTHONPATH
没有项目目录的条目。 因此,我设置了PYTHONPATH
per Import 语句适用于 PyCharm,但不适用于终端:还有另一种方法使用
sys.path
来执行此操作:您可以根据您希望搜索项目的顺序使用 insert/append。
You are reading this answer says that your
__init__.py
is in the right place, you have installed all the dependencies and you are still getting theImportError
.I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that
PYTHONPATH
didn't have the entry for the project directory. So, I setPYTHONPATH
per Import statement works on PyCharm but not from terminal:There's another way to do this using
sys.path
as:You can use insert/append based on the order in which you want your project to be searched.
使用 PyCharm(JetBrains 套件的一部分),您需要将脚本目录定义为源:
右键单击> 将目录标记为 > 来源根
Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
Right Click > Mark Directory as > Sources Root
我解决了我自己的问题,我会写一个错误的地方和解决方案的总结:
该文件需要准确地调用
__init__.py
。 如果扩展名不同,例如在我的例子中.py.bin
,那么 Python 就无法在目录中移动,然后就无法找到模块。 要编辑文件,您需要使用 Linux 编辑器,例如 vi 或 nano。 如果您使用 Windows 编辑器,这将写入一些隐藏字符。影响它的另一个问题是我通过 root 安装了另一个 Python 版本,因此如果有人使用本地安装的 python,请确保运行程序的 Python 安装是本地 Python。 要检查这一点,只需执行
which python
,然后查看可执行文件是否位于本地目录中。 如果不是,请更改路径,但请确保本地 Python 目录早于其他 Python 目录。I solved my own problem, and I will write a summary of the things that were wrong and the solution:
The file needs to be called exactly
__init__.py
. If the extension is different such as in my case.py.bin
then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do
which python
, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.对我来说,这确实是一件愚蠢的事情。 我使用
pip3 install
安装了该库,但将我的程序运行为python program.py
,而不是python3 program.py
。For me, it was something really stupid. I installed the library using
pip3 install
but was running my program aspython program.py
as opposed topython3 program.py
.一个简单的解决方案是使用
python -m pip install
而不是pip install
安装模块如果受到管理员限制,您可以使用 sudo
an easy solution is to install the module using
python -m pip install <library-name>
instead ofpip install <library-name>
you may use sudo in case of admin restrictions
要将目录标记为包,您需要一个名为
__init__.py
的文件。To mark a directory as a package you need a file named
__init__.py
.对于所有仍然有这个问题的人。 我相信 Pycharm 会与导入混淆。 对我来说,当我写“从名称空间导入某些内容”时,前一行会用红色下划线表示,表明存在错误,但可以工作。 然而,“from .namespace import Something”没有下划线,但也不起作用。
尝试
To all those who still have this issue. I believe Pycharm gets confused with imports. For me, when i write 'from namespace import something', the previous line gets underlined in red, signaling that there is an error, but works. However ''from .namespace import something' doesn't get underlined, but also doesn't work.
Try
是的。 您需要该目录包含
__init__.py
文件,该文件是初始化包的文件。 在这里,看看这个。Yup. You need the directory to contain the
__init__.py
file, which is the file that initializes the package. Here, have a look at this.如果您尝试了上面提供的所有方法但都失败了,则可能您的模块与内置模块同名。 或者,文件夹中存在同名的模块,该模块在
sys.path
中的优先级高于您的模块。要进行调试,请说出您的
from foo.bar import baz
投诉ImportError: No module named bar
。 更改为import foo; print foo
,这将显示foo
的路径。 是你所期望的吗?如果没有,请重命名
foo
或使用 绝对导入。If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in
sys.path
than your module's.To debug, say your
from foo.bar import baz
complaintsImportError: No module named bar
. Changing toimport foo; print foo
, which will show the path offoo
. Is it what you expect?If not, Either rename
foo
or use absolute imports.我的两分钱:
吐槽:
这让我很困惑 - 浏览了建议丑陋的 syspath 的帖子和帖子黑客(如你所见,我的
__init__.py
都在那里)。 事实证明 game/oblivion.py 和 game/oblivion 混淆了 python它吐出相当无用的“没有名为 RecordGroups 的模块”。 我对解决方法和/或记录此(同名)行为的链接感兴趣 -> 编辑(2017.01.24) - 看看 如果我有一个模块和一个包会怎样同名? 有趣的是,通常包优先,但显然我们的启动器违反了这一点。
编辑(2015.01.17):我没有提到我们使用 自定义启动器在此处进行了剖析。
My two cents:
Spit:
This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my
__init__.py
were all there). Well turns out that game/oblivion.py and game/oblivion was confusing pythonwhich spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.
EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.
对我来说,将文件作为模块运行有帮助。
而不是
使用
它不完全相同,但在某些情况下它可能是更好的方法。
For me, running the file as a module helped.
Instead of
using
It's not exactly the same but it might be a better approach in some cases.
例如:
/etc/environment
PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2
/opt/folder1/foo
/opt/folder2/foo
并且,如果您尝试导入 foo 文件,python 将不知道您想要哪一个。
从 foo 导入... >>> 导入错误:没有名为 foo 的模块
eg:
/etc/environment
PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2
/opt/folder1/foo
/opt/folder2/foo
And, if you are trying to import foo file, python will not know which one you want.
from foo import ... >>> importerror: no module named foo
通过编写
print (sys.path)
解决了我的问题,并发现尽管进行了全新安装,但 python 仍在使用过时的软件包。 删除这些使得 python 自动使用正确的包。Fixed my issue by writing
print (sys.path)
and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.就我而言,问题是我链接到 debug
python
&boost::Python
,要求扩展名是FooLib_d.pyd
,而不仅仅是FooLib.pyd
; 重命名文件或更新CMakeLists.txt
属性修复了该错误。In my case, the problem was I was linking to debug
python
&boost::Python
, which requires that the extension beFooLib_d.pyd
, not justFooLib.pyd
; renaming the file or updatingCMakeLists.txt
properties fixed the error.就我而言,因为我使用 PyCharm,PyCharm 为项目文件夹中的每个项目创建一个“venv”,但它只是 python 的迷你环境。 虽然您已经在Python中安装了所需的库,但在您的自定义项目“venv”中,它不可用。 这就是 PyCharm 中出现“ImportError: No module name xxxxxx”的真正原因。
要解决此问题,您必须通过以下步骤将库添加到项目自定义环境中:
享受吧。
In my case, because I'm using PyCharm and PyCharm create a 'venv' for every project in project folder, but it is only a mini env of python. Although you have installed the libraries you need in Python, but in your custom project 'venv', it is not available. This is the real reason of 'ImportError: No module named xxxxxx' occurred in PyCharm.
To resolve this issue, you must add libraries to your project custom env by these steps:
Enjoy.
Linux:导入的模块位于 /usr/local/lib/python2.7/dist-packages
如果您使用的是用 C 编译的模块,请不要忘记在 sudo setup.py 之后 chmod .so 文件安装。
Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages
If you're using a module compiled in C, don't forget to chmod the .so file after
sudo setup.py install
.我的问题是,我将带有
__init__.py
文件的目录添加到 PYTHONPATH,而实际上我需要添加其父目录。My problem was that I added the directory with the
__init__.py
file to PYTHONPATH, when actually I needed to add its parent directory.如果您使用安装脚本/实用程序(例如setuptools)来部署您的包,请不要不要忘记将相应的文件/模块添加到安装程序中。
如果支持,请使用
find_packages()
或类似方法自动将新包添加到安装脚本中。 这绝对会让您免于头痛,尤其如果您将项目搁置一段时间然后稍后添加一些内容。(示例取自 setuptools 文档)
If you are using a setup script/utility (e.g. setuptools) to deploy your package, don't forget to add the respective files/modules to the installer.
When supported, use
find_packages()
or similar to automatically add new packages to the setup script. This will absolutely save you from a headache, especially if you put your project aside for some time and then add something later on.(Example taken from setuptools documentation)
我遇到了同样的问题(Python 2.7 Linux),我找到了解决方案,我想分享它。 就我而言,我的结构如下:
在“main.py”中,我尝试了以下所有组合,但均未成功:
解决方案比我想象的要简单得多。 我将文件夹“Booklet”重命名为“booklet”,就是这样。 现在,Python 可以通过在“main.py”中使用代码正常导入类问题:
由此我可以得出结论,像“booklet”这样的包名称(文件夹)必须从小写字母开头,否则 Python 会将其与类名称混淆,并且文件名。
显然,这不是您的问题,但是 John Fouhy 的回答非常好,并且该线程几乎包含任何可能导致此问题的内容。 所以,这是另一件事,我希望这可以帮助其他人。
I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:
In 'main.py' I had tried unsuccessfully all the combinations bellow:
The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:
From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.
Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.
在 Linux 服务器中尝试
dos2unix script_name
(使用命令
find . -name '*.pyc' -delete
pyc 文件) >)如果您在 Windows 上处理脚本,则重新运行
In linux server try
dos2unix script_name
(remove all (if there is any)
pyc
files with commandfind . -name '*.pyc' -delete
)and re run in the case if you worked on script on windows
就我而言,我使用 sys.path.insert() 导入本地模块,并从不同的库中获取
未找到模块
。 我必须将 sys.path.insert() 放在报告找不到模块
的导入下面。 我想最好的做法是将 sys.path.insert() 放在导入的底部。In my case, I was using
sys.path.insert()
to import a local module and was gettingmodule not found
from a different library. I had to putsys.path.insert()
below the imports that reportedmodule not found
. I guess the best practice is to putsys.path.insert()
at the bottom of your imports.我发现更改别名文件夹 (Mac) 的名称(通过 GUI)可能会导致加载模块出现问题。 如果原文件夹名称发生更改,请重新制作符号链接。 我不确定这种行为有多普遍,但调试起来很令人沮丧。
I've found that changing the name (via GUI) of aliased folders (Mac) can cause issues with loading modules. If the original folder name is changed, remake the symbolic link. I'm unsure how prevalent this behavior may be, but it was frustrating to debug.
如果你的截止日期很紧,而其他一切都失败了:
If you're on a tight deadline and all else fails:
在遇到同样的问题后,我发现我的解决方案是从项目中删除所有 pyc 文件,似乎这些缓存的文件以某种方式导致了此错误。
我发现执行此操作的最简单方法是导航到 Windows 资源管理器中的项目文件夹并搜索
*.pyc
,然后选择全部 (Ctrl+A)并删除它们(Ctrl+X)。我可能可以通过删除特定的 pyc 文件来解决我的问题,但我从未尝试过这个
After just suffering the same issue I found my resolution was to delete all
pyc
files from my project, it seems like these cached files were somehow causing this error.Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for
*.pyc
, then selecting all (Ctrl+A) and deleting them (Ctrl+X).Its possible I could have resolved my issues by just deleting the specific
pyc
file but I never tried this我遇到了同样的问题:
导入错误
。 此外,该库已 100% 正确安装。 问题的根源是我的PC上安装了3版本的python(anaconda包)。 这就是为什么库没有安装在正确的位置。 之后,我只是在 IDE PyCharm 中更改为正确的 python 版本。I faced the same problem:
Import error
. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.我有同样的错误。 这是由于有人在与我的脚本相同的文件夹中创建了一个文件夹,该文件夹的名称与我从其他地方导入的模块冲突。 它没有导入外部模块,而是查看了该文件夹的内部,该文件夹显然不包含预期的模块。
I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.