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.
我的观点是:
随地吐痰:
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):我没有提到我们使用一个自定义启动器解剖这里。
我有同样的问题(Python 2.7 Linux),我已经找到了解决方案,我想分享它。在我的情况下,我有下面的结构:
Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py
在'main.py'中,我尝试了以下所有的组合,但都失败了:
from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...
解决办法比我想象的要简单得多。我把文件夹“小册子”重命名为“小册子”,就是这样。现在Python可以通过在'main.py'中使用代码来正常导入类Question:
from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass
由此我可以得出结论,像“小册子”这样的包名(文件夹)必须以小写开头,否则Python会将其与类名和文件名混淆。
显然,这不是你的问题,但John Fouhy的回答非常好,这个帖子几乎有任何可能导致这个问题的东西。所以,这是另一件事,我希望这能帮助到其他人。
你正在阅读这个答案说你的__init__.py是在正确的地方,你已经安装了所有的依赖项,你仍然得到ImportError。
我也遇到了类似的问题,除了我的程序在使用PyCharm运行时运行良好,但当我从终端运行时出现上述错误。在深入研究之后,我发现PYTHONPATH没有项目目录的条目。所以,我设置PYTHONPATH每个Import语句在PyCharm上工作,但不是从终端:
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
还有另一种方法可以使用sys。路径为:
import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')
您可以根据希望搜索项目的顺序使用插入/追加。
如果你正在使用安装脚本/实用程序(例如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文档)
我也有类似的问题。我创建了一个名为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
这就解决了问题。
在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]
或者检查上面提到的依赖关系。
另一个原因导致了这个问题
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