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


当前回答

当我在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.

其他回答

我也有类似的问题。我创建了一个名为python3.6的新虚拟环境。

conda create -n python3.6 python=3.6
pip install pandas

一切正常,但当我运行脚本时,发生了一个错误

ModuleNotFoundError: No module named 'pandas'

我发现Python包的元数据和pip的缓存已经更新,但它实际上没有下载pandas包。 所以我试着让皮普重新安装

pip uninstall pandas --no-cache-dir
pip install pandas 

这就解决了问题。

您必须将文件__ init__.py放在您要导入的文件所在的同一目录中。 不能尝试从PYTHONPATH上配置的两个文件夹中导入同名的文件。

例如: /etc/environment

PYTHONPATH = PYTHONPATH:美元/ opt / folder1: / opt / folder2

/opt/folder1/foo

/opt/folder2/foo

而且,如果你试图导入foo文件,python将不知道你想要哪个。

从foo import…>>> importerror:没有foo模块

另一个原因导致了这个问题

file.py

#!/bin/python
from bs4 import BeautifulSoup

如果你的默认python是pyyhon2

$ file $(which python)
/sbin/python: symbolic link to python2

File.py在这种情况下需要python3 (bs4) 你不能像这样用python2执行这个模块:

$ python file.py
# or
$ file.py
# or
$ file.py # if locate in $PATH

两种方法来修复这个错误,

# should be to make python3 as default by symlink
$ rm $(which python) && ln -s $(which python3) /usr/bin/python
# or use alias
alias python='/usr/bin.../python3'

或者将file.py中的shebang修改为

#!/usr/bin/...python3

使用sys.path.append对我有用,就像在回答中提到的那样。起初,我认为它不能工作,直到我意识到在目录路径之前有一个空格。所以要确保路径前没有错别字或空格

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