平时导出依赖一般都是 pip freeze > requirements.txt
这种方式导出的是当前python环境中所有的包,只会多不会少,有些库不是必需的也跟着导出来,冗余过重。
这个时候 pipreqs
这个库就派上用场了,它只会导出当前项目运行所依赖的包,环境中多余的库就不会导出,在迁移环境时候很少方便。
pip install pipreqs
# 使用的时候也很简单,进入项目的根目录
pipreqs --pypi-server http://mirrors.aliyun.com/pypi/simple/ ./
# 等待一会就会生成一个 requirements.txt 文件
异常
D:\script>pipreqs ./
Traceback (most recent call last):
File "c:\users\46069\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\46069\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\46069\AppData\Local\Programs\Python\Python39\Scripts\pipreqs.exe\__main__.py", line 7, in <module>
File "c:\users\46069\appdata\local\programs\python\python39\lib\site-packages\pipreqs\pipreqs.py", line 470, in main
init(args)
File "c:\users\46069\appdata\local\programs\python\python39\lib\site-packages\pipreqs\pipreqs.py", line 406, in init
candidates = get_all_imports(input_path,
File "c:\users\46069\appdata\local\programs\python\python39\lib\site-packages\pipreqs\pipreqs.py", line 122, in get_all_imports
contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 96: illegal multibyte sequence
报错的时候需要调整下文件编码 直接修改pipreqs.py 的75行,将encoding改为'utf-8'
D:\script>pipreqs ./
INFO: Successfully saved requirements file in ./requirements.txt
D:\script>