系统环境:
OS Platform Ubunut 16
Python 3.7.2
描述问题:
在对项目进行打包时,经常需要编写requirements.txt(一个项目所需要的模块)
当项目太大的时候,手动添加模块和对应的版本的工作量就大起来了。
问题:
python自动python 导出一个项目所需要的模块及其对应版本
解决方案:
- 使用 pip freeze
pip freeze > requirements.txt
这种方式配合virtualenv才好使,否则把整个环境中的包都列出来了。
- 使用 pipreqs
这个工具的好处是可以通过对项目目录的扫描,自动发现使用了那些类库,自动生成依赖清单。
pip install pipreqs
cd $project_location
pipreqs ./
INFO: Successfully saved requirements file in ./requirements.txt
如果你是window环境,
运行pipreqs ./ 可能会报错:
UnicodeDecodeError: ‘gbk’ codec
只需将 pipreqs ./ 修改为
pipreqs ./ --encoding=utf-8