文章详情页 您现在的位置是:网站首页>文章详情

替换pipenv的python包管理方案

图片丢失 Jeyrce.Lu 发表于:2020年5月18日 13:16 分类:【Python 2319次阅读

之前的包管理工具pipenv,使用起来总是觉得有点怪异,例如可能会下载失败,下载慢,锁定失败,锁定慢,总之就是让人不爽。本篇将介绍利用现有工具汇总的一种新的python包管理解决方案。

使用到的工具

(0)pip

(1)virtualenv、virtualenvwrapper

(2)pip-tools

pip install virtualenv virtualenvwrapper pip-tools

使用方法如下

(1)配置pip源  ~/.pip/pip.conf

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url = http://192.168.1.80:8080/
timeout = 60
trusted-host = 
	mirrors.aliyun.com
	192.168.1.80

(2)配置环境变量 ~/.bashrc

# ==============python环境

# 虚拟环境存放位置
export WORKON_HOME=$HOME/.local/share/.virtualenvs

# virtualenv所使用的python命令
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

# 别名,方便退出虚拟环境
alias workout='deactivate'

# 使虚拟环境相关命令生效
source /usr/local/bin/virtualenvwrapper.sh

# 将虚拟环境可能用到的三方命令追加到PATH
export PATH=$PATH:$HOME/.local/bin

(3)虚拟环境相关操作

# 查看当前机器上所有已存在虚拟环境
lsvirtualenv

# 使用某个python版本创建一个虚拟环境(py2和py3可以共存)
mkvirtualenv webservice -p /usr/bin/python2

# 删除虚拟环境
rmvirtualenv webservice

# 退出某个虚拟环境
deactivate 或 workout

# 复制一个虚拟环境
cpvirtualenv

# 激活某个虚拟环境
workon  webservice

(4)导出虚拟环境依赖文件

pip freeze > requirements.txt

之后会得到一个包含包名和版本信息的文件

(webservice-x) [root@192-168-1-11 ~]# cat requirements-sm.txt 
amqp==1.4.9
anyjson==0.3.3
APScheduler==3.6.3
backports.functools-lru-cache==1.6.1
backports.ssl-match-hostname==3.7.0.1
bcrypt==3.1.7
beautifulsoup4==4.9.0
billiard==3.3.0.23
bs4==0.0.1
celery==3.1.26.post2
certifi==2020.4.5.1

(5)锁定包hash值

# 必须在(3)的基础上做
pip-compile --generate-hashes requirements.txt

之后会得到一个既包含包版本,又包含hash值的依赖文件

(webservice-x) [root@192-168-1-11 ~]# cat requirements.txt 
#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile --generate-hashes requirements.txt
#
--index-url https://mirrors.aliyun.com/pypi/simple/
--extra-index-url http://192.168.1.80:8080/
--trusted-host mirrors.aliyun.com
--trusted-host 192.168.1.80

amqp==1.4.9 \
    --hash=sha256:2dea4d16d073c902c3b89d9b96620fb6729ac0f7a923bbc777cb4ad827c0c61a \
    --hash=sha256:e0ed0ce6b8ffe5690a2e856c7908dc557e0e605283d6885dd1361d79f2928908 \
    # via -r requirements.txt, kombu
anyjson==0.3.3 \
    --hash=sha256:37812d863c9ad3e35c0734c42e0bf0320ce8c3bed82cd20ad54cb34d158157ba \
    # via -r requirements.txt, kombu
apscheduler==3.6.3 \
    --hash=sha256:3bb5229eed6fbbdafc13ce962712ae66e175aa214c69bed35a06bffcf0c5e244 \
    --hash=sha256:e8b1ecdb4c7cb2818913f766d5898183c7cb8936680710a4d3a966e02262e526 \
    # via -r requirements.txt, qdata-alert
backports.functools-lru-cache==1.6.1 \
    --hash=sha256:0bada4c2f8a43d533e4ecb7a12214d9420e66eb206d54bf2d682581ca4b80848 \
    --hash=sha256:8fde5f188da2d593bd5bc0be98d9abc46c95bb8a9dde93429570192ee6cc2d4a \
    # via -r requirements.txt, soupsieve
backports.ssl-match-hostname==3.7.0.1 \
    --hash=sha256:bb82e60f9fbf4c080eabd957c39f0641f0fc247d9a16e31e26d594d8f42b9fd2 \
    # via -r requirements.txt, tornado
bcrypt==3.1.7 \
    --hash=sha256:0258f143f3de96b7c14f762c770f5fc56ccd72f8a1857a451c1cd9a655d9ac89 \
    --hash=sha256:0b0069c752ec14172c5f78208f1863d7ad6755a6fae6fe76ec2c80d13be41e42 \
    --hash=sha256:19a4b72a6ae5bb467fea018b825f0a7d917789bcfe893e53f15c92805d187294 \
    --hash=sha256:5432dd7b34107ae8ed6c10a71b4397f1c853bd39a4d6ffa7e35f40584cffd161 \
    --hash=sha256:6305557019906466fc42dbc53b46da004e72fd7a551c044a827e572c82191752 \

(6)在其他机器利用依赖文件创建虚拟环境

mkvirtualenv new

pip install -r requirements.txt

# ===>
adding trusted host: 'mirrors.aliyun.com' (from line 9 of requirements.txt)
adding trusted host: '192.168.1.80' (from line 10 of requirements.txt)
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/, http://192.168.1.80:8080/
Collecting amqp==1.4.9
  Using cached https://mirrors.aliyun.com/pypi/packages/ed/09/314d2788aba0aa91f2578071a6484f87a615172a98c309c2aad3433da90b/amqp-1.4.9-py2.py3-none-any.whl (51 kB)
Collecting anyjson==0.3.3

补充:此处使用 pip innstall -r xxx.txt 时,如果xxx.txt中存在hash值,将会自动校验,无需手动开启。


版权声明 本文属于本站  原创作品,文章版权归本站及作者所有,请尊重作者的创作成果,转载、引用自觉附上本文永久地址: http://blog.lujianxin.com/x/art/5vj43raevyt4

文章评论区

作者名片

图片丢失
  • 作者昵称:Jeyrce.Lu
  • 原创文章:61篇
  • 转载文章:3篇
  • 加入本站:1830天

站点信息

  • 运行天数:1831天
  • 累计访问:164169人次
  • 今日访问:0人次
  • 原创文章:69篇
  • 转载文章:4篇
  • 微信公众号:第一时间获取更新信息