linux 安装pyenv

前言

由python的各种优点,学习python的人,越来越多。但是,学习python有一个不容忽视的问题就是版本问题。到现在为止,python的版本有很多,但是问题在于python2与python3的区别。python3的对一些模块进行了改变,导致了python2写的代码有的不被python3兼容,从而导致程序运行报错。因此,在工作和学习python的时候,最好是安装一个pyenv,创建虚拟环境,多安装几个python的版本。于是下面就是介绍安装pyenv的python多版本操作。


安装pyenv

1.安装pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
或者
git clone https://github.com/yyuu/pyenv.git ~/.pyenv


2.配置
如果采用第一种,系统是会告诉你怎么做的(即将最后的三行追加到~/.bash_profile,做环境变量)

# Load pyenv automatically by adding
# the following to ~/.bash_profile:

export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果采用第二种(同样是做环境变量)

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile  
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile 
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
  • 1
  • 2
  • 3

3.使用变量
source ~/.bash_profile


安装python的不同版本

1.查看可安装的python的版本
pyenv install –list

Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
2.6.7
2.6.8
2.6.9
2.7-dev
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
2.7.10
2.7.11
2.7.12
2.7.13
2.7.14
2.7.15rc1
3.0.1
3.1
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.2
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.2.6
3.3.0
3.3.1
3.3.2
3.3.3
3.3.4
3.3.5
3.3.6
3.3.7
3.4.0
3.4-dev
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8
3.5.0
3.5-dev
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5
3.5.5rc1
3.6.0
3.6-dev
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.7.0b3
3.7-dev
3.8-dev



2.安装python
pyenv install -v 2.7.13
pyenv install -v 3.6.4

卸载python
pyenv uninstall 2.7.13

常见问题及解决方案:
在使用pyenv install安装python,可能会比较慢,甚至下载安装不成功

<1>.这时最好使用是下载好python源码包,然后通过pyenv进行安装(可以下载到境外云服务器)
<2>.将python源码包放置在~/.pyenv/cache/目录中(不要解压 ; 如没有cache就创建一个)
<3>.执行pyenv install 3.6.4


3.查看版本

<1>查看pyenv版本

pyenv –version
pyenv 1.2.3

<2>查看当前python版本

pyenv versions
* system (set by /root/.pyenv/version)
2.7.13
3.6.4

<3>查看当前使用的版本(system 代表当前系统的python 版本)

pyenv version
system (set by /root/.pyenv/version)

4.切换python版本
pyenv global 3.6.4

pyenv version
3.6.4 (set by /root/.pyenv/version)


pyenv versions
system
2.7.13
* 3.6.4 (set by /root/.pyenv/version)



创建虚拟python环境

1.安装插件

创建虚拟的python环境需要pyenv-virtualenv的插件,试着pyenv virtual补全一下,如果存在无需安装

如果不存在,执行如下操作
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
echo ‘eval “$(pyenv virtualenv-init -)”’ >> ~/.bash_profile
source ~/.bash_profile


2.创建虚拟环境
pyenv virtualenv 2.7.13 env2713

这是创建了一个名为env2713的python虚拟环境,这个环境的目录位于:~/.pyenv/versions/


3.查看python版本

pyenv versions
* system (set by /root/.pyenv/version)
2.7.13
2.7.13/envs/env2713
3.6.4
3.6.4/envs/env364
env2713
env364



使用python虚拟环境

1.使用虚拟环境

[xxx]#pyenv activate env364
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1’ to simulate the behavior.v364)


[xxx]# python
Python 3.6.4 (default, Apr 19 2018, 10:35:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.


2.退出虚拟环境

pyenv deactivate


3.环境迁移
pyenv virtualenv 2.7.13 env2713

<1>~./pyenv/versions/下名为2.7.14的文件夹拷贝到要迁移的服务器上
<2>修改env364/pyvenv.cfg文件中的home路径(迁移的服务器无需安装任何东西)

[xxx env364]# cat pyvenv.cfg
home =/root/.pyenv/versions/2.7.13/bin
include-system-site-packages = false
version = 2.7.13



注意事项

1.在使用环境的时候不要用sudo,否则就变成使用全局环境了(例如安装django,直接pip install django就行了,不要用sudo pip install. )

2.迁移时,python3由于依赖于相对高一点的glibc库,要注意迁移与被迁移服务器的glibc的版本问题,版本不同时还需要手动安装,更新时注意

(glibc是GNU发布的libc库,即c运行库;glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibclinux系统的依赖)


下面是github上有关安装部分

Installation

If you're on Mac OS X, consider installing with Homebrew.

The automatic installer

Visit my other project:https://github.com/pyenv/pyenv-installer

Basic GitHub Checkout

This will get you going with the latest version of pyenv and make iteasy to fork and contribute any changes back upstream.

  1. Check out pyenv where you want it installed.A good place to choose is $HOME/.pyenv (but you can install it somewhere else).

     $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    
  2. Define environment variable PYENV_ROOT to point to the path wherepyenv repo is cloned and add $PYENV_ROOT/bin to your $PATH for accessto the pyenv command-line utility.

    $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

    Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.Proxy note: If you use a proxy, export http_proxy and HTTPS_PROXY too.

  3. Add pyenv init to your shell to enable shims and autocompletion.Please make sure eval "$(pyenv init -)" is placed toward the end of the shellconfiguration file since it manipulates PATH during the initialization.

    $ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

    Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.

    General warning: There are some systems where the BASH_ENV variable is configuredto point to .bashrc. On such systems you should almost certainly put the abovementioned lineeval "$(pyenv init -)" into .bash_profile, and not into .bashrc. Otherwise youmay observe strange behaviour, such as pyenv getting into an infinite loop.See #264 for details.

  4. Restart your shell so the path changes take effect.You can now begin using pyenv.

    $ exec "$SHELL"
  5. Install Python versions into $(pyenv root)/versions.For example, to download and install Python 2.7.8, run:

    $ pyenv install 2.7.8

    NOTE: If you need to pass configure option to build, please useCONFIGURE_OPTS environment variable.

    NOTE: If you want to use proxy to download, please use http_proxy and https_proxyenvironment variable.

    NOTE: If you are having trouble installing a python version,please visit the wiki page aboutCommon Build Problems

Upgrading

If you've installed pyenv using the instructions above, you canupgrade your installation at any time using git.

To upgrade to the latest development version of pyenv, use git pull:

$ cd $(pyenv root)
$ git pull

To upgrade to a specific release of pyenv, check out the corresponding tag:

$ cd $(pyenv root)
$ git fetch
$ git tag
v0.1.0
$ git checkout v0.1.0

Uninstalling pyenv

The simplicity of pyenv makes it easy to temporarily disable it, oruninstall from the system.

  1. To disable pyenv managing your Python versions, simply remove thepyenv init line from your shell startup configuration. This willremove pyenv shims directory from PATH, and future invocations likepython will execute the system Python version, as before pyenv.

pyenv will still be accessible on the command line, but your Pythonapps won't be affected by version switching.

  1. To completely uninstall pyenv, perform step (1) and then removeits root directory. This will delete all Python versions that wereinstalled under $(pyenv root)/versions/ directory:

    rm -rf $(pyenv root)

    If you've installed pyenv using a package manager, as a final stepperform the pyenv package removal. For instance, for Homebrew:

     brew uninstall pyenv
    

另外一种安装

Installation

If you're on Mac OS X, consider installing with Homebrew.

The automatic installer

Visit my other project:https://github.com/pyenv/pyenv-installer

Basic GitHub Checkout

This will get you going with the latest version of pyenv and make iteasy to fork and contribute any changes back upstream.

  1. Check out pyenv where you want it installed.A good place to choose is $HOME/.pyenv (but you can install it somewhere else).

     $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    
  2. Define environment variable PYENV_ROOT to point to the path wherepyenv repo is cloned and add $PYENV_ROOT/bin to your $PATH for accessto the pyenv command-line utility.

    $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

    Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.Proxy note: If you use a proxy, export http_proxy and HTTPS_PROXY too.

  3. Add pyenv init to your shell to enable shims and autocompletion.Please make sure eval "$(pyenv init -)" is placed toward the end of the shellconfiguration file since it manipulates PATH during the initialization.

    $ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

    Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.

    General warning: There are some systems where the BASH_ENV variable is configuredto point to .bashrc. On such systems you should almost certainly put the abovementioned lineeval "$(pyenv init -)" into .bash_profile, and not into .bashrc. Otherwise youmay observe strange behaviour, such as pyenv getting into an infinite loop.See #264 for details.

  4. Restart your shell so the path changes take effect.You can now begin using pyenv.

    $ exec "$SHELL"
  5. Install Python versions into $(pyenv root)/versions.For example, to download and install Python 2.7.8, run:

    $ pyenv install 2.7.8

    NOTE: If you need to pass configure option to build, please useCONFIGURE_OPTS environment variable.

    NOTE: If you want to use proxy to download, please use http_proxy and https_proxyenvironment variable.

    NOTE: If you are having trouble installing a python version,please visit the wiki page aboutCommon Build Problems

Upgrading

If you've installed pyenv using the instructions above, you canupgrade your installation at any time using git.

To upgrade to the latest development version of pyenv, use git pull:

$ cd $(pyenv root)
$ git pull

To upgrade to a specific release of pyenv, check out the corresponding tag:

$ cd $(pyenv root)
$ git fetch
$ git tag
v0.1.0
$ git checkout v0.1.0

Uninstalling pyenv

The simplicity of pyenv makes it easy to temporarily disable it, oruninstall from the system.

  1. To disable pyenv managing your Python versions, simply remove thepyenv init line from your shell startup configuration. This willremove pyenv shims directory from PATH, and future invocations likepython will execute the system Python version, as before pyenv.

pyenv will still be accessible on the command line, but your Pythonapps won't be affected by version switching.

  1. To completely uninstall pyenv, perform step (1) and then removeits root directory. This will delete all Python versions that wereinstalled under $(pyenv root)/versions/ directory:

    rm -rf $(pyenv root)

    If you've installed pyenv using a package manager, as a final stepperform the pyenv package removal. For instance, for Homebrew:

     brew uninstall pyenv
    

你可能感兴趣的:(python,linux,ubuntu)