生成requirements.txt的方法
第一种 是使用freeze
# 安装
pip install freeze
#生成requirements文件
pip freeze > requirements.txt
此方法是将此环境所有的依赖包全部生成加入,及时在当前项目中未成使用的模块,
第二种 是使用pipreqs
# 安装
pip install pipreqs
# 在项目根目录下执行命令
pipreqs . --encoding=utf8 --force
or
pipreqs ./ --encoding=utf8 --force
# 相关参数
Usage:
pipreqs [options] <path>
Options:
--use-local Use ONLY local package info instead of querying PyPI
--pypi-server <url> Use custom PyPi server
--proxy <url> 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
--ignore <dirs>... Ignore extra directories
--encoding <charset> Use encoding parameter for file open
--savepath <file> Save the list of requirements in the given file
--print Output the list of requirements in the standard output
--force Overwrite existing requirements.txt
--diff <file> Compare modules in requirements.txt to project imports.
--clean <file> Clean up requirements.txt by removing modules that are not imported in project.
--no-pin Omit version of output packages.
此方法将当前环境所用的模块生成加入,未使用的模块则不会加入到requirements文件。
如果想查看源码,可以通过github地址为: https://github.com/bndr/pipreqs 进行查看
另外 其中的–encoding=utf8 为编码设置(此处使用的是utf8编码),有的系统可能会报UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xae in position 406: illegal multibyte sequence 的错误。可根据情况进行设置
安装requirements.txt方法
pip install -r requirements.txt