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


当前回答

对我来说,将该文件作为模块运行很有帮助。

而不是

python myapp/app.py

使用

python -m myapp.app

这并不完全相同,但在某些情况下这可能是一种更好的方法。

其他回答

当我在LPTHW中做这个练习时,我遇到了非常相似的情况;我永远无法让Python识别我调用的目录中有文件。但最后我还是让它工作了。我所做的,以及我所推荐的,是这样做的:

(注意:从你最初的文章中,我假设你使用的是基于* nix的机器,并从命令行运行,所以这个建议是为你量身定做的。因为我运行Ubuntu,这是我所做的)

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 the toolkit.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 the toolkit 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.

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

Try

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

我的观点是:

随地吐痰:

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

这招对我很管用: 在父文件夹内创建__init__.py文件(在您的情况下,在site-packages文件夹内)。并像这样导入:

from site-packages.toolkit.interface import interface

希望对你也有用!

在我的例子中,我使用sys.path.insert()导入一个本地模块,并从其他库中找不到模块。我不得不把sys.path.insert()放在报告模块未找到的导入下面。我认为最好的做法是将sys.path.insert()放在导入的底部。