mac下使用pyenv,pyenv-virtualenv管理python的多个版本

0.系统版本

angel:~ $ more /System/Library/CoreServices/SystemVersion.plist

        ProductBuildVersion

        14C109

        ProductCopyright

        1983-2015 Apple Inc.

        ProductName

        Mac OS X

        ProductUserVisibleVersion

        10.10.2

        ProductVersion

        10.10.2


1.安装homebrew

打开终端输入如下命令,就可以安装homebrew。

angel:~ $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

测试是否安装成功

angel:~ $ brew -v

Homebrew 0.9.5


表示安装成功。

参照 http://brew.sh/index_zh-cn.html

2.安装pyenv,pyenv-virtualenv

angel:~ $ brew install pyenv 

angel:~ $ brew install pyenv-virtualenv

验证是否安装成功

angel:~ $ pyenv -v

pyenv 20150326

angel:~ $ pyenv-virtualenv --version

/usr/local/bin/pyenv-virtualenv: line 127: pyenv-prefix: command not found

/usr/local/bin/pyenv-virtualenv: line 130: pyenv-prefix: command not found

pyenv-virtualenv 20150119 (virtualenv unknown)

pyenv安装成功,pyenv-virtualenv有点异常先略过(具体原因还未调查,倒是不影响后面使用,有看官知道原因的欢迎交流)。

3.pyenv install --list 查看可以安装的python版本

angel:~ $ pyenv install --list

Available versions:

  2.7

  2.7.1

  2.7.2

  2.7.3

  2.7.4

  2.7.5

  2.7.6

  2.7.7

  2.7.8

  2.7.9

  ......(由于篇幅问题,此处略去3000字)

  3.4.2

  3.4.3

  3.5.0a1

  3.5.0a2

  3.5-dev


4.安装对应的python版本

angel:~ $ pyenv install 3.4.3

pyenv: /Users/angel/.pyenv/versions/3.4.3 already exists

continue with installation? (y/N) N

我的电脑已经安装过3.4.3。。。

pyenv versions可以查看目前正在使用的python版本,前面带星号(*)的就是表示当前使用的python版本(3.4.3)

angel:~ $ pyenv versions

  system

* 3.4.3 (set by /Users/angel/.pyenv/version)

参照 http://v2in.com/pyenv-installation-and-usage.html

http://seisman.info/python-pyenv.html

5.设定环境

在 .bashrc(或者.bash_profile)加入如下内容

没有如上2个文件,可以生成一个。

if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

6.pyenv global 来回切换python版本

angel:~ $ python

Python 3.4.3 (default, May  2 2015, 21:56:04) 

[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> exit()

angel:~ $ pyenv versions

  system

* 3.4.3 (set by /Users/angel/.pyenv/version)

angel:~ $ pyenv global system  

angel:~ $ python

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> exit()

angel:~ $ pyenv versions

* system (set by /Users/angel/.pyenv/version)

  3.4.3

angel:~ $ pyenv versions

“system"是系统默认版本

7.pyenv virtualenv创建虚拟python环境

angel:~ $ pyenv virtualenv 3.4.3 myvenv

Ignoring indexes: https://pypi.python.org/simple

Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/angel/.pyenv/versions/myvenv/lib/python3.4/site-packages

Requirement already satisfied (use --upgrade to upgrade): pip in /Users/angel/.pyenv/versions/myvenv/lib/python3.4/site-packages


angel:~ $ pyenv virtualenvs  ‘查看python虚拟环境

  myvenv (created from /Users/angel/.pyenv/versions/3.4.3)

angel:~ $ pyenv versions

* system (set by /Users/angel/.pyenv/version)

  3.4.3

  myvenv

angel:~ $ pyenv shell myvenv

pyenv-virtualenv: activate myvenv

(myvenv) angel:~ $ python

Python 3.4.3 (default, May  2 2015, 21:56:04) 

[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.49)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> exit()

(myvenv) angel:~ $ pyenv versions

  system

  3.4.3

* myvenv (set by PYENV_VERSION environment variable)

最后想说的是,pyenv 的相关命令通过pyenv -h 和 pyenv commands来查看,具体用法自己试验。

angel:~ $ pyenv commands

--version

activate

commands

completions

deactivate

exec

global

help

hooks

init

install

local

prefix

rehash

root

shell

shims

uninstall

version

version-file

version-file-read

version-file-write

version-name

version-origin

versions

virtualenv

virtualenv-init

virtualenv-prefix

virtualenvs

whence

which

angel:~ $ pyenv -h

pyenv 20150326

Usage: pyenv []


Some useful pyenv commands are:

   commands    List all available pyenv commands

   local       Set or show the local application-specific Python version

   global      Set or show the global Python version

   shell       Set or show the shell-specific Python version

   install     Install a Python version using python-build

   uninstall   Uninstall a specific Python version

   rehash      Rehash pyenv shims (run this after installing executables)

   version     Show the current Python version and its origin

   versions    List all Python versions available to pyenv

   which       Display the full path to an executable

   whence      List all Python versions that contain the given executable


See `pyenv help ' for information on a specific command.

For full documentation, see: https://github.com/yyuu/pyenv#readme

考照 http://tbb.co/managing-python-on-os-x-with-pyenv/

Good night !

你可能感兴趣的:(Python)