在Mac OS X上控制Python-设置环境和库 [英] Getting Python under control on Mac OS X - setting up environment and libraries
问题描述
在Ubuntu Linux上开始使用Python之后,现在我已经在Mac(目前是Mac OS X 10.6)上进行了大部分持续工作.不幸的是,我忽略了适当地注意如何在其中安装Python并最终导致:
After starting out with Python on Ubuntu Linux, I've now for a good while been doing most of my sustained work on the Mac, currently Mac OS X 10.6. Unfortunately I've neglected to give proper attention to how Python is installed there and ended up with:
-
/usr/bin
中的Python 2.6.1(Mac默认版本?)(也是2.5.4,我不确定它是怎么到达的)
通过 - Python 2.6.5.这是我的默认设置
- 我使用
pip
来安装库,这些库最终会出现在某些地方(如/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
-SRSLY?) - 否则,主要是TextMate和Git用于版本控制. Django和Google App Engine等...
/opt/local/bin/
中的 MacPorts 安装的- Python 2.6.1 (Mac default version?) in
/usr/bin
(also, 2.5.4, which I'm not sure how it got there) - Python 2.6.5 installed via MacPorts in
/opt/local/bin/
. This is my default - I use
pip
to install libraries, which end up in some ungodly place (something like/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
- SRSLY?) - Otherwise, mostly TextMate, and Git for version control. Django and Google App Engine, etc...
现在,我准备设置2.7和3.2,但我对事情的偶然状态感到不满意.那么,您最喜欢的组织代码和库的方法是什么,又如何使Mac努力提交呢?
Now I'm preparing to set up 2.7 and 3.2, and I am unhappy with the haphazard state of things. So what are your favourite approaches to organising the code and libraries, and how have you wrestled the Mac into submission?
我想继续使用pip
,但是想要更多地控制或至少了解要安装哪个版本的库并可以从何处访问:我在安装py.test
时遇到了问题通过pip
只能从过时的2.6.1 Python加载它,而不是从我当前的2.6.5加载. MacPorts具有python_select
,但并不太有用:
I'd like to continue using pip
, but would like to have more control or at least understanding of what libraries for which version are getting installed and made accessible from where: I've had problems with installing py.test
via pip
and only being able to load it from the obsolete 2.6.1 Python, not my current 2.6.5 one. MacPorts has python_select
, but it's not overly helpful:
reason: chris$ python_select -l
Available versions:
current none python26 python26-apple
我问过的大多数Python用户不使用MacPorts,我不太喜欢,但使用的是python.org
中的Mac Python.我也听说过建议系统地使用virtualenv
的建议,那么,一个好的实用介绍的链接是什么?
Most Python people I've asked don't use MacPorts, which I don't much like, but the stock Mac Python from python.org
. I've also heard the recommendation to use virtualenv
systematically, so what is the link to a good practical introduction?
推荐答案
virutalenv
是一个很好的工具,对于在大多数平台上管理多个Python实例非常有用.但是,在Mac OS X上,事情变得更加复杂,因为使用框架构建使遇到相同主版本Python的多个实例变得很普遍.我建议您先研究并熟悉如何在Mac OS X上管理框架安装,然后再研究virtualenv
.
virutalenv
is a great tool and is very useful for managing multiple Python instances on most platforms. On Mac OS X, though, things are more complicated because the use of framework builds makes it common to encounter multiple instances of even the same major version of Python. I suggest you first understand and be comfortable with how to manage framework installs on Mac OS X before delving into virtualenv
.
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
-SRSLY?"
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
- SRSLY?"
是的,这是Python Mac OS X框架版本中的默认位置.在Mac OS X上,大多数Python发行商都使用框架构建,每个发行人通常使用不同的根目录,因为这样可以更轻松地支持多个版本并为特定的Python保留所有站点包的安装.实例一起放在一个地方.
Yes, that's the default location within a Python Mac OS X framework build. Most distributors of Python on Mac OS X use a framework build, each generally using a different root, because it makes it simpler to support multiple versions and to keep all the site packages installed for a particular Python instance together in one place.
对于Mac OS X 10.6,Apple提供了Python 2.6(植根于/System/Library/Frameworks/Python.framework
并符号链接到/usr/bin/python2.6
)和Python 2.5(与Mac OS X 10.5兼容-即/Library/Frameworks/Python.framework
. MacPorts使用/opt/local/Library/Frameworks/Python.framework
.
For Mac OS X 10.6, Apple supplies a Python 2.6 (rooted at /System/Library/Frameworks/Python.framework
and symlinked to /usr/bin/python2.6
) as well as a Python 2.5 (for compatibility with Mac OS X 10.5 - that's the /usr/bin/python2.5
you see). The python.org Mac OS X installers install to /Library/Frameworks/Python.framework
. MacPorts uses /opt/local/Library/Frameworks/Python.framework
.
框架包括bin
目录(在框架中的../Versions/x.x/bin
);这通常是安装程序包脚本的默认位置.也是指向解释器二进制文件的符号链接所在的地方.
The frameworks include a bin
directory (at ../Versions/x.x/bin
in the framework); this is normally the default location where package scripts will be installed. It's also where symlinks to the interpreter binaries are placed.
这里是一个安装了多个Python版本的python.org样式框架:
Here's a look at a python.org-style framework with multiple versions of Python installed:
$ cd /Library/Frameworks/Python.framework/Versions/
$ ls -l
total 8
drwxrwxr-x 8 root admin 340 Dec 23 2008 2.5/
drwxrwxr-x 8 root admin 340 Aug 10 21:42 2.6/
drwxrwxr-x 8 root admin 340 Jul 3 12:24 2.7/
drwxrwxr-x 7 root admin 306 Mar 23 17:42 3.1/
drwxrwxr-x 7 root admin 306 Aug 10 22:14 3.2/
lrwxr-xr-x 1 root admin 3 Aug 10 21:47 Current@ -> 2.6
$ cd 2.6
$ ls -l
total 7776
lrwxr-xr-x 1 root admin 17 Aug 10 21:47 Headers@ -> include/python2.6
drwxrwxr-x 3 root admin 102 Aug 10 21:42 Mac/
-r-xrwxr-x 1 root admin 3973552 Aug 10 21:41 Python*
drwxrwxr-x 4 root admin 170 Aug 10 21:48 Resources/
drwxrwxr-x 2 root admin 578 Aug 12 15:19 bin/
drwxrwxr-x 3 root admin 102 Aug 10 21:42 include/
drwxrwxr-x 3 root admin 646 Aug 10 21:43 lib/
drwxrwxr-x 3 root admin 102 Aug 10 21:41 share/
$ ls -l bin
total 264
-rwxrwxr-x 1 root admin 150 Aug 10 21:41 2to3*
-rwxr-xr-x 1 nad admin 360 Aug 12 15:19 easy_install*
-rwxr-xr-x 1 nad admin 368 Aug 12 15:19 easy_install-2.6*
lrwxr-xr-x 1 root admin 7 Aug 10 21:47 idle@ -> idle2.6
-rwxrwxr-x 1 root admin 138 Aug 10 21:41 idle2.6*
lrwxr-xr-x 1 root admin 8 Aug 10 21:47 pydoc@ -> pydoc2.6
-rwxrwxr-x 1 root admin 123 Aug 10 21:41 pydoc2.6*
lrwxr-xr-x 1 root admin 9 Aug 10 21:47 python@ -> python2.6
lrwxr-xr-x 1 root admin 16 Aug 10 21:47 python-config@ -> python2.6-config
-rwxrwxr-x 1 root admin 29584 Aug 10 21:43 python2.6*
-rwxrwxr-x 1 root admin 1463 Aug 10 21:42 python2.6-config*
lrwxr-xr-x 1 root admin 10 Aug 10 21:47 pythonw@ -> pythonw2.6
-rwxrwxr-x 1 root admin 29584 Aug 10 21:43 pythonw2.6*
lrwxr-xr-x 1 root admin 11 Aug 10 21:47 smtpd.py@ -> smtpd2.6.py
-rwxrwxr-x 1 root admin 18272 Aug 10 21:41 smtpd2.6.py*
这是MacPorts bin
,其中包括许多其他站点软件包:
Here's a MacPorts bin
including a number of additional site packages:
$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.6
$ ls -l bin
total 952
-rwxr-xr-x 2 root wheel 160 Jul 15 01:56 2to3*
-rwxr-xr-x 2 root wheel 1541 Jun 8 19:59 apirst2html.py*
-rwxr-xr-x 2 root wheel 363 Oct 5 2009 bdist_mpkg*
[...]
-rwxr-xr-x 2 root wheel 211 Jun 2 12:45 django-admin.py*
-rwxr-xr-x 2 root wheel 410 Jul 15 17:30 easy_install*
-rwxr-xr-x 2 root wheel 418 Jul 15 17:30 easy_install-2.6*
[...]
-rwxr-xr-x 2 root wheel 368 Jun 24 02:00 pip*
-rwxr-xr-x 1 root wheel 342 Sep 18 2009 pkginfo*
[...]
lrwxr-xr-x 1 root wheel 9 Jul 15 01:58 python@ -> python2.6
lrwxr-xr-x 1 root wheel 16 Jul 15 01:58 python-config@ -> python2.6-config
-rwxr-xr-x 2 root wheel 24932 Jul 15 01:57 python2.6*
-rwxr-xr-x 2 root wheel 1473 Jul 15 01:57 python2.6-config*
[...]
如果使用python.org安装程序或MacPorts安装2.7或3.1 Python,它们全部植根于/Library/Frameworks
或/opt/local/Library/Frameworks
,并且可以与其他版本愉快地共存.要成功使用框架安装,您需要管理shell PATH
.特别是,您应确保要使用的Python的框架bin
目录位于PATH
上.因此,对于MacPorts,在~/.bash_profile
中是这样的:
If you install 2.7 or 3.1 Pythons using python.org installers or MacPorts, they are all rooted under /Library/Frameworks
or /opt/local/Library/Frameworks
and will happily co-exist with other versions. To successfully use framework installs, you need to manage your shell PATH
. In particular, you should ensure that the framework bin
directory of the Python(s) you want to use are on your PATH
. So for MacPorts, something like this in ~/.bash_profile
:
export PATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin:/opt/local/bin:$PATH
或python.org安装程序:
or for the python.org installers:
export PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:$PATH
此外,通常,您需要为使用的每个Python实例安装一个单独版本的喜爱的Python软件包管理插件(例如pip
或Distribute
或setuptools
中的easy_install
命令).每个都安装到默认脚本目录(即框架bin
目录)中. MacPorts在安装其软件包的版本(sudo port install py26-pip
或sudo port install py31-distribute
)时会自动进行处理.请注意,Apple在/usr/bin
中为其Python提供了一组easy_install
链接,并且它们将软件包安装到/Library/Python
.您需要确保使用适用于您的Python实例的easy_install
或pip
.确保实例的框架bin
目录在PATH
中首先位于该位置.
In addition, generally you need to install a separate version of your favorite Python package management add-ons (like pip
or the easy_install
command from Distribute
or setuptools
) for each Python instance you use. Each one is installed into the default script directory (i.e. the framework bin
directory). MacPorts takes care of that automatically when you install its version of the package (sudo port install py26-pip
or sudo port install py31-distribute
). Note that Apple supplies a set of easy_install
links in /usr/bin
for its Pythons and they install packages to /Library/Python
. You need to make sure you use the easy_install
or pip
appropriate for your instance of Python. Making sure the instance's framework bin
directory is first in the PATH
ensures that.
请注意,如果您使用MacPorts,则可以使用其python_select
命令来管理/opt/local/bin/python
指向哪个Python实例.然后,如果/opt/local/bin
出现在PATH列表中的位置早于其他带有python
的目录,它将决定哪个是默认的python
.但是您确实需要通过添加框架bin
目录来管理PATH,因此python_select
通常并没有那么有用.
Note, if you use MacPorts you can use its python_select
command to manage which Python instance is pointed to by /opt/local/bin/python
. Then if /opt/local/bin
appears earlier in your PATH list than other directories with a python
it will govern which is your default python
. But you really need to manage the PATH by adding the framework bin
directory so python_select
is generally not all that useful.
使用MacPorts(或类似的基础广泛的发行商)的另一个积极方面是,它提供了许多流行的Python程序包,包括,这些临时程序处理了构建和链接到第三方C库时偶尔出现的混乱细节, Mac OS X随附.例如,由于MySQL客户端库和libjpeg
,MySQLdb
和PIL
软件包在Mac OS X上经常成为绊脚石,但是MacPorts使得正确处理一切变得容易得多.但是您可以选择直接使用MacPorts(sudo port py26-pil
)或通过pip
(sudo pip install pil
)或easy_install
直接安装,也可以下载源代码并自己进行安装(sudo python setup.py ...
).而且由于所有这些方法都是使用Python的Distutils
来完成肮脏的工作,因此,对于给定的Python实例,所有这些方法通常都会最终将软件包的文件安装在同一位置.
Another positive aspect of using MacPorts (or similar broad-based distributor) is that it makes available many popular Python packages including handling the occasional messy details of building and linking to 3rd-party C libraries not shipped with Mac OS X. For example, the MySQLdb
and PIL
packages are frequent stumbling blocks on Mac OS X because of the MySQL client libraries and libjpeg
, but MacPorts makes it much easier to get everything right. But you do have the choice to install things directly using MacPorts (sudo port py26-pil
) or via pip
(sudo pip install pil
) or easy_install
, or you can download the source, and do it yourself (sudo python setup.py ...
). And because all of them under the covers use Python's Distutils
to do the dirty work, all of these methods will generally end up installing a package's files in the same place for a given Python instance.
这篇关于在Mac OS X上控制Python-设置环境和库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!