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

我已经检查了 sys.path 在那里我有目录 /site-packages. 。另外,我有文件 __init__.py.bin 在工具箱文件夹中,向Python表示这是一个软件包。我也有一个 __init__.py.bin 在示例目录中。

我不知道为什么Python在该文件中找到该文件 sys.path. 。有任何想法吗?可以是权限问题吗?我需要一些执行许可吗?

有帮助吗?

解决方案

根据您对Orip帖子的评论,我想这就是发生的事情:

  1. 你编辑 __init__.py 在窗户上。
  2. Windows编辑器添加了一些非打印的东西,也许是一个退货(Windows中的终端是CR/LF;在Unix中仅是LF),或者可能是CTRL-Z(Windows fun-en-eent offile)。
  3. 您使用WINSCP将文件复制到您的UNIX框。
  4. Winscp认为:“这有一些不是基本文本;我将放置.bin扩展名来指示二进制数据。”
  5. 失踪 __init__.py (现在叫 __init__.py.bin)表示Python不将工具包理解为软件包。
  6. 您创建 __init__.py 在适当的目录中,一切正常...?

其他提示

(local directory)/site-packages/toolkit

有一个 __init__.py?

进口 通过您的目录,每个目录都必须有一个 __init__.py 文件。

在 *nix上,还要确保正确配置了PythonPath,尤其是具有此格式:

 .:/usr/local/lib/python

(请注意 .: 一开始,它也可以在当前目录上进行搜索。)

它也可能在其他位置,具体取决于版本:

 .:/usr/lib/python
 .:/usr/lib/python2.6
 .:/usr/lib/python2.7 and etc.

当我在LPTHW中进行此练习时,我遇到了非常相似的事情。我永远无法让python认识到我正在拨打的目录中有文件。但是我能够最终使它工作。我所做的以及我建议的是尝试一下:

(注意:从您的初始帖子中,我假设您正在使用基于 *nix的计算机并从命令行运行内容,因此此建议是为此量身定制的。由于我运行Ubuntu,这就是我所做的)

1)将目录(CD)更改为目录 以上 文件所在的目录。在这种情况下,您正在尝试运行 mountain.py 文件,并试图调用 toolkit.interface.py 模块,在单独的目录中。在这种情况下,您将转到包含这两个文件的路径的目录(换句话说,这是两个文件共享的路径的最接近目录)。在这种情况下是 toolkit 目录。

2)当你在 tookit 目录,在您的命令行上输入此代码:

export PYTHONPATH=.

这将您的pythonpath设置为“”。这基本上意味着您的pythonpath现在将在您当前所在的目录中寻找任何调用文件 子目录分支 您所处的目录。因此,它不仅在您当前的目录中查看,而且还在所有目录中 您当前的目录)。

3)在上面的步骤中设置了pythonpath之后,请从当前目录运行模块( toolkit 目录)。 Python现在应该找到并加载您指定的模块。

希望这可以帮助。我自己对此感到非常沮丧。

我解决了自己的问题,我将写一个错误和解决方案的摘要:

该文件需要精确调用 __init__.py. 。如果扩展是不同的,例如我的情况 .py.bin 然后,Python无法通过目录移动,然后找不到模块。要编辑文件,您需要使用Linux编辑器,例如 vi 或者 纳米. 。如果您使用Windows编辑器,则会写一些隐藏的字符。

另一个影响它的问题是我已经安装了另一个Python版本,因此,如果有人正在使用Python的本地安装,请确保正在运行该程序的Python安装是本地Python。要检查一下,只需 which python, ,看看可执行文件是否是您本地目录中的一个。如果没有,请更改路径,但请确保本地Python目录比其他Python之前。

要将目录标记为包装,您需要一个名称的文件 __init__.py, ,这有帮助吗?

一个简单的解决方案是使用 python -m pip install <library-name> 代替 pip install <library-name>如果管理限制,您可以使用sudo

使用 PyCharm (一部分Jetbrains Suite)您需要将脚本目录定义为来源:
Right Click > Mark Directory as > Sources Root

是的。您需要目录来包含 __init__.py 文件,该文件是初始化软件包的文件。在这里,看看 这个.

__init__.py文件必须使Python视为包含软件包的目录;这样做是为了防止使用通用名称(例如字符串)的目录,从稍后在模块搜索路径上发生的无意间隐藏有效的模块。在最简单的情况下,__init__.py可以是一个空文件,但它也可以执行软件包的初始化代码或设置__ all__变量,稍后描述。

  1. 您必须将文件__ Init__.py放在同一目录中,该目录是您要导入的文件。
  2. 您无法尝试导入具有相同名称的文件,并是来自PythonPath上配置的2个文件夹的文件。

例如: /etc /环境

pythonpath = $ pythonpath:/opt/folder1:/opt/folder2

/opt/folder1/foo

/opt/folder2/foo

而且,如果您要导入Foo文件,Python将不知道您想要哪个文件。

来自foo import ... >>> Importerror:没有名为foo的模块

我的两分钱:

enter image description here

吐:

Traceback (most recent call last):
      File "bash\bash.py", line 454, in main
        import bosh
      File "Wrye Bash Launcher.pyw", line 63, in load_module
        mod = imp.load_source(fullname,filename+ext,fp)
      File "bash\bosh.py", line 69, in <module>
        from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
    ImportError: No module named RecordGroups

这使我感到困惑 - 浏览了帖子和帖子,暗示了丑陋的syspath hacks(正如您所看到的那样 __init__.py 都在那里)。事实证明,游戏/遗忘。我对记录此(相同名称)行为的解决方法和/或链接感兴趣 - > edit(2017.01.24) - 请查看 如果我有一个具有相同名称的模块和包裹怎么办? 有趣的是 软件包 优先考虑,但显然我们的发射器违反了这一点。

编辑(2015.01.17):我没有提及我们使用 自定义启动器 解剖 这里.

Linux:导入的模块位于/USR/local/lib/python2.7/dist-packages中

如果您正在使用C中编译的模块,请不要忘记在 sudo setup.py install.

sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so

您正在阅读此答案,说您 __init__.py 在正确的位置,您已经安装了所有依赖项,并且仍在获得 ImportError.

我面临类似的问题,除非我的程序在使用pycharm运行时运行良好,但是当我从终端运行时,上述错误。进一步挖掘后,我发现 PYTHONPATH 没有项目目录的条目。所以,我设定了 PYTHONPATH导入声明在Pycharm上起作用,但不适用于终端:

export PYTHONPATH=$PYTHONPATH:`pwd`  (OR your project root directory)

还有另一种方法可以使用 sys.path 作为:

import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')

您可以根据希望搜索项目的顺序使用插入/附加。

就我而言,问题是我要链接到 调试 python & boost::Python, ,这要求扩展为 FooLib_d.pyd, , 不只是 FooLib.pyd;重命名文件或更新 CMakeLists.txt 属性修复了错误。

My problem was that I added the directory with the __init__.py file to PYTHONPATH, when actually I needed to add its parent directory.

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 complaints ImportError: No module named bar. Changing to import foo; print foo, which will show the path of foo. Is it what you expect?

If not, Either rename foo or use absolute imports.

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:

  • In PyCharm, from menu 'File'->Settings
  • In Settings dialog, Project: XXXProject->Project Interpreter
  • Click "Add" button, it will show you 'Available Packages' dialog
  • Search your library, click 'Install Package'
  • Then, all you needed package will be installed in you project custom 'venv' folder.

Settings dialog

Enjoy.

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.

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

try:
    from namespace import something 
except NameError:
    from .namespace import something

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

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.

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:

Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py

In 'main.py' I had tried unsuccessfully all the combinations bellow:

from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...

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 booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass

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.

In my case I was including the path to package.egg folder rather than the actual package underneath. I copied the package to top level and it worked.

This worked for me: Created __init__.py file inside parent folder (in your case, inside site-packages folder). And imported like this:

from site-packages.toolkit.interface import interface

Hope it will be useful for you as well !

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top