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

2024-06-06 17:44:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以我跑:

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

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

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:

''

好吧,很公平——我没有做任何事情来确定道路。所以我在~/.bash_配置文件中添加了一个快速行:

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

然后source再试一次。

相同的错误消息。

这有点奇怪,因为PYTHONPATH已经设置好了;我可以echo $PYTHONPATH然后返回:/usr/local/python/lib/python2.5。我决定从内部查看include路径的外观:

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将此位置识别为包含路径?

更新

正如斯文·马纳奇建议的那样,我忽略了Python的出口。我已经纠正了这个问题,现在看到它在我从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?)

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

Traceback (most recent call last):
File "checkerboard.py", line 1, in import Image, ImageDraw ImportError: No module named 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是元数据或帮助脚本)。安装到哪里去了?

我注意到:

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

但不知道那是什么。

我也注意到了“找不到鸡蛋”的信息。这是暗示吗?


Tags: installthepypillibpackagesusrlocal
2条回答

为什么在easy_install调用中指定--prefix?你有没有试过:

sudo easy_install pil

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

编辑:有人否决了这个答案,可能是因为它太简洁了 . 我想,这就是我试着用手机回复的原因。但它的要点是完全正确的,IMHO:如果您使用--prefix来指定一个带有easy_install的自定义安装位置,您就有点“做错了”。这可能是可能的来实现的,但是easy_install文档中有一个关于custom installation locations的部分甚至没有提到这是一种可能性,只是对virtual python选项做了一个小的调整。如果你想安装到Mac上的一个自定义位置,我建议遵循OS X instructions命令,--prefix似乎不是适合这个任务的工具。

您在OSX中使用的是Apple提供的Python2.5;它是一个框架构建,默认情况下使用/Library/Python/2.5/site-packages作为已安装软件包的位置,而不是/usr/local。通常,您不需要使用OSX框架构建指定--prefix。还要注意的是,苹果公司随OSX10.5提供的setuptoolseasy_install)和Python本身的版本一样,也相当陈旧。

也就是说,在OSX上完全正确地安装PIL,特别是OSX10.5并不是特别简单。在档案或其他地方搜索提示和/或二进制软件包。特别是如果您计划使用其他模块,如MySQL或Django,我的建议是使用像MacPorts这样的包管理器安装所有组件(Python和PIL)。

相关问题 更多 >