天天看點

Python項目生成requirements.txt檔案Python項目生成requirements.txt檔案

Python項目生成requirements.txt檔案

我們在寫Python腳本的時候往往會用到很多第三方庫,但是當我們把腳本換個環境之後就需要手動安裝第三方庫,有時候有的第三方庫還需要一些别的依賴。為了省事,我們可以導出一個requirements.txt,把需要安裝的第三方庫放在裡面。下面我們就講一下如何導出這個requirements.txt。

方法一:

pip freeze > requirements.txt
           

這種方法配合virtualenv才好用,否則會把整個環境中的包都列出來。是以往往不适用

方法二:

使用pipreqs,這個工具的好處是可以通過對項目目錄的掃描,發現使用了哪些庫,生成依賴清單。

安裝

pip install pipreqs
           

使用

在項目的根目錄下 使用   pipreqs ./

要注意:Windows系統下直接使用會出現編碼錯誤UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 173: illegal multibyte sequence。是以在使用時要指定編碼格式。

pipreqs ./ --encoding=utf8
           

最後

最後生成出來的requirements.txt,可以根據這個檔案下載下傳所有依賴

pip install -r requriements.txt
           

附上pipreqs的使用說明:

Usage:

    pipreqs [options] <path>

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

    --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.

使用方法:

    pipreqs [options] <path>

選項:

    --use-local                            僅使用本地報資訊而不查詢PyPI

    --pypi-server <url>               使用自定義的PyPI服務

    --proxy <url>                        使用Proxy,參數将傳遞給請求庫。你也可以設定

                                                終端中的環境參數:

                                                 $ export HTTP_PROXY="http://10.10.1.10:3128"

                                                 $ export HTTPS_PROXY="https://10.10.1.10:1080"

    --debug                                 列印調試資訊

    --ignore <dirs>...                   忽略額外的目錄,每個目錄用逗号分隔

    --encoding <charset>           使用編碼參數打開檔案

    --savepath <file>                  儲存給定檔案中的需求清單

    --print                                   輸出标準輸出中的需求清單

    --force                                  覆寫現有的requirements.txt

    --diff <file>                            将requirements.txt中的子產品與項目導入進行比較。

    --clean <file>                        通過删除未在項目中導入的子產品來清理requirements.txt。