Django 导出项目依赖库到 requirements.txt过程解析

时间:2021-05-22

虚拟环境:

使用 pip freeze

pip freeze > requirements.txt# 这种方式推荐配合 virtualenv ,否则会把整个环境中的包都列出来。

只扫描项目目录

使用 pipreqs

这个工具的好处是可以通过对项目目录的扫描,自动发现使用了那些类库,自动生成依赖清单。缺点是可能会有些偏差,需要检查并自己调整下。

# 首先安装 pipreqs# pip install pipreqs# 安装位置在pip所在的目录下# 使用方式也比较简单pipreqs /var/www/projectINFO: Starting new HTTPS connection (1): pypi.python.orgINFO: Starting new HTTPS connection (1): pypi.python.orgINFO: Starting new HTTPS connection (1): pypi.python.orgINFO: Starting new HTTPS connection (1): pypi.python.orgINFO: Starting new HTTPS connection (1): pypi.python.orgINFO: Starting new HTTPS connection (1): pypi.python.orgINFO: Successfully saved requirements file in ./requirements.txtcat requirements.txtDjango == 1.6django_redis == 4.3.0redis == 2.10.5django_redis_cache == 1.6.5simplejson == 3.8.2Twisted == 16.0.0pycrypto == 2.6.1ConcurrentLogHandler == 0.9.1cx_Oracle == 5.2.1ujson == 1.35

用法:

# pip install -r /var/www/project/requriements.txt # 即可安装所有依赖

问题补充

在使用 pipreqs /var/www/project 时可能会遇见:

UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 776: illegal multibyte sequence

解决方法为:

在后面指定编码格式 pipreqs /var/www/project --encoding=utf8

用法:pip install -r requriements.txt 即可安装所有依赖

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章