python 只導出項目依賴包

平時導出依賴通常都是 pip freeze >  requirements.txt  python

這種方式導出的是當前python環境中全部的包,只會多不會少,有些庫不是必需的也跟着導出來,冗餘太重。shell

這個時候  pipreqs 這個庫就派上用場了,它只會導出當前項目運行所依賴的包,環境中多餘的庫就不會導出,在遷移環境時候不多方便。ui

pip install pipreqs

使用的時候也很簡單,進入項目的根目錄

pipreqs ./

等待一會就會生成一個 requirements.txt  文件


D:\PyPt\2018\ElasticSearch_New>pipreqs ./ Traceback (most recent call last): File "d:\anaconda3\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "d:\anaconda3\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "D:\Anaconda3\Scripts\pipreqs.exe\__main__.py", line 9, in <module> File "d:\anaconda3\lib\site-packages\pipreqs\pipreqs.py", line 396, in main init(args) File "d:\anaconda3\lib\site-packages\pipreqs\pipreqs.py", line 341, in init extra_ignore_dirs=extra_ignore_dirs) File "d:\anaconda3\lib\site-packages\pipreqs\pipreqs.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:\PyPt\2018\ElasticSearch_New>pipreqs ./ INFO: Successfully saved requirements file in ./requirements.txt D:\PyPt\2018\ElasticSearch_New>
相關文章
相關標籤/搜索