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

我已经检查过系统了。这里有目录/site-packages。此外,我在工具包文件夹中有__init__.py.bin文件,以向Python表明这是一个包。我在examples目录中也有一个__init__.py.bin。

我不知道为什么Python无法在sys.path中找到该文件。什么好主意吗?会是权限问题吗?我需要一些执行许可吗?


当前回答

我发现改变别名文件夹(Mac)的名称(通过GUI)会导致加载模块的问题。如果原文件夹名称已更改,请重新制作符号链接。我不确定这种行为有多普遍,但调试起来很令人沮丧。

其他回答

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

如果你使用的是C语言编译的模块,不要忘记在sudo setup.py install后chmod .so文件。

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

我的观点是:

随地吐痰:

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

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 python which 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.

编辑(2015.01.17):我没有提到我们使用一个自定义启动器解剖这里。

致所有还有这个问题的人。我相信Pycharm与import混淆了。对我来说,当我写“从命名空间导入一些东西”时,前一行会用红色划线,表明有错误,但有效。然而,“from .namespace import something”没有下划线,但也不起作用。

Try

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

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

我也遇到了同样的问题:导入错误。此外,库已经100%正确安装。问题的根源是在我的PC上安装了3版的python (anaconda包)。这就是为什么图书馆没有安装在正确的地方。之后,我只是在我的IDE PyCharm中更改了适当的python版本。