温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何搭建Python生产环境pyenv和pipenv

发布时间:2020-08-05 10:49:39 来源:亿速云 阅读:183 作者:清晨 栏目:编程语言

这篇文章主要介绍了如何搭建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生产环境pyenv和pipenv内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI