python 项目下生成requirements.txt方法

一、使用pip freeae

pip freeze > requirements.txt

该命令存在局限:

该命令只会生成通过pip install 安装的包

如果没有创建Python的虚拟环境virtualenv,该命令会将环境中所有的包都输出到requirements.txt文件,不管你当前的Project有没有用到这些包

二、使用pipreqs

Pipreqs 只会统计项目使用的包

2.1、安装pipreqs

pip install pipreqs

2.2、使用方法

pipreqs "目录"   #在目录下生成requirements.txt

2.3、特殊说明

2.3.1、Mac 下没法使用
pipreqs               
zsh: command not found: pipreqs

没有找到使用办法

2.3.2、Linux 使用报错
pipreqs ./
Traceback (most recent call last):
  File "/usr/bin/pipreqs", line 7, in <module>
    from pipreqs.pipreqs import main
  File "/usr/lib/python2.7/site-packages/pipreqs/pipreqs.py", line 51, in <module>
    from pipreqs import __version__
ImportError: cannot import name __version__

解决办法,更换pipreqs版本

# 查看可安装版本
pip install pipreqs==100
You are using pip version 7.1.0, however version 22.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pipreqs==100
  Could not find a version that satisfies the requirement pipreqs==100 (from versions: 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.7, 0.2.8, 0.2.9, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3.9, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.4.7, 0.4.8, 0.4.9, 0.4.10, 0.4.11)
No matching distribution found for pipreqs==100

# 选择某一版本安装
pip install pipreqs==0.4.0

# 查看能否使用
pipreqs --help
pipreqs - Generate pip requirements.txt file based on imports

Usage:
    pipreqs [options] <path>

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server         Use custom PyPi server
    --proxy               Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --force               Overwrite existing requirements.txt


2.3.3、Windows下使用
pipreqs "目录" --encoding=utf8

三、更换环境后安装requirements.txt

pip install -r requirements.txt

四、参考文档

1、https://www.cnblogs.com/kai-/p/15572539.html

2、https://blog.csdn.net/forGemini/article/details/124098636

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值