如何设置本地Python库目录/PYTHONPATH?

发布于 2024-10-06 17:28:45 字数 4664 浏览 0 评论 0 原文

今天在尝试编写一个使用 PIL 的 Python 脚本的过程中,我发现我的本地计算机上似乎没有它(OS X 10.5.8,默认安装 2.5 Python)。

所以我运行:

easy_install --prefix=/usr/local/python/ pil

它抱怨 /usr/local/python/lib/python2.5/site-packages 尚不存在,所以我创建它,然后再试一次,得到这个:

测试失败: /usr/local/python//lib/python2.5/site-packages 不支持 .pth 文件错误:坏 安装目录或PYTHONPATH

您正在尝试安装 打包到不在的目录 PYTHONPATH 而 Python 没有 从中读取“.pth”文件。这 您指定的安装目录 (通过 --install-dir、--prefix 或 distutils 默认设置)是:

/usr/local/python//lib/python2.5/site-packages

和你的 PYTHONPATH 环境 变量当前包含:

<前><代码>''

好吧,很公平——我没有做任何事情来设置路径。所以我在 ~/.bash_profile 中添加了一行快速内容:

PYTHONPATH="$PYTHONPATH:/usr/local/python/lib/python2.5"

source 它,然后重试。

同样的错误消息。

考虑到 PYTHONPATH 已明确设置,这有点奇怪;我可以echo $PYTHONPATH并返回:/usr/local/python/lib/python2.5。我决定从内部检查包含路径是什么样的:

import sys
print "\n".join(sys.path)

它会产生:

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5 /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload /Library/Python/2.5/site-packages /System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC

其中 /usr/local/python/yadda/yadda 明显缺失。

不知道我应该在这里做什么。如何让 python 将此位置识别为包含路径?

更新

正如 Sven Marnach 所建议的,我忽略了导出 PYTHONPATH。我已经纠正了这个问题,现在当我从 Python 中打印出 sys.path 时,就会看到它出现了。但是,我仍然收到上面提到的 TEST FAILED 错误消息,只是使用了新的 PYTHONPATH 环境变量。

因此,我尝试将其从 /usr/local/python/lib/python2.5 更改为 /usr/local/python/lib/python2.5/site-packages 、导出并再次运行相同的 easy_install 命令。这会导致一个全新的结果,起初看起来像成功(但事实并非如此):

Creating /usr/local/python/lib/python2.5/site-packages/site.py
Searching for pil
Reading http://pypi.python.org/simple/pil/
Reading http://www.pythonware.com/products/pil
Reading http://effbot.org/zone/pil-changes-115.htm
Reading http://effbot.org/downloads/#Imaging
Best match: PIL 1.1.7
Downloading http://effbot.org/media/downloads/PIL-1.1.7.tar.gz
Processing PIL-1.1.7.tar.gz
Running PIL-1.1.7/setup.py -q bdist_egg --dist-dir /var/folders/XW/XWpClVq7EpSB37BV3zTo+++++TI/-Tmp-/easy_install-krj9oR/PIL-1.1.7/egg-dist-tmp--Pyauy
--- using frameworks at /System/Library/Frameworks
[snipped: compiler warnings]
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      darwin 2.5.1 (r251:54863, Sep  1 2010, 22:03:14)
              [GCC 4.0.1 (Apple Inc. build 5465)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.
zip_safe flag not set; analyzing archive contents...
Image: module references __file__
No eggs found in /var/folders/XW/XWpClVq7EpSB37BV3zTo+++++TI/-Tmp-/easy_install-krj9oR/PIL-1.1.7/egg-dist-tmp--Pyauy (setup script problem?)

同样,这看起来不错,但是当我去运行我的脚本时:

回溯(最近一次调用最后一次):
文件“checkerboard.py”,第 1 行,位于 导入图像、ImageDraw 导入错误:没有名为 Image 的模块

当我使用 find . 检查 /usr/local/python/ 下现在的内容时,我得到:

./lib ./lib/python2.5 ./lib/python2.5/site-packages ./lib/python2.5/site-packages/site.py ./lib/python2.5/site-packages/site.pyc

所以...没有任何模块外观(我假设 site.py 和 site.pyc 是元数据或帮助程序脚本)。安装到哪里去了?

我注意到这一点:

要检查构建,请运行 selftest.py 脚本。

但实在不知道那是什么。

我还注意到“没有找到鸡蛋”的消息。这些提示是其中之一吗?

In the process of trying to write a Python script that uses PIL today, I discovered I don't seem have it on my local machine (OS X 10.5.8, default 2.5 Python install).

So I run:

easy_install --prefix=/usr/local/python/ pil

and it complains a little about /usr/local/python/lib/python2.5/site-packages not yet existing, so I create it, and try again, and get this:

TEST FAILED:
/usr/local/python//lib/python2.5/site-packages
does NOT support .pth files error: bad
install directory or PYTHONPATH

You are attempting to install a
package to a directory that is not on
PYTHONPATH and which Python does not
read ".pth" files from. The
installation directory you specified
(via --install-dir, --prefix, or the
distutils default setting) was:

/usr/local/python//lib/python2.5/site-packages

and your PYTHONPATH environment
variable currently contains:

''

OK, fair enough -- I hadn't done anything to set the path. So I add a quick line to ~/.bash_profile:

PYTHONPATH="$PYTHONPATH:/usr/local/python/lib/python2.5"

and source it, and try again.

Same error message.

This is kindof curious, given that PYTHONPATH is clearly set; I can echo $PYTHONPATH and get back :/usr/local/python/lib/python2.5. I decided to check out what the include path looked like from inside:

import sys
print "\n".join(sys.path)

which yields:

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC

from which /usr/local/python/yadda/yadda is notably missing.

Not sure what I'm supposed to do here. How do I get python to recognize this location as an include path?

UPDATE

As Sven Marnach suggested, I was neglecting to export PYTHONPATH. I've corrected that problem, and now see it show up when I print out sys.path from within Python. However, I still got the TEST FAILED error message I mentioned above, just with my new PYTHONPATH environment variable.

So, I tried changing it from /usr/local/python/lib/python2.5 to /usr/local/python/lib/python2.5/site-packages, exporting, and running the same easy_install command again. This leads to an all new result that at first looked like success (but isn't):

Creating /usr/local/python/lib/python2.5/site-packages/site.py
Searching for pil
Reading http://pypi.python.org/simple/pil/
Reading http://www.pythonware.com/products/pil
Reading http://effbot.org/zone/pil-changes-115.htm
Reading http://effbot.org/downloads/#Imaging
Best match: PIL 1.1.7
Downloading http://effbot.org/media/downloads/PIL-1.1.7.tar.gz
Processing PIL-1.1.7.tar.gz
Running PIL-1.1.7/setup.py -q bdist_egg --dist-dir /var/folders/XW/XWpClVq7EpSB37BV3zTo+++++TI/-Tmp-/easy_install-krj9oR/PIL-1.1.7/egg-dist-tmp--Pyauy
--- using frameworks at /System/Library/Frameworks
[snipped: compiler warnings]
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      darwin 2.5.1 (r251:54863, Sep  1 2010, 22:03:14)
              [GCC 4.0.1 (Apple Inc. build 5465)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.
zip_safe flag not set; analyzing archive contents...
Image: module references __file__
No eggs found in /var/folders/XW/XWpClVq7EpSB37BV3zTo+++++TI/-Tmp-/easy_install-krj9oR/PIL-1.1.7/egg-dist-tmp--Pyauy (setup script problem?)

Again, this looks good, but when I go to run my script:

Traceback (most recent call last):
File "checkerboard.py", line 1, in

import Image, ImageDraw ImportError: No module named Image

When I check what's now under /usr/local/python/ using find ., I get:

./lib ./lib/python2.5
./lib/python2.5/site-packages
./lib/python2.5/site-packages/site.py
./lib/python2.5/site-packages/site.pyc

So... nothing module-looking (I'm assuming site.py and site.pyc are metadata or helper scripts). Where did the install go?

I note this:

To check the build, run the
selftest.py script.

But don't really know what that is.

And I also noticed the "No eggs found" message. Are either of these hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-10-13 17:28:45

您在 OS X 中使用 Apple 提供的 Python 2.5;它是一个框架构建,默认情况下使用 /Library/Python/2.5/site-packages 作为安装包的位置,而不是 /usr/local。通常,您不需要在 OS X 框架构建中指定 --prefix。另请注意,Apple 随 OS X 10.5 提供的 setuptools (easy_install) 也相当旧,Python 本身的版本也是如此。

也就是说,在 OS X 尤其是 OS X 10.5 上完整且正确地安装 PIL 并不是特别简单。在档案或其他地方搜索提示和/或二进制包。特别是如果您计划使用 MySQL 或 Django 等其他模块,我的建议是使用像 MacPorts

You are using the Apple-supplied Python 2.5 in OS X; it's a framework build and, by default, uses /Library/Python/2.5/site-packages as the location for installed packages, not /usr/local. Normally you shouldn't need to specify --prefix with an OS X framework build. Also beware that the setuptools (easy_install) supplied by Apple with OS X 10.5 is also rather old as is the version of Python itself.

That said, installing PIL completely and correctly on OS X especially OS X 10.5 is not particularly simple. Search the archives or elsewhere for tips and/or binary packages. Particularly if you are planning to use other modules like MySQL or Django, my recommendation is to install everything (Python and PIL) using a package manager like MacPorts.

白衬杉格子梦 2024-10-13 17:28:45

为什么在 easy_install 调用中指定 --prefix ?您是否尝试过:

sudo easy_install pil

如果您只是尝试将 PIL 安装到默认位置,我认为 easy_install 可以找出正确的路径。 (显然,/usr/local/python 不是吗...)

编辑:有人否决了这个答案,也许是因为它太简洁了
。我想这就是我尝试通过手机发布答案时得到的结果。但它的要点是完全有效的,恕我直言:如果您使用 --prefix 来使用 easy_install 指定自定义安装位置,那么您就在“这样做”错误的'。可能可能可以实现此功能,但 easy_install 文档中有一个关于 自定义安装位置,甚至没有提到这种可能性,除了对 虚拟 python 选项。如果您愿意,我建议您按照 OS X 说明想要安装到 Mac 上的自定义位置,--prefix 似乎不是适合该工作的工具。

Why did you specify --prefix in your easy_install invocation? Did you try just:

sudo easy_install pil

If you're only trying to install PIL to the default location, I would think easy_install could work out the correct path. (Clearly, /usr/local/python isn't it...)

EDIT: Someone down-voted this answer, maybe because it was too terse
. That's what I get for trying to post an answer from my cell phone, I guess. But the gist of it is perfectly valid, IMHO: if you're using --prefix to specify a custom install location with easy_install, you're kind of 'doing it wrong'. It might be possible to make this work, but the easy_install documentation has a section on custom installation locations that doesn't even mention this as a possibility, except as a small tweak to the virtual python option. I'd suggest following the OS X instructions if you want to install to a custom location on a Mac, --prefix just does not seem like the right tool for the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文