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


当前回答

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

其他回答

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

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

对我来说,这是件非常愚蠢的事。我使用pip3 install安装了这个库,但是我运行的程序是python program.py,而不是python3 program.py。

使用PyCharm (JetBrains套件的一部分),你需要定义你的脚本目录为Source: 右键单击>将目录标记为> Sources Root

如果你正在使用安装脚本/实用程序(例如setuptools)来部署你的包,不要忘记将相应的文件/模块添加到安装程序中。


在支持的情况下,使用find_packages()或类似的方法自动向设置脚本添加新包。这绝对会让你免于头疼,尤其是当你把项目搁置一段时间,然后再添加一些东西的时候。

import setuptools

setuptools.setup(
    name="example-pkg",
    version="0.0.1",
    author="Example Author",
    author_email="author@example.com",
    description="A small example package",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

(示例取自setuptools文档)

在ubuntu apt-get安装程序中,python3版本的包通常被命名

python3-XYZ

以及python2版本

python-XYZ

根据经验,请尝试错误消息中提到的包的python3-XYZ或python-XYZ。 不需要猜测,使用RegEx搜索apt缓存。然后:

$ apt-cache search "python.*toolkit.*interface"
python3-cli-helpers - easy command-line apps with Python
python3-exam - Python module to help write better tests
python3-fltk - Python wrapper for the Fast Light Toolkit
python3-mpltoolkits.basemap - matplotlib toolkit to plot on map projections (Python 3)
python3-nltk - Python3 libraries for natural language processing
python3-onnx - Open Neural Network Exchange (ONNX) (Python)
python3-paraview - Parallel Visualization Application. python-support
python3-pyswarms - research toolkit for particle swarm optimization in Python
python3-wxgtk-media4.0 - Python 3 interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.media)
python3-wxgtk-webview4.0 - Python 3 interface to the wxWidgets Cross-platform C++ GUI toolkit (wx.html2)
python3-wxgtk4.0 - Python 3 interface to the wxWidgets Cross-platform C++ GUI toolkit
python3-xapian - Xapian search engine interface for Python3
wxglade - GUI designer written in Python with wxPython

它没有找到它。

请注意,这种apt-get技巧有时也需要用于依赖的包。

我对python2.7的flask包有相同的错误消息,当我尝试时它消失了:

sudo apt-get install python-flask

因此,试一试:

sudo apt-get install python-[YOURPYTHONVERION]-[YOURERRORPACKAGE]

或者检查上面提到的依赖关系。