pipreqs簡介
項目開發的過程中, 避免不了搭建和部署開發環境, 而搭建和部署開發環境需要項目依賴的python第三方包, 如何獲取一個項目中所需依賴的python第三方包, 這就需要使用pipreqs包, 它可以自動生成項目中依賴的第三方包, 並能生成requirements.txt
文件, 方便在搭建和部署項目開發環境時安裝依賴包.
pipreqs安裝
pip install pipreqs
可以使用--help
參數, 查看pipreqs支持的參數
pipreqs - Generate pip requirements.txt file based on imports
Usage:
pipreqs [options] [<path>]
Arguments:
<path> The path to the directory containing the application
files for which a requirements file should be
generated (defaults to the current working
directory).
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, each separated by a comma.
--no-follow-links Do not follow symbolic links in the project
--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.
pipreqs與freeze的區別
pipreqs對項目目錄進行掃描, 發現使用了哪些第三方包, 自動生成依賴包列表. 但有時候可能會有一點偏差, 需要檢查調整一下.
freeze配合虛擬環境使用時效果更好, 因為freeze是把整個環境中的包都列出來, 不論項目中是否使用了, 都會被列出來.
pipreqs使用時遇到的問題
出現UnicodeDecodeError
出現編碼錯誤時, 可以指定編碼格式
pipreqs ./ --encoding=utf-8
出現SyntaxError
出現語法錯誤時, 一般時python2和python3之間的不兼容問題, 可以使用--debug
找到問題文件, 然后--ignore
忽略掉問題文件所在的目錄.
pipreqs ./ --encoding=utf-8 --debug
pipreqs ./ --encoding=utf-8 --ignore dir目錄