linux centos7 安装虚拟Python环境,pyenv安装文档

时间:2021-02-14 08:55:57

python多版本控制pyenv安装文档

1.在线安装:

[root@enofyw tools]# curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 148 100 148 0 0 20 0 0:00:07 0:00:07 --:--:-- 32
100 2454 100 2454 0 0 30 0 0:01:21 0:01:21 --:--:-- 709
正克隆到 '/root/.pyenv'...
remote: Enumerating objects: 647, done.
remote: Counting objects: 100% (647/647), done.
remote: Compressing objects: 100% (490/490), done.
remote: Total 647 (delta 318), reused 246 (delta 66), pack-reused 0
接收对象中: 100% (647/647), 372.99 KiB | 47.00 KiB/s, done.
处理 delta 中: 100% (318/318), done.
正克隆到 '/root/.pyenv/plugins/pyenv-doctor'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 11 (delta 1), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (11/11), done.
正克隆到 '/root/.pyenv/plugins/pyenv-installer'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 16 (delta 1), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (16/16), done.
正克隆到 '/root/.pyenv/plugins/pyenv-update'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
正克隆到 '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 57 (delta 11), reused 21 (delta 0), pack-reused 0
Unpacking objects: 100% (57/57), done.
正克隆到 '/root/.pyenv/plugins/pyenv-which-ext'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done. WARNING: seems you still have not added 'pyenv' to the load path. Load pyenv automatically by adding the following to ~/.bashrc: export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[root@enofyw tools]#
  • 将最后提示的加入到环境变量中。

    vi /etc/profile 或者当前用户的 vi ~/.bashrc

    export PATH="/root/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"

    source /etc/profile source ~/.bashrc

  • 输入 pyenv -v --查看pyenv版本

    [root@enofyw tools]# pyenv -v
    pyenv 1.2.13
  • 查看当前python版本

    pyenv versions

  • 查看可以安装的python版本

    pyenv install --list

  • 查看当前使用的python版本

    pyenv version

  • 安装python版本

    pyenv install -v3.6.5

  • 离线安装python版本方法

    在.pyenv目录下创建cache文件件,

    正常安装时,会提示下载地址

    [root@devops ~]# pyenv install -v 3.7.2
    /tmp/python-build.20190821171919.62788 ~
    Downloading Python-3.7.2.tar.xz...
    -> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz

    进入cache文件夹下,先wget 下载到本地,在执行install安装

    wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz

  • 卸载python

    pyenv uninstall 3.6.5

  • 切换python版本

    pyenv global 3.6.7

查看是否切换成功

python -V

python 3.6.7

如果切换不成功

这种情况一般是因为用pyenv指定了local版本。

解决方法:

取消设置local版本:pyenv local --unset

安装python3.7版本 需先安装系统依赖包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make

创建虚拟python环境

  • 创建虚拟的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
  • 创建虚拟环境

    pyenv virtualenv 3.6.5 env365

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

  • 查看

  • 使用虚拟环境

    • pyenv activate env365

    • python 看当前python版本

    • [root@enofyw ~]# pyenv activate venv365
      pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
      (venv365) [root@enofyw ~]# python
      Python 3.6.5 (default, Aug 19 2019, 14:39:19)
      [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
  • 退出虚拟环境

    • pyenv deactivate