1. 程式人生 > 實用技巧 >pyenv虛擬環境安裝

pyenv虛擬環境安裝

安裝過程

配置yum源

# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# yum -y install yum-utils

# yum install wget -y 

# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 

安裝git

# yum install -y git

安裝Python環境依賴

# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

新增使用者

# useradd python

# echo |passwd --stdin python

# su - python

安裝pyenv

$  curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer|bash

新增環境變數

$ vim .bash_profile
# .bash_profile # Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)" $ . .bash_profile

檢視python版本

$ python -V
Python 2.7.

Pyenv的命令

$ pyenv
pyenv 1.2.
Usage: pyenv <command> [<args>] Some useful pyenv commands are:
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and development tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version(s)
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version(s)
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version(s) and its origin
--version Display the version of pyenv
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable

虛擬環境下安裝Python3.5.3版本

1、從官網下載(非常慢,不建議)

$ pyenv install 3.5. -v
/tmp/python-build.20200718063111.6245 ~
Downloading Python-3.5..tar.xz...
-> https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz

2、傳包的方式

$ ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .pki .pyenv .viminfo $ mkdir .pyenv/cache 將下載好的包放入cache目錄,然後執行安裝命令 $ pyenv install 3.6. -v
/tmp/python-build.20200718070338.7001 ~/.pyenv
/tmp/python-build.20200718070338.7001/Python-3.6. /tmp/python-build.20200718070338.7001 ~/.pyenv
Installing Python-3.6....
checking build system type... x86_64-pc-linux-gnu

pyenv版本控制

當前登入使用者全域性生效

[[email protected] .pyenv]$ pyenv version
system (set by /home/python/.pyenv/version) [[email protected] .pyenv]$ python -V
Python 2.7.

更改全域性版本(企業中禁用)

$ pyenv version
3.6. (set by /home/python/.pyenv/version) $ python -V
Python 2.7.

開啟新終端檢視版本資訊

[[email protected] ~]$ pyenv versions
system
* 3.6. (set by /home/python/.pyenv/version) [[email protected] ~]$ python -V
Python 3.6.

切回原版本

[[email protected] ~]$ pyenv global system

[[email protected] ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.

注意:這裡的global引數作用的是python使用者,而非root使用者。如果是root使用者安裝的,那麼不要用global,影響很大

修改當前shell的Python版本

[[email protected] .pyenv]$ pyenv shell 3.6.

[[email protected] .pyenv]$ pyenv versions
system
* 3.6. (set by PYENV_VERSION environment variable) [[email protected] .pyenv]$ python -V
Python 2.7.

新終端檢視版本

[[email protected] ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.

[[email protected] ~]$ python -V
Python 2.7.
這種方法是當前shell生效的,如果當前shell關閉了,版本設定就無效了,再開啟shell的時候版本就變回原來的了,還需要重新再設定一次,非常麻煩,推薦使用下面的方法

Local(當前資料夾生效)

建立專案目錄

[[email protected] .pyenv]$ cd
[[email protected] ~]$ mkdir zh/projects -p
[[email protected] ~]$ cd zh/projects/

新終端切換到專案目錄

[[email protected] ~]$ cd zh/projects/

新終端設定當前目錄版本

[[email protected] projects]$ pyenv local 3.6.

[[email protected] projects]$ pyenv versions
system
* 3.6. (set by /home/python/zh/projects/.python-version) [[email protected] projects]$ python -V
Python 3.6.

檢視其它目錄版本

[[email protected] cmdb]$ cd

[[email protected] ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6. [[email protected] ~]$ python -V
Python 2.7.

檢視子目錄版本

[[email protected] projects]$ mkdir cmdb

[[email protected] projects]$ cd zh/projects/cmdb

[[email protected] cmdb]$ pyenv versions
system
* 3.6. (set by /home/python/zh/projects/.python-version) [[email protected] cmdb]$ python -V
Python 3.6.

虛擬環境(解決打包問題)

安裝3.5.3版本

[[email protected] ~]$ cd .pyenv/cache/

[[email protected] cache]$ ll
total
-rw-r--r--. python python Dec Python-3.5..tar.xz
-rw-r--r--. python python Nov Python-3.6..tar.xz [[email protected] projects]$ pyenv install 3.5. -v [[email protected] ~]$ ll .pyenv/versions
total
drwxr-xr-x. python python Jul : 3.5.
drwxr-xr-x. python python Jul : 3.6.

建立虛擬環境

[[email protected] cmdb]$ pyenv virtualenv 3.5. zh353
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5./envs/zh353/lib/python3./site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5./envs/zh353/lib/python3./site-packages

檢視版本資訊

[[email protected] cmdb]$ pyenv versions
system
3.5.
3.5./envs/zh353
* 3.6. (set by /home/python/zh/projects/.python-version)
zh353

修改當前專案目錄的Python版本

[[email protected] cmdb]$ pyenv local zh353 

(zh353) [[email protected] cmdb]$ pyenv versions
system
3.5.
3.5./envs/zh353
3.6.
* zh353 (set by /home/python/zh/projects/cmdb/.python-version)

建立新的專案目錄並設定版本

(zh353) [[email protected] cmdb]$ mkdir ../web
(zh353) [[email protected] cmdb]$ cd ../web [[email protected] web]$ pyenv local 3.6.
[[email protected] web]$ pyenv versions
system
3.5.
3.5./envs/zh353
* 3.6. (set by /home/python/zh/projects/web/.python-version)
zh353