python多版本管理

mac不要輕易的安裝新版本的python!
mac不要輕易的安裝新版本的python!
mac不要輕易的安裝新版本的python盒粮!

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

一般重要的事情說三遍垂涯,因為用到一個python的新功能,所以需要安裝衣屏。2.7.9以上的版本,但是mac上面是2.7.6的奉瘤,如果真的想要更新勾拉,下載官方的pkg并且設置一下環(huán)境變量煮甥,我就是沒有設置環(huán)境變量導致了一系列的問題。
配置方法找到一個博客

擔心以后博客消失藕赞,摘錄一下成肘,作為參考

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

如果安裝完了,但是沒有配置環(huán)境變量的話斧蜕,可以直接卸載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開發(fā)者都說了

This project is no longer under active development.

You are encouraged to try out pyenv instead.

那就介紹一下 pyenv

用神器homebrew安裝一下,如果出現(xiàn)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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末批销,一起剝皮案震驚了整個濱河市洒闸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌均芽,老刑警劉巖丘逸,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異掀宋,居然都是意外死亡深纲,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門劲妙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來湃鹊,“玉大人,你說我怎么就攤上這事镣奋”液牵” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵侨颈,是天一觀的道長余赢。 經(jīng)常有香客問我,道長哈垢,這世上最難降的妖魔是什么没佑? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮温赔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鬼癣。我一直安慰自己陶贼,他們只是感情好,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布待秃。 她就那樣靜靜地躺著拜秧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪章郁。 梳的紋絲不亂的頭發(fā)上枉氮,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天志衍,我揣著相機與錄音,去河邊找鬼聊替。 笑死楼肪,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的惹悄。 我是一名探鬼主播春叫,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼泣港!你這毒婦竟也來了暂殖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤当纱,失蹤者是張志新(化名)和其女友劉穎呛每,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坡氯,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡晨横,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了廉沮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颓遏。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖滞时,靈堂內(nèi)的尸體忽然破棺而出叁幢,到底是詐尸還是另有隱情,我是刑警寧澤坪稽,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布曼玩,位于F島的核電站,受9級特大地震影響窒百,放射性物質(zhì)發(fā)生泄漏黍判。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一篙梢、第九天 我趴在偏房一處隱蔽的房頂上張望顷帖。 院中可真熱鬧,春花似錦渤滞、人聲如沸贬墩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陶舞。三九已至,卻和暖如春绪励,著一層夾襖步出監(jiān)牢的瞬間肿孵,已是汗流浹背唠粥。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留停做,地道東北人晤愧。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像雅宾,于是被迫代替她去往敵國和親养涮。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容