[python@python ~]$ pyenv #直接运行,可以查看其版本和基本使用
pyenv 1.2.9
Usage: pyenv <command> [<args>]
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 <command>' for information on a specific command.
pyenv的install选项
-l :列出可以安装的版本[python@python ~]$ pyenv install -l #列出可以安装的版本
Available versions:
2.1.3
2.2.3
2.3.7
.....[python@python ~]$ pyenv install 3.6.6
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
....注:pyenv提供了缓存的机制,也就是说,如果在cache路径中找的参数对应的安装文件,则使用cache路径下的安装文件进行安装,安装包上传好之后,再执行上述安装命令,即可使用本地安装包进行安装[python@python cache]$mkdir ~/.pyenv/cache #在本用户下的家目录下创建cache路径,必须是~/.pyenv/cache
[python@python cache]$ cd ~/.pyenv/cache;ll #将python安装包上传到该路径下
total 94356
-rw-rw-r--. 1 python python 20656090 Feb 2 17:38 Python-3.5.3.tar.gz
-rw-rw-r--. 1 python python 15213396 Feb 2 17:36 Python-3.5.3.tar.xz
-rw-rw-r--. 1 python python 20656090 Feb 2 17:39 Python-3.5.3.tgz
-rw-rw-r--. 1 python python 17156744 Feb 2 17:39 Python-3.6.6.tar.xz
-rw-rw-r--. 1 python python 22930752 Feb 2 17:40 Python-3.6.6.tgz[python@python cache]$ pyenv versions # *表示当前正在使用的
* system (set by /home/python/.pyenv/version)
3.6.6[python@python cache]$ pyenv version
3.6.6 (set by /home/python/.pyenv/version)
# 这与python -V的结果类似
[python@python ~]$ python -V
Python 2.6.6[python@python ~]$ pyenv global system
[python@python ~]$ python -V
Python 2.6.6
python@python ~]$ pyenv global 3.6.6
[python@python ~]$ python -V
Python 3.6.6如果想要在一个用户下,控制不同的项目的python版本,可以使用一下方法[python@python ~]$ pyenv shell 3.5.3
[python@python ~]$ python -V
Python 3.5.3要想在目录级别使用不同版本的python,则可以使用如下选项[python@python ~]$ mkdir -p ~/dingcx/projects/web
[python@python ~]$ cd ~/dingcx/projects/web
#在当前目录下,修改当前目录的python版本,并且,这个级别的版本不会受shell或者global下的python版本控制
[python@python web]$ pyenv local 3.5.3
[python@python web]$ python -V
Python 3.5.3
[python@python web]$ cd ..
[python@python projects]$ python -V
Python 3.6.6pyenv的local版本控制的本质
使用一个隐藏文件来实现了该目录及其子目录下的版本控制,但是如果我们在当前目录下安装了一个pip,那影响的就是整个3.5.3的[python@python projects]$ pwd
/home/python/dingcx/projects
[python@python projects]$ cd ./web
[python@python web]$ ll -a
total 12
drwxrwxr-x. 2 python python 4096 Mar 14 10:20 .
drwxrwxr-x. 3 python python 4096 Mar 14 10:20 ..
-rw-rw-r--. 1 python python 6 Mar 14 10:20 .python-version
[python@python web]$ cat .python-version
3.5.3
[python@python web]$ pwd
/home/python/dingcx/projects/web
#pyenv local --unset,撤销以上都是在公共的空间中配置不同的python版本,如果多个项目使用不同的版本开发,或者使用不同的Python版本部署运行,或者同样的版本开发但是不同项目使用了不同版本的库,使用公共空间配置Python版本就会发生冲突,那么最好的解决办法就是使用虚拟环境(ding) [python@www plugins]$ ll
total 24
drwxr-xr-x. 4 python python 4096 Dec 30 07:54 pyenv-doctor
drwxr-xr-x. 6 python python 4096 Dec 30 07:54 pyenv-installer
drwxr-xr-x. 5 python python 4096 Dec 30 07:55 pyenv-update
drwxr-xr-x. 8 python python 4096 Dec 30 07:55 pyenv-virtualenv
drwxr-xr-x. 4 python python 4096 Dec 30 07:55 pyenv-which-ext
drwxrwxr-x. 5 python python 4096 Dec 30 07:54 python-build
(ding) [python@www plugins]$ pwd
/home/python/.pyenv/pluginspyenv-virtualenv的使用,setuptools和pip是默认就安装的[python@python web]$ pyenv virtualenv 3.6.6 ding #安装virtualenv
Looking in links: /tmp/tmp8eq3hccs
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.6/envs/ding/lib/python3.6/site-packages (39.0.1)
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.6/envs/ding/lib/python3.6/site-packages (10.0.1)
[python@python web]$ pyenv versions
system
* 3.5.3 (set by /home/python/dingcx/projects/web/.python-version)
3.6.6
3.6.6/envs/ding
ding创建完虚拟环境之后,可以把这个当做一个版本来使用[python@www ~]$ pyenv global ding
(ding) [python@www ~]$ python -V
Python 3.6.6
(ding) [python@www ~]$ pyenv global 3.6.6
[python@www ~]$ pyenv shell ding
(ding) [python@www ~]$ python -V
Python 3.6.6
(ding) [python@www ~]$ pyenv global 3.6.6
(ding) [python@www ~]$ pyenv local ding
(ding) [python@www ~]$ python -V
Python 3.6.6pyenv虚拟环境的本质
1.在~/.pyenv/versions下有一个软连接文件,会指向pyenv管理的版本目录(3.6.6)中,在这个路径中的envs路径下会创建虚拟环境名的路径
(ding) [python@www ~]$ pip list
Package Version
---------------- -------
backcall 0.1.0
decorator 4.4.1
ipython 7.12.0
ipython-genutils 0.2.0
jedi 0.16.0
parso 0.6.1
pexpect 4.8.0
pickleshare 0.7.5
pip 10.0.1
prompt-toolkit 3.0.3
ptyprocess 0.6.0
Pygments 2.5.2
setuptools 39.0.1
six 1.14.0
traitlets 4.3.3
wcwidth 0.1.8
You are using pip version 10.0.1, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(ding) [python@www ~]$ pip freeze > requirements
You are using pip version 10.0.1, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(ding) [python@www ~]$ more requirements
backcall==0.1.0
decorator==4.4.1
ipython==7.12.0
ipython-genutils==0.2.0
jedi==0.16.0
parso==0.6.1
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.3
ptyprocess==0.6.0
Pygments==2.5.2
six==1.14.0
traitlets==4.3.3
wcwidth==0.1.8
#在部署环境中,安装库
(dcx) [python@www ~]$ pip install -r requirements