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中找到该文件。什么好主意吗?会是权限问题吗?我需要一些执行许可吗?


当前回答

Does

(local directory)/site-packages/toolkit

有__init__.py?

为了让导入遍历你的目录,每个目录必须有一个__init__.py文件。

其他回答

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

需要__init__.py文件才能使Python将目录视为包含包;这样做是为了防止具有通用名称的目录(如string)在无意中隐藏之后在模块搜索路径上出现的有效模块。在最简单的情况下,__init__.py可以只是一个空文件,但它也可以执行包的初始化代码或设置__all__变量,稍后将介绍。

在*nix上,还要确保PYTHONPATH配置正确,特别是它具有以下格式:

 .:/usr/local/lib/python

(注意开头的.:,这样它也可以在当前目录上搜索。)

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

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

所有python文件的文件编码必须为utf-8。

请看这里:https://github.com/jupyter/notebook/issues/3166#issuecomment-581332097

一个简单的解决方案是使用python -m PIP install <library-name>来安装模块,而不是PIP install <library-name> 在管理限制的情况下,您可以使用sudo

我的观点是:

随地吐痰:

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):我没有提到我们使用一个自定义启动器解剖这里。