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文件的文件编码必须为utf-8。

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

其他回答

根据你对orip帖子的评论,我猜事情是这样的:

You edited __init__.py on windows. The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file). You used WinSCP to copy the file to your unix box. WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data." The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package. You create __init__.py in the appropriate directory and everything works... ?

Does

(local directory)/site-packages/toolkit

有__init__.py?

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

当我在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和PyCharm为项目文件夹中的每个项目创建了一个“venv”,但它只是python的一个迷你env。虽然您已经在Python中安装了所需的库,但在您的自定义项目“venv”中,它不可用。这是PyCharm中出现'ImportError: No module named xxxxxx'的真正原因。 要解决此问题,您必须按以下步骤向项目自定义env添加库:

在PyCharm中,从菜单“文件”->设置 在“设置”对话框中,输入“项目:XXXProject->项目解释器” 点击“添加”按钮,会弹出“Available Packages”对话框 搜索你的库,点击“安装包” 然后,所有你需要的包将安装在你的项目自定义的“venv”文件夹。

享受。

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

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