Python生产环境的构建(pyenv、pipenv)
以下文档,基于debian:
$ cat /proc/version Linux version 4.19.0-6-amd64 (debian-kernel@lists.debian.org) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.67-2 (2019-08-28)
一、Pyenv
1. 能做什么
可以基于用户修改全局python的版本。
支持每个项目对应不同的python版本。
2. 简单说明
可以通过设置PATH来改变当前shell session的python版本:pyenv shell.
可以通过配置项目所在目录的.python-version文件来指定项目使用的python版本。你也可以使用pyenv local命令来修改这个文件指定的python版本。这个文件会首先在当前目录查找,如果没有找到,会继续往上层目录递归寻找。
可以修改$(pyenv root)/version来指定系统默认的python版本,可以通过pyenv global修改。
3. 安装 pyenv
参考自:https://github.com/pyenv/pyenv-installer
curl https://pyenv.run | bash # 按照提示,将下面三行添加到~/.bashrc文件中 # Load pyenv automatically by adding # the following to ~/.bashrc: export PYENV_ROOT="$HOME/.pyenv" export PATH="/home/kyan/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" # 重启shell环境 exec $SHELL # 更新 pyenv update # 卸载,还需要删除添加到bashrc文件中的那几行 rm -fr ~/.pyenv
4. 安装和删除指定的python版本
# 安装 # 在安装python之前,最好安装下面的包,不然会有 "Missing ctypes" 错误 sudo apt-get install libffi-dev sudo apt-get install libbz2-dev sudo apt-get install libreadline-dev sudo apt-get install libsqlite3-dev pyenv install 3.8.0 # 删除 pyenv uninstall 3.8.0 # 详细命令参数,可以参考help kyan@kyanvultrtokyo0:~$ pyenv pyenv 1.2.15 Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands --version Display the version of pyenv 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 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 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 and its origin 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
二、使用pipenv
1. 简介
pipenv是用来替换pip+virtualenv的解决方案。
官网:https://github.com/pypa/pipenv
2. 安装
# 更新一下pip,并且安装下pipenv /home/kyan/.pyenv/shims/pip install --upgrade pip /home/kyan/.pyenv/shims/pip install pipenv
3. 使用
mkdir pipenv_test cd pipenv_test # 项目使用python 3.8.0 pyenv shell 3.8.0 pipenv install --python=`pyenv which python` # Creating a virtualenv for this project... pipenv install # 安装需要的软件包 pipenv install requests # 卸载 pipenv uninstall requests # 检查包 pipenv check # 全部卸载 pipenv uninstall --all # 展示所有依赖图 pipenv graph # 使用当前环境执行命令 pipenv run python --version # 进入shell环境 pipenv shell
云海天教程网,免费的在线学习python平台,欢迎关注!
本文转自:https://www.jianshu.com/p/d6448b0fd12f