python多版本管理

作者: oliverhuang | 来源:发表于2015-10-11 21:04 被阅读66225次

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-pycharmpython-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

相关文章

  • Mac下Python环境搭建、多版本管理

    1、查看Python版本python 多版本管理 参考网站:Mac多Python版本共存,多个独立Python开发...

  • pyenv

    pyenv是个多版本python管理器,可以同时管理多个python版本共存,如pypy,miniconde等等 ...

  • Python AI 必备开发工具和工具包

    Anaconda -- python多版本虚拟环境管理和包管理工具;Anaconda可以创建不同python版本的...

  • Ubuntu管理软件的多个版本

    update-alternatives(维护系统命令链接符) 可用来管理多版本的JDK,python等 管理多版本...

  • Node版本管理控制器n

    简介: Node 可以通过n来管理不同的版本。类似Python中的pyenv进行python多版本管理。 安装 n...

  • Python版本管理pyenv

    Pyenv简介 对多版本的python进行管理 为指定目录设定python环境,进入目录自动切换python版本 ...

  • python 多版本管理

    使用 pyenv: brew update brew install pyenv brew upgrade pye...

  • Python多版本管理

    背景使用python的同学肯定遇到过以下这些情况:1)系统自带的Python是2.x,自己需要Python 3.x...

  • python多版本管理

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

  • python多版本管理

    pyenv 介绍维护不同版本的 Python一键(命令)切换全局、本地或当前 shell 使用的 Python 版...

网友评论

  • Luat物联网通信模块:可以关注下Luat开源项目,基于Air200 GPRS模块(价格才12块多),基于Lua开发应用软件,超级容易上手。 完美支持mqtt,而且开发点阵LCD 的UI 非常方便,有丰富的底层库,支持中文很方便。
  • 2423d130677d:啊啊啊啊,我和你一样,心都碎了

本文标题:python多版本管理

本文链接:https://www.haomeiwen.com/subject/ewfocttx.html