简体   繁体   English

在Mac OS X上控制Python - 设置环境和库

[英]Getting Python under control on Mac OS X - setting up environment and libraries

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. 在Ubuntu Linux上开始使用Python之后,我现在已经很好地完成了我在Mac上的大部分持续工作,目前是Mac OS X 10.6。 Unfortunately I've neglected to give proper attention to how Python is installed there and ended up with: 不幸的是,我忽略了如何适当地关注Python的安装方式,最终得到:

  • Python 2.6.1 (Mac default version?) in /usr/bin (also, 2.5.4, which I'm not sure how it got there) /usr/bin Python 2.6.1(Mac默认版本?)(另外,2.5.4,我不确定它是如何到达的)
  • Python 2.6.5 installed via MacPorts in /opt/local/bin/ . 通过/opt/local/bin/ MacPorts安装Python 2.6.5。 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?) 我使用pip来安装库,最终会进入一些/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/地方(例如/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/ - SRSLY?)
  • Otherwise, mostly TextMate, and Git for version control. 否则,主要是TextMate和Git进行版本控制。 Django and Google App Engine, etc... Django和Google App Engine等......

Now I'm preparing to set up 2.7 and 3.2, and I am unhappy with the haphazard state of things. 现在我准备设置2.7和3.2,我对随意的状态不满意。 So what are your favourite approaches to organising the code and libraries, and how have you wrestled the Mac into submission? 那么你最喜欢的组织代码和库的方法是什么,以及你如何将Mac摔跤提交?

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. 我想继续使用pip ,但是想要有更多的控制权或者至少要了解哪些库安装了哪些版本并且可以从哪里访问:我遇到了通过pip安装py.test遇到的问题能够从过时的2.6.1 Python加载它,而不是我当前的2.6.5。 MacPorts has python_select , but it's not overly helpful: MacPorts有python_select ,但它没有太大的帮助:

reason: chris$ python_select -l
Available versions:
current none python26 python26-apple

Most Python people I've asked don't use MacPorts, which I don't much like, but the stock Mac Python from python.org . 我问过的大多数Python人都不使用我不太喜欢的MacPorts,而是使用来自python.org Mac Python。 I've also heard the recommendation to use virtualenv systematically, so what is the link to a good practical introduction? 我也听说过系统地使用virtualenv的建议,那么实用介绍的链接是什么?

virutalenv is a great tool and is very useful for managing multiple Python instances on most platforms. virutalenv是一个很棒的工具,对于在大多数平台上管理多个Python实例非常有用。 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. 然而,在Mac OS X上,事情变得更复杂,因为使用框架构建使得遇到甚至相同主要版本的Python的多个实例也很常见。 I suggest you first understand and be comfortable with how to manage framework installs on Mac OS X before delving into virtualenv . 我建议您首先了解并熟悉如何在深入研究virtualenv之前在Mac OS X上管理框架安装。

" /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?”

Yes, that's the default location within a Python Mac OS X framework build. 是的,这是Python Mac OS X框架构建中的默认位置。 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上的大多数Python分销商都使用框架构建 ,每个框架构建通常使用不同的根,因为它可以更简单地支持多个版本并将特定Python实例的所有网站包保存在一个地方。

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). 对于Mac OS X 10.6,Apple提供了Python 2.6(根据/System/Library/Frameworks/Python.framework/System/Library/Frameworks/Python.framework链接到/usr/bin/python2.6 )以及Python 2.5(与Mac OS X 10.5兼容) - 那就是你看到的/usr/bin/python2.5 )。 The python.org Mac OS X installers install to /Library/Frameworks/Python.framework . python.org Mac OS X安装程序安装到/Library/Frameworks/Python.framework MacPorts uses /opt/local/Library/Frameworks/Python.framework . MacPorts使用/opt/local/Library/Frameworks/Python.framework

The frameworks include a bin directory (at ../Versions/xx/bin in the framework); 框架包括一个bin目录(在框架中的../Versions/xx/bin ); this is normally the default location where package scripts will be installed. 这通常是安装包脚本的默认位置。 It's also where symlinks to the interpreter binaries are placed. 它也是放置解释器二进制文件的符号链接的地方。

Here's a look at a python.org-style framework with multiple versions of Python installed: 下面是一个安装了多个Python版本的python.org样式框架:

$ 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*

Here's a MacPorts bin including a number of additional site packages: 这是一个MacPorts bin包括许多其他站点包:

$ 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*
[...]

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. 如果您使用python.org安装程序或MacPorts安装2.7或3.1 Pythons,它们都以/Library/Frameworks/opt/local/Library/Frameworks并且很乐意与其他版本共存。 To successfully use framework installs, you need to manage your shell PATH . 要成功使用框架安装,您需要管理shell PATH In particular, you should ensure that the framework bin directory of the Python(s) you want to use are on your PATH . 特别是,您应该确保要使用的Python的框架bin目录位于PATH So for MacPorts, something like this in ~/.bash_profile : 所以对于MacPorts,在~/.bash_profile

export PATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin:/opt/local/bin:$PATH

or for the python.org installers: 或者对于python.org安装程序:

export PATH="/Library/Frameworks/Python.framework/Versions/2.6/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. 此外,通常您需要为您使用的每个Python实例安装一个单独版本的您喜欢的Python包管理加载项(如pip或来自Distributesetuptoolseasy_install命令)。 Each one is installed into the default script directory (ie the framework bin directory). 每个都安装在默认的脚本目录(即框架bin目录)中。 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 ). 当你安装它的软件包版本时,MacPorts会自动处理这个问题( sudo port install py26-pipsudo 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 . 请注意,Apple在/usr/bin中为其Pythons提供了一组easy_install链接,并将它们安装到/Library/Python You need to make sure you use the easy_install or pip appropriate for your instance of Python. 您需要确保使用适合您的Python实例的easy_installpip Making sure the instance's framework bin directory is first in the PATH ensures that. 确保实例的框架bin目录在PATH是第一个确保。

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 . 请注意,如果您使用MacPorts,则可以使用其python_select命令来管理/opt/local/bin/python指向的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 . 然后,如果/opt/local/bin出现在PATH列表中的早于其他带有python目录,它将控制哪个是你的默认python But you really need to manage the PATH by adding the framework bin directory so python_select is generally not all that useful. 但是你真的需要通过添加框架bin目录来管理PATH,因此python_select通常不是那么有用。

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. 使用MacPorts(或类似的基于广泛的分发服务器)的另一个积极方面是它提供了许多流行的Python软件包, 包括处理构建和链接到Mac OS X未附带的第三方C库的偶然混乱细节。例如,由于MySQL客户端库和libjpegMySQLdbPIL软件包在Mac OS X上经常出现绊脚石,但MacPorts使得一切正常变得容易。 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 ... ). 但您可以选择使用MacPorts( sudo port py26-pil )或通过pipsudo pip install pil )或easy_install sudo pip install pil ,或者您可以下载源代码,并自己动手( 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. 并且因为所有这些都使用Python的Distutils来完成脏工作,所有这些方法通常最终会在给定Python实例的同一位置安装包的文件。

This is an old question, but I have struggled in the past with upgrading Python and virtual environments. 这是一个老问题,但我过去一直在努力升级Python和虚拟环境。 I have figured out how to get this to work for the stuff I do. 我已经想出如何让这个对我所做的事情起作用。 Hopefully this will help someone. 希望这会对某人有所帮助。

Setup: 设定:
Mac running 10.14 Mac运行10.14
Python 3.7 Python 3.7
Various packages: Pandas, IPython, Paramiko, etc. 各种套餐:熊猫,IPython,Paramiko等

Python: 蟒蛇:
Just use the installer they provide at Python.org . 只需使用他们在Python.org上提供的安装程序即可。 Click on the "Download" button for macOS, download the packages and click through the installer. 单击macOS的“下载”按钮,下载软件包并单击安装程序。 Done. 完成。 When it comes time to upgrade to the next Python 3 version, repeat the process. 当需要升级到下一个Python 3版本时,请重复此过程。
To get the Python3 interpreter on the command-line instead of the Python2 one, use python3 instead of python . 要在命令行而不是Python2上获取Python3解释器,请使用python3而不是python

Getting Rid of Older Python3 versions 摆脱旧的Python3版本
If you don't like the idea of having old Python versions hanging around, open the Applications folder and drag-and-drop the old Python3 version you don't want to the trash. 如果您不喜欢使用旧的Python版本,请打开Applications文件夹,然后将您不想要的旧Python3版本拖放到垃圾箱中。 DO NOT do this to the Python2 versions unless you know what you a re doing. 除非你知道自己在做什么,否则不要对Python2版本执行此操作。 Some of these came with the OS and will likely cause trouble if they disappear. 其中一些附带操作系统,如果它们消失可能会带来麻烦。

Installing Packages: . 安装包:
@Ned Deily's suggestion to use virtual environments is a good one if you want to make installing packages easier. @Ned Deily建议使用虚拟环境是一个很好的建议,如果你想让安装包更容易。 The virtual environment tool for Python2 is 'virtualenv'. Python2的虚拟环境工具是'virtualenv'。 That tool is called 'venv' in Python3. 该工具在Python3中称为“venv”。

Step 1: 第1步:
Create a directory to hold all of your virtual environments. 创建一个目录以容纳所有虚拟环境。 This isn't necessary, but this is how I keep track of mine. 这不是必要的,但这就是我跟踪我的方式。 I keep two separate dirs named 'virtenvs' and 'venvs' depending on whether I'm installing a package for Python2 or Python3. 我保留了两个名为'virtenvs'和'venvs'的独立目录,具体取决于我是否正在为Python2或Python3安装包。 For this example, we'll use Python3: 对于这个例子,我们将使用Python3:

cd ~
mkdir venvs
cd venvs

Now, I name my packages after my project or after the major package I've installed. 现在,我在项目之后或在我安装的主要软件包之后命名我的软件包。 For this example, let's just call it 'project1': 对于这个例子,我们只称它为'project1':
venv project1

Step 2: 第2步:
"Activate" the virtual environment using the following command. 使用以下命令“激活”虚拟环境。 You'll need to to this every time you start a new terminal or session. 每次开始新的终端或会话时,您都需要这样做。
source ~/venvs/project1/bin/activate

Pip changes a lot, so update it Pip变化很大,所以要更新它
pip install --upgrade pip

Step 3: 第3步:
Now you're ready to start installing packages into your new virtual environment: 现在您已准备好开始将软件包安装到新的虚拟环境中:
pip install pandas
etc. 等等

When you're done or you want to switch to a different virtual environment, just type: 完成后或想要切换到其他虚拟环境时,只需键入:
deactivate

What's Next: 下一步是什么:
Explore the web for more info on Python virtual environments. 浏览Web以获取有关Python虚拟环境的更多信息。 Using this approach, I've not run into a package that required MacPorts, Anaconda, etc. There are undoubtedly packages that do. 使用这种方法,我没有遇到需要MacPorts,Anaconda等的软件包。毫无疑问,软件包可以做到。 YMMV. 因人而异。 Hopefully this approach will work for you. 希望这种方法对您有用。

I just used 'pythonbrew', which I easy_installed with the stock system Python on 10.6.7. 我刚刚使用了'pythonbrew',我在10.6.7上使用了股票系统Python轻松安装了它。 I had to use the '--force' option to bypass the post-compile tests though, as I had failures with test_dl and test_readline (a quick google check did not show these to be a real issue). 我不得不使用'--force'选项来绕过后编译测试,因为我在test_dl和test_readline上失败了(快速google检查没有显示这些是真正的问题)。

$ sudo easy_install pythonbrew

Once installed, run this for more instructions: 安装后,运行此命令以获取更多说明:

$ pythonbrew_install

So I now have a nicely isolated python-2.7.1 installation without interfering with the system installation. 所以我现在有一个很好的隔离python-2.7.1安装,而不会干扰系统安装。

I would recommend using Homebrew, as do the Python Guide people: 我建议使用Homebrew,Python指南人员也是如此:

Here is a fantastic step by step guide to getting started: 这是一个非常棒的一步一步的入门指南:

http://docs.python-guide.org/en/latest/starting/install/osx/#install-osx http://docs.python-guide.org/en/latest/starting/install/osx/#install-osx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM