macos 如何设置本地python库目录/PYTHONPATH?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4393830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set up a local python library directory / PYTHONPATH?
提问by Weston C
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).
今天在尝试编写一个使用 PIL 的 Python 脚本的过程中,我发现我的本地机器上似乎没有它(OS X 10.5.8,默认 2.5 Python 安装)。
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:
它有点抱怨 /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:
''
测试失败:/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 环境变量当前包含:
''
OK, fair enough -- I hadn't done anything to set the path. So I add a quick line to ~/.bash_profile:
好吧,很公平——我没有做任何事情来设置路径。所以我在 ~/.bash_profile 中添加了一个快速行:
PYTHONPATH="$PYTHONPATH:/usr/local/python/lib/python2.5"
PYTHONPATH="$PYTHONPATH:/usr/local/python/lib/python2.5"
and source
it, and try again.
和source
它,然后再试一次。
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:
鉴于 PYTHONPATH 已明确设置,这有点奇怪;我可以echo $PYTHONPATH
回来:/usr/local/python/lib/python2.5
。我决定从内部检查包含路径的样子:
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
/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.
其中/usr/local/python/yadda/yadda
明显缺失。
Not sure what I'm supposed to do here. How do I get python to recognize this location as an include path?
不知道我应该在这里做什么。如何让python将此位置识别为包含路径?
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.
正如 Sven Marnach 所建议的那样,我忽略了导出 PYTHONPATH。我已经更正了这个问题,现在当我sys.path
从 Python 中打印出来时,它会出现。但是,我仍然收到了TEST FAILED
上面提到的错误消息,只是使用了我的新 PYTHONPATH 环境变量。
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 lookedlike success (but isn't):
因此,我尝试将其从 更改/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?)
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
回溯(最近一次调用最后一次):
文件“checkerboard.py”,第 1 行,在导入图像中,ImageDraw 导入错误:没有名为 Image 的模块
When I check what's now under /usr/local/python/
using find .
, I get:
当我检查现在正在/usr/local/python/
使用的内容时find .
,我得到:
./lib ./lib/python2.5 ./lib/python2.5/site-packages ./lib/python2.5/site-packages/site.py ./lib/python2.5/site-packages/site.pyc
./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?
所以......没有任何模块(我假设 site.py 和 site.pyc 是元数据或帮助脚本)。安装到哪里去了?
I note this:
我注意到这一点:
To check the build, run the selftest.py script.
要检查构建,请运行 selftest.py 脚本。
But don't really know what that is.
但真的不知道那是什么。
And I also noticed the "No eggs found" message. Are either of these hints?
而且我还注意到“未找到鸡蛋”消息。是这些提示中的任何一个吗?
采纳答案by Ned Deily
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.
您在 OS X 中使用 Apple 提供的 Python 2.5;它是一个框架构建,默认情况下/Library/Python/2.5/site-packages
用作安装包的位置,而不是/usr/local
. 通常,您不需要指定--prefix
OS X 框架构建。另请注意,Apple 随 OS X 10.5 提供的setuptools
( easy_install
) 与 Python 本身的版本一样也很旧。
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.
也就是说,PIL
在 OS X 尤其是 OS X 10.5 上完全正确地安装并不是特别简单。在档案或其他地方搜索提示和/或二进制包。特别是如果您打算使用其他模块(如 MySQL 或 Django),我的建议是使用MacPorts等包管理器安装所有内容(Python 和 PIL)。
回答by evadeflow
Why did you specify --prefix
in your easy_install
invocation? Did you try just:
你为什么--prefix
在easy_install
调用中指定?您是否尝试过:
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...)
如果您只是尝试将 PIL 安装到默认位置,我认为easy_install
可以找出正确的路径。(很明显,/usr/local/python
是不是……)
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 possibleto make this work, but the easy_install
documentation has a section on custom installation locationsthat doesn't even mention this as a possibility, except as a small tweak to the virtual pythonoption. I'd suggest following the OS X instructionsif you want to install to a custom location on a Mac, --prefix
just does not seem like the right tool for the job.
编辑:有人对这个答案投了反对票,也许是因为它太简洁了。我想这就是我试图从手机上发布答案所得到的结果。但它的要点是完全有效的,恕我直言:如果您使用--prefix
指定自定义安装位置easy_install
,那么您有点“做错了”。这可能是可能的,使这项工作,但easy_install
文件对一款自定义安装位置,它甚至没有提到这种可能性,但作为一个小调整到虚拟蟒蛇选项。如果您想安装到 Mac 上的自定义位置,我建议按照OS X 说明进行操作,--prefix
但这似乎不是适合该工作的工具。