python多版本管理

mac不要轻易的安装新版本的python!
mac不要轻易的安装新版本的python!
mac不要轻易的安装新版本的python!

不同的版本用python的版本管理工具

一般重要的事情说三遍,因为用到一个python的新功能,所以需要安装。2.7.9以上的版本,但是mac上面是2.7.6的,如果真的想要更新,下载官方的pkg并且设置一下环境变量,我就是没有设置环境变量导致了一系列的问题。
配置方法找到一个博客

担心以后博客消失,摘录一下,作为参考

Installing / Updating Python on OS X

While Python comes pre-installed on OS X, Apple doesn’t do a good job on keeping the Python runtime environment up to date. Currently, on Mac OS X 10.7.4 “Lion”, entering python -V returns Python 2.7.1. Even worse, Mac OS X 10.6 “Snow Leopard” is still on a Python 2.6 release.

While the latest Python releases are always available on http://www.python.org, updating a Mac isn’t a simple, straight forward process.

Follow along and update your Mac to Python 2.7.3, or 3.3.0 or whatever the newest 2.x and 3.x release might be, when you read this. To update your Mac to something like Python 2.7.3, I assume that

  • your Mac-User account is setup as an “Administrator” account.
  • your Mac already has this folder: /System/Library/Frameworks/Python.framework/Versions/

To read about how to upgrade to Python 3.3, jump to the very bottom of this post.

1. Downloading and Installing the latest Python Release

Go to python.org and pick the most recent Python 2.x release: http://python.org/download.
I picked Python 2.7.3 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and later), an about 18 MB download.

Opening the DMG and executing the installer (Python.mpkg) will install the Python release into /Library/Frameworks/Python.framework, which is not next to the other, already installed Python versions and may lead to some nasty incompatibilities. Moreover, /usr/bin/python still executes python 2.7.1., i.e. some work still needs to be done to really update Python on your Mac.

2. Moving Python into the right place

If Python 2.7.x is already available on the Mac (e.g. on OS X Lion), open a terminal and enter:

sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7

This will delete Python 2.7.

In any case, now enter this into the terminal

sudo mv /Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions

which moves the just installed python 2.7.3 release next to the other Python releases.

3. Fixing the Group

Setting group to wheel, just like it’s done for the already installed Python Versions:

sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/2.7

4. Updating the Current Link

sudo rm /System/Library/Frameworks/Python.framework/Versions/Current
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/Current

5. Fixing /usr/bin

Since python, pythonw, etc. are not linked but actually reside in the /usr/bin directory,
/usr/bin/python still executes python 2.7.1.
Therefore, we are going to remove the old python executable files and replacing them with links into /System/Library/Frameworks/Python.framework/Versions/Current:

5.1. Removing old copies

sudo rm /usr/bin/pydoc
sudo rm /usr/bin/python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config

5.2. Creating links into /System/…/Python.framework/Versions/Current

sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc /usr/bin/pydoc
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw /usr/bin/pythonw
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config /usr/bin/python-config

6. Updating .bash_profile

Use TextMate, pico, or your favorite text editor to edit the hidden ~/.bash_profile file. If you want to be able to execute python tools like idle easily, without providing the path, edit the PATH for Python like this:

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

However, you can also remove those four lines altogether or fall-back to the .bash_profile.pysave file.
Try it by closing the terminal app, re-opening it, and entering python -Vas well as /usr/bin/python -V.

7. Updating your IDE

The last remaining step it to tell your IDE about the new Python release that you just installed.
I hope you have discovered PyCharm, JetBrain’s IDE for Python and Django.
In PyCharm, you would open its properties and under “Project Interpreters” change the Python Interpreter, by pointing it to the /System/Library/Frameworks/Python.framework/Versions/2.7 directory.

python-pycharm
python-pycharm

Updating to Python 3.3

Following the same procedure, you can also update to Python 3.3. E.g, I downloaded the Python 3.3.0 Mac OS X 64-bit/32-bit x86-64/i386 Installer form here: http://python.org/download/

After running the included installer, a script like this (run with sudo) should put everything into the right place and make python 3.3 the default python version.

#!/bin/bash
rm -R /System/Library/Frameworks/Python.framework/Versions/3.3
mv /Library/Frameworks/Python.framework/Versions/3.3 /System/Library/Frameworks/Python.framework/Versions
chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.3

rm /System/Library/Frameworks/Python.framework/Versions/Current
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3 /System/Library/Frameworks/Python.framework/Versions/Current

rm /usr/bin/pydoc
rm /usr/bin/python
rm /usr/bin/pythonw
rm /usr/bin/python-config

rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc
rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python
rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw
rm /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python-config

ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc3 /usr/bin/pydoc
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /usr/bin/python
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3 /usr/bin/pythonw
ln -s /System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3-config /usr/bin/python-config

如果安装完了,但是没有配置环境变量的话,可以直接卸载python

cd /usr/local/bin; 
ls -l . | grep '../Library/Frameworks/Python.framework/Versions/2.7' | awk '{print $9}' | xargs rm

所以还是使用类似nvm的管理node的工具比如pyenv或者pythonbrew

pythonbrew开发者都说了

This project is no longer under active development.

You are encouraged to try out pyenv instead.

那就介绍一下 pyenv

用神器homebrew安装一下,如果出现Warning: The post-install step did not complete successfully可以使用一下sudo chown -Rwhoami/usr/local/lib/node_modules

安装完毕后的命令在这里

这里也粘贴一份作为存档,但是还是以官方为准。

Command Reference

Like git, the pyenv command delegates to subcommands based on its
first argument.

The most common subcommands are:

pyenv commands

Lists all available pyenv commands.

pyenv local

Sets a local application-specific Python version by writing the version
name to a .python-version file in the current directory. This version
overrides the global version, and can be overridden itself by setting
the PYENV_VERSION environment variable or with the pyenv shell
command.

$ pyenv local 2.7.6

When run without a version number, pyenv local reports the currently
configured local version. You can also unset the local version:

$ pyenv local --unset

Previous versions of pyenv stored local version specifications in a
file named .pyenv-version. For backwards compatibility, pyenv will
read a local version specified in an .pyenv-version file, but a
.python-version file in the same directory will take precedence.

pyenv local (advanced)

You can specify multiple versions as local Python at once.

Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

$ pyenv local 2.7.6 3.3.3
$ pyenv versions
  system
* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

or, if you prefer 3.3.3 over 2.7.6,

$ pyenv local 3.3.3 2.7.6
$ pyenv versions
  system
* 2.7.6 (set by /Users/yyuu/path/to/project/.python-version)
* 3.3.3 (set by /Users/yyuu/path/to/project/.python-version)
  venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

pyenv global

Sets the global version of Python to be used in all shells by writing
the version name to the ~/.pyenv/version file. This version can be
overridden by an application-specific .python-version file, or by
setting the PYENV_VERSION environment variable.

$ pyenv global 2.7.6

The special version name system tells pyenv to use the system Python
(detected by searching your $PATH).

When run without a version number, pyenv global reports the
currently configured global version.

pyenv global (advanced)

You can specify multiple versions as global Python at once.

Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

$ pyenv global 2.7.6 3.3.3
$ pyenv versions
  system
* 2.7.6 (set by /Users/yyuu/.pyenv/version)
* 3.3.3 (set by /Users/yyuu/.pyenv/version)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

or, if you prefer 3.3.3 over 2.7.6,

$ pyenv global 3.3.3 2.7.6
$ pyenv versions
  system
* 2.7.6 (set by /Users/yyuu/.pyenv/version)
* 3.3.3 (set by /Users/yyuu/.pyenv/version)
  venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

pyenv shell

Sets a shell-specific Python version by setting the PYENV_VERSION
environment variable in your shell. This version overrides
application-specific versions and the global version.

$ pyenv shell pypy-2.2.1

When run without a version number, pyenv shell reports the current
value of PYENV_VERSION. You can also unset the shell version:

$ pyenv shell --unset

Note that you'll need pyenv's shell integration enabled (step 3 of
the installation instructions) in order to use this command. If you
prefer not to use shell integration, you may simply set the
PYENV_VERSION variable yourself:

$ export PYENV_VERSION=pypy-2.2.1

pyenv shell (advanced)

You can specify multiple versions via PYENV_VERSION at once.

Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

$ pyenv shell 2.7.6 3.3.3
$ pyenv versions
  system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

or, if you prefer 3.3.3 over 2.7.6,

$ pyenv shell 3.3.3 2.7.6
$ pyenv versions
  system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
  venv27
$ python --version
Python 3.3.3
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3

pyenv install

Install a Python version (using python-build).

Usage: pyenv install [-f] [-kvp] <version>
       pyenv install [-f] [-kvp] <definition-file>
       pyenv install -l|--list

  -l/--list        List all available versions
  -f/--force       Install even if the version appears to be installed already

  python-build options:

  -k/--keep        Keep source tree in $PYENV_BUILD_ROOT after installation
                   (defaults to $PYENV_ROOT/sources)
  -v/--verbose     Verbose mode: print compilation status to stdout
  -p/--patch       Apply a patch from stdin before building
  -g/--debug       Build a debug version

pyenv uninstall

Uninstall a specific Python version.

Usage: pyenv uninstall [-f|--force] <version>

   -f  Attempt to remove the specified version without prompting
       for confirmation. If the version does not exist, do not
       display an error message.

pyenv rehash

Installs shims for all Python binaries known to pyenv (i.e.,
~/.pyenv/versions/*/bin/*). Run this command after you install a new
version of Python, or install a package that provides binaries.

$ pyenv rehash

pyenv version

Displays the currently active Python version, along with information on
how it was set.

$ pyenv version
2.7.6 (set by /home/yyuu/.pyenv/version)

pyenv versions

Lists all Python versions known to pyenv, and shows an asterisk next to
the currently active version.

$ pyenv versions
  2.5.6
  2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)
  3.3.3
  jython-2.5.3
  pypy-2.2.1

pyenv which

Displays the full path to the executable that pyenv will invoke when
you run the given command.

$ pyenv which python3.3
/home/yyuu/.pyenv/versions/3.3.3/bin/python3.3

pyenv whence

Lists all Python versions with the given command installed.

$ pyenv whence 2to3
2.6.8
2.7.6
3.3.3

pyenv install

Part of Python-build, this installs versions of python

$ pyenv install 2.7.6
$ pyenv install 2.6.8
$ pyenv versions
  system
  2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)

pyenv install --list

List available remote versions of Python, including Anaconda, Jython, pypy, and stackless

$ pyenv install --list
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容