一起開發項目的時候總是要搭建環境和部署環境的,這個時候必須得有個python第三方包的list,一般都叫做requirements.txt。 如果一個項目使用時virtualenv環境,還好辦 pip freeze 就可以解決,但是如果一個項目的依賴list沒有維護,而且又是環境混用,那就不好整理的呀,不過,這里安利一個工具 pipreqs,可以自動根據源碼生成 requirements.txt .
使用pip freeze
$ pip freeze > requirements.txt
這種方式配合virtualenv 才好使,否則把整個環境中的包都列出來了。
使用 pipreqs
這個工具的好處是可以通過對項目目錄的掃描,自動發現使用了那些類庫,自動生成依賴清單。
缺點是可能會有些偏差,需要檢查並自己調整下。
# pip install pipreqs # 使用方式也比較簡單 pipreqs ./ INFO: Starting new HTTPS connection (1): pypi.python.org INFO: Starting new HTTPS connection (1): pypi.python.org INFO: Starting new HTTPS connection (1): pypi.python.org INFO: Starting new HTTPS connection (1): pypi.python.org INFO: Starting new HTTPS connection (1): pypi.python.org INFO: Starting new HTTPS connection (1): pypi.python.org INFO: Successfully saved requirements file in ./requirements.txt cat requirements.txt Django == 1.6 django_redis == 4.3.0 redis == 2.10.5 django_redis_cache == 1.6.5 simplejson == 3.8.2 Twisted == 16.0.0 pycrypto == 2.6.1 ConcurrentLogHandler == 0.9.1 cx_Oracle == 5.2.1 ujson == 1.35 # 安裝 pip install [-r] requirements.txt