Pyenv--优秀的Python多版本管理工具

引言

在python的世界里,其实是分裂的,有Python2, 还有Python3,于是在两者之间的切换也是非常麻烦的,有时候,也想安装一下最新的版本,该如何来做呢?好吧,这里Pyenv来拯救你。

环境说明

CentOS 7.4

准备基础环境

yum install -y readline readline-devel readline-static openssl openssl-devel openssl-static sqlite-devel bzip2-devel bzip2-libs git

这里写图片描述

然后执行环境变量:

git clone https://github.com/yyuu/pyenv.git ~/.pyenv
echo ‘export PYENV_ROOT=”$HOME/.pyenv”’ >> ~/.bash_profile

echo ‘export PATH=” P Y E N V R O O T / b i n : PATH”’ >> ~/.bash_profile

echo ‘eval “$(pyenv init -)”’ >> ~/.bash_profile

这里写图片描述

执行环境变量,生效机制:

source ~/.bash_profile

这里写图片描述

执行pyenv命令

查看pyenv可用的命令参数:

pyenv –help

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.
For full documentation, see: https://github.com/pyenv/pyenv#readme

查看目前可以支持的python版本:

pyenv install –list

安装特定的版本:

pyenv install -v 3.7.0

这里写图片描述

查看目前本地的使用版本:

pyenv versions

  system
* 3.6.5 (set by /root/.pyenv/version)
  3.7.0
  3.7.0/envs/env370
  env370

查看当前使用的版本:

pyenv version

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

设置系统版本:

pyenv global 3.6.5

设置当前版本

pyenv local 3.6.5

安装virtualenv

virtualenv是一个虚拟的隔离环境,相当于从本地安装的环境中,复制了一份完全独立的环境。
下载源代码文件:

git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

这里写图片描述
执行环境变量:

echo ‘eval “$(pyenv virtualenv-init -)”’ >> ~/.bash_profile
source ~/.bash_profile

这里写图片描述

于是整个过程就安装好了,大家就可以使用virtualenv直接来创建自己的虚拟环境了。

问题与Fenix

在安装过程中,碰到了如下问题:

Traceback (most recent call last):
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
  File "/tmp/tmpw4nbv7tn/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py", line 42, in <module>
  File "/tmp/tmpw4nbv7tn/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py", line 16, in <module>
  File "/tmp/tmpw4nbv7tn/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 25, in <module>
  File "/tmp/tmpw4nbv7tn/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 39, in <module>
  File "/tmp/tmpw4nbv7tn/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module>
  File "/tmp/python-build.20180804141530.25664/Python-3.7.0/Lib/ctypes/__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
make: *** [install] Error 1

BUILD FAILED (CentOS 7.4.1708 using python-build 1.2.6)

解决的办法:

yum install libffi-devel -y

猜你喜欢

转载自blog.csdn.net/blueheart20/article/details/81567442
今日推荐