Python 生成与使用 requirements.txt

方法一:

适用范围:

  • 适用於单虚拟环境,因为这种方式,会将环境中的依赖包全都加入
  • 使用的全局环境,则下载的所有包都会在里面,不管是不时当前项目依赖的
pip freeze > requirements.txt

 

方法二:(推荐)

适用范围:解决方法一 的难题

pipreqs github:地址

# 安装
pip install pipreqs
# 在当前目录生成
pipreqs . --encoding=utf8 --force

注意 :

  • --encoding=utf8 为使用utf8编码,不然可能会报UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 406: illegal multibyte sequence 的错误。
  • --force 强制执行,当 生成目录下的requirements.txt存在时覆盖。

 

使用requirements.txt安装依赖:

pip install -r requirements.txt

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章