确定将哪些内容添加到 PYTHONPATH 来解决导入问题的一般过程/概念是什么?
我在 Django 项目(在带有 PyDev 的 Eclipse 中)中不断遇到未解决的导入问题。在对给定问题进行一段时间的谷歌搜索后,我通常可以找到正确的内容添加到 PYTHONPATH 中来解决问题。但似乎我缺少一些概念或过程,通过它我可以明确地说“显然我应该添加一些东西才能正确导入该东西”。更令人沮丧的是,我只是遵循基本的 Django 教程,所以我希望这些事情能够简单地处理。在 Java 世界中,它通常非常清楚,因为我可以准确地找出特定包来自哪个库,下载它并将其添加到构建路径。 Python/Django 世界中的等效概念是什么?
我并不是要解决这里的特定问题,而是想知道要遵循的步骤是什么,或者在哪里查找它,或者我缺少的概念。或者可能会发现没有一个并且它总是猜测......
我正在 Ubuntu 10.10、Python 2.6 上运行
I keep running into problems with Unresolved imports in my Django project (in Eclipse w/ PyDev). After googling for a while on a given issue I can generally find the right thing to add to PYTHONPATH to solve the problem. But it seems like I'm missing some concept or process by which I can definitively say "obviously there is what I should add to get that thing to import properly". What makes this more frustrating is that I'm just following the basic Django tutorial, so I'd expect these things to be simply dealt with. In Java world it is usually pretty clear because I can find out exactly what library a particular package is coming from, download it and add it to build path. What is the equivalent concept in Python/Django world?
I am not looking to solve a specific problem here, but rather to know what are the steps to follow, or where to look it up, or the concept I am missing. Or possibly to find out that there is not one and its always guesswork...
I am running on Ubuntu 10.10, Python 2.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 eclipse,她可以自动配置 python 解释器(包括系统范围内安装的所有库)。您可能已经完成了这一步。
如果您仍然面临导入问题,您可能没有在系统范围内安装库。或者您修改了 python 路径并删除了默认目录。
通常有一个包含 python 库的 setup.py。它将将该库安装到您的系统中:
setup.py
附带的一些库可以从 python 包索引 和 pip。如果库不提供 setup.py 脚本,那么您将需要手动处理它。
所以关于 python 库的基本规则是:如果你没有在系统范围内安装它们,那么你必须手动将它们添加到 python-path 中。
如果您不想在系统范围内安装一堆库,可以使用 virtualenv 或 buildout 可以提供帮助。
Virtualenv 创建一个环境,您可以在其中安装库,就像全局安装它们一样:
Buildout 的工作方式略有不同。您在配置文件中指定要安装的软件包列表,它将生成路径固定的脚本文件。 Buildout 还可以为 pydev 项目生成配置设置。 Buildout 是一种自动化工具,以自己的方式创建虚拟环境。 Buildout 比 virtualenv 做更多的事情,但一开始可能很难处理。
If you are using eclipse, she can automatically configure python interpreter (includes all libraries which are installed system-wide). You probably have done this step.
If you are still facing import problems you probably did not installed libraries system-wide. Or you have modified python paths and removed default directories.
Usually there is a
setup.py
with python library. It will install that library into your system:Some libraries which comes with
setup.py
can be installed from python package index with pip.If library does not provide
setup.py
script then you will need to deal with it manually.So basic rule about python libraries is: if you did not installed them system wide, then you have to add them to python-path manually.
If you do not want to install bunch of libraries system-wide, tools like virtualenv or buildout can help.
Virtualenv creates an environment where you can install libraries as if you would install them globally:
Buildout works a bit differently. You specify a list of packages you want to install in a configuration files, and it will generate script files with paths fixed. Buildout can also generate configuration settings for
pydev
project. Buildout is automization tool with its own way to create virtual environment. Buildout does more things than virtualenv, but can be difficult to deal with at first.从你的问题来看,我假设你对 Python 很陌生,所以让我们从头开始。
我的建议是查看 Django 生成的回溯并搜索如下所示的行:“ImportError:没有名为 somemodule 的模块”。这会告诉您缺少的模块的名称。
一旦您知道了模块的名称,您就可以尝试使用 easy_install 来查看 PyPi(Python 包索引)中是否列出了缺少的模块。
尝试运行:
如果幸运的话,缺少的模块将被安装到 python 安装的 site-packages 目录中(可能是 /usr/lib/python2.6/site-packages,具体取决于 python 的安装位置)。 site-packages 目录是所有第 3 方模块应安装的位置,并且此目录中的所有模块始终可导入并位于 PYTHONPATH 上。
您的代码可能正在尝试导入 PyPi 上不可用的内容,在这种情况下,Google 是您的最佳选择 :p
p.s.
如果 easy_install 不起作用(或者丢失),那么您可能没有安装 setuptools python 模块,您可以从这里获取该模块: http://pypi.python.org/pypi/setuptools
From your question I assume that you are quite new to Python, so let's start at the beginning.
My advice would be to look at the traceback that Django generates and search for a line which looks like this: "ImportError: No module named somemodule". This tells you the name of the missing module.
Once you know the name of the module you can try to see if the missing module is listed in PyPi (Python Package Index) by using easy_install.
Try running:
If you're lucky then the missing module will be installed into your python installation's site-packages directory (possibly /usr/lib/python2.6/site-packages, depending on where your python is installed). The site-packages directory is where all 3rd party modules should be installed to, and all modules in this directory are always import-able and on the PYTHONPATH.
It's possible that your code is trying to import something that is not available on PyPi, in which case Google is your best option :p
p.s.
If easy_install doesn't work (or it's missing) then it's possible that you don't have the setuptools python module installed, which you can get from here: http://pypi.python.org/pypi/setuptools
如果您要在系统范围内安装模块(站点包或类似的),那么在系统上运行它们可能不会有问题,所以我将讨论您项目的 PYTHONPATH。
作为程序入口路径的文件目录将与所有系统定义的 PYTHONPATH 一起添加到 PYTHONPATH。要访问该目录结构中的任何脚本,每个目录必须包含一个名为 __init__.py 的文件,该文件表明该目录是程序中的一个模块。
如果运行
python entrypoint.py
,您将能够访问app1
,因为从entrypoint.py
到app1
的所有文件夹> 包含一个__init__.py
文件。但是,app2
无法导入,因为它作为模块不可见。如果您有一个模块目录,则必须确保它包含
__init__.py
,或者/path/to/app1
位于 sys.path 本身内,然后只需使用导入模型
即可导入。If you're installing modules system wide (site-packages or similar), you probably won't have a problem running them on your system, so I'm going to discuss the PYTHONPATH of your project.
The directory of the file that is your entry path into your program is added to the PYTHONPATH, along with all the system defined PYTHONPATHS. To get to any script within that directory structure, each directory must contain a file called
__init__.py
, which signals that that directory is a module within your program.If run
python entrypoint.py
you will be able to accessapp1
, because all folders fromentrypoint.py
toapp1
contain an__init__.py
file. However,app2
can't be imported, since it is not visible as a module.If you have a directory that is a module, you must ensure that it contains an
__init__.py
, or that/path/to/app1
is within sys.path itself, which can then be imported simply withimport models
.