Pyenv: It is for the management of the Python version and is implemented by modifying the environment variables.
Vittualenv: For multi-version management of Python packages, by installing the Python package into a module as a virtual environment for Python packages, switch directories to enable switching between different environments.
Pyenv principle:
PYENV Inserts the shim path (shims) in front of the path (pointing to the specific Python version and the development project environment) by controlling the implementation of the version and Development environment control in the current shell path $path, so that the system invokes the specified version and the development project environment first.
Add a delete version, develop the project environment, install package changes to run "Pyenv rehash", Update the shim Path (shims) to the shell system path.
Use of pyenv:
1, install Pyenv
(1): Select the installed directory; I am installing the home directory here.
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
(2): Configure environment variables.
echo ' Export pyenv_root= ' $HOME/.pyenv "' >> ~/.bash_profileecho ' export path=" $PYENV _root/bin: $PATH "' >> ~/ . bash_profile
(3): Add pyenv Initialize to shell environment.
Echo ' eval ' $ (pyenv init-) "' >> ~/.bash_profile
(4): Restart Shell, is the change takes effect
2, install a version of Python
You can see which versions of Python can be installed;
[Email protected] ~]$ 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 .... |
Pyenv install-v 2.7.1pyenv install-v 3.5.0
You can complete the installation!!
Attention!!!! 安装的过程中可能会报如下错误: ERROR: The Python SSL extension is not compiled. Missing the OpenSSL Lib? Consult to the Wiki, page to fix the problem. Https://github.com/yyuu/pyenv/wiki/Common-build-problems This is because some dependent packages need to be installed before installation, as follows:yum install readline readline-devel readline-static -y
yum install openssl openssl-devel openssl-static -y
yum install sqlite-devel -y
yum install bzip2-devel bzip2-libs -y
问题即可解决
|
Then repeat the previous step:
Pyenv install-v 3.5.0pyenv install-v 2.7.12
Successful installation!!!
Unloading:
To view all versions:
[[email protected] ~]$ pyenv versions system 2.7.12* 3.5.0 (set by. Pyenv/version)
You can also view the current previous version:
[[email protected] ~]$ pyenv version3.5.0 (set by. Pyenv/version)
As you can see, I've installed three versions of Python
System represents this version
2.7.12 and 3.5.0 I installed it with pyenv.
*: Indicates the current Python version
You can switch versions of Python:
[[email protected] ~]$ pyenv versions # View all versions System 2.7.12 * 3.5.0 (set by. Pyenv/version) [[email protected] ~]$ pyenv global 2.7.12 # Toggle Python version [[email protected] ~]$ pyenv versions # The current version has been nearly switched to 2.7.12 System * 2.7.12 (set by. Pyenv/version) 3.5.0 [[email protected] ~]$ pyenv version # view current Python versions 2.7.12 (set by. Pyenv/version) [[email protected] ~]$ python Python 2.7.12 (Default, Sep 2 2017, 05:38:14) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "Help", "copyright", "credits" or "license" for more information. >>> |
3, using Virtualenv to create a virtual Python environment,
(1) Install pyenv-virtualenv plug-in:
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv Echo ' eval ' $ (pyenv Virtualenv-init-) "' >> ~/.bash_profilesource ~/.bash_profile
(2): Create a 2.7.12 virtual environment;
Pyenv virtualenv 2.7.12 env2712
Multi-environment
# Toggle and use the new Python virtual environment $ pyenv Activate env2712$ pythonpython 2.7.5 (default, Nov, 02:00:19) [GCC 4.8.5 20150623 ( Red Hat 4.8.5-4)] on Linux2type ' help ', ' copyright ', ' credits ' or ' license ' for more information.>>> # Return to System environment $pye NV deactivate# Delete the newly created environment $RM-RF ~/.pyenv/versions/env2712
Pyenv and virtualenv build python virtual Environments