平时导出依赖一般都是 pip freeze > requirements.txt
这种方式导出的是当前python环境中所有的包,只会多不会少,有些库不是必需的也跟着导出来,冗余过重。
这个时候 pipreqs 这个库就派上用场了,它只会导出当前项目运行所依赖的包,环境中多余的库就不会导出,在迁移环境时候很少方便。
pip install pipreqs
使用的时候也很简单,进入项目的根目录
pipreqs ./
等待一会就会生成一个requirements.txt
文件
D:PyPt2018ElasticSearch_New>pipreqs ./ Traceback (most recent call last): File "d:anaconda3lib unpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "d:anaconda3lib unpy.py", line 85, in _run_code exec(code, run_globals) File "D:Anaconda3Scriptspipreqs.exe\__main__.py", line 9, in <module> File "d:anaconda3libsite-packagespipreqspipreqs.py", line 396, in main init(args) File "d:anaconda3libsite-packagespipreqspipreqs.py", line 341, in init extra_ignore_dirs=extra_ignore_dirs) File "d:anaconda3libsite-packagespipreqspipreqs.py", line 75, in get_all_ imports contents = f.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0x8d in position 232: illegal multibyte sequence
报错的时候需要调整下文件编码 直接修改pipreqs.py 的75行,将encoding改为'utf-8'
D:PyPt2018ElasticSearch_New>pipreqs ./ INFO: Successfully saved requirements file in ./requirements.txt D:PyPt2018ElasticSearch_New>