Sun Blog

Back

python 如何生成 requirements.txt 文件Blur image

方法一#

pip freeze > requirements.txt
shell

这种方式会将当前虚拟环境中的所有依赖加入到 requirements.txt 文件 中

可能会存在一些我们项目中没有使用的依赖

不推荐使用

方法二(推荐)#

推荐使用 pipreqs

可以指定我们需要生成 requirements.txt 的项目,只添加项目使用的依赖

# 安装
pip install pipreqs

# 使用
pipreqs /home/project/location
shell

如果出现报错 UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xae in position 406: illegal multibyte sequence

则需要指定项目编码为 utf-8

pipreqs /home/project/location --encoding=utf8--force
shell
python 如何生成 requirements.txt 文件
https://blog.csun.site/blog/2024-12-29-python-generate-requirements-txt-file
Author Sun Xin
Published at December 29, 2024
Comment seems to stuck. Try to refresh?✨