Python問題集を解いてみよう!!

【Python】Pipenv の仮想環境 にインストールした外部ライブラリをrequirements.txt に出力したい【Pipenv】

当ページのリンクには広告が含まれています。
Pipenv の仮想環境 にインストールした外部ライブラリをrequirements.txt に出力したい

Pipenv で作成した仮想環境にインストールした外部ライブラリを requirements.txt に出力したいことがあって、方法を検索していたのですが、

pipenv lock -r > requirements.txt

と記載されているサイトがけっこう多いのですが、これを試すと、

Usage: pipenv lock [OPTIONS]
Try 'pipenv lock -h' for help.

Error: No such option: -r

とエラーになってしまいます。

エラーの指摘通り、pipenv lockコマンドのヘルプを確認すると、

Usage: pipenv lock [OPTIONS]

  Generates Pipfile.lock.

Options:
  --categories TEXT
  --dev-only             Emit development dependencies *only* (overrides
                         --dev)
  -d, --dev              Generate both develop and default requirements  [env
                         var: PIPENV_DEV]
  --extra-pip-args TEXT
  --pre                  Allow pre-releases.
  --python TEXT          Specify which version of Python virtualenv should
                         use.
  --clear                Clears caches (pipenv, pip).  [env var: PIPENV_CLEAR]
  -q, --quiet            Quiet mode.
  -v, --verbose          Verbose mode.
  --pypi-mirror TEXT     Specify a PyPI mirror.
  -h, --help             Show this message and exit.

とあり、-rオプションはありません。

仕様が変わったんでしょうかね。

pipenvコマンドの方のヘルプを確認すると、

pipenv -h (長めです)
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where                         Output project home information.
  --venv                          Output virtualenv information.
  --py                            Output Python interpreter information.
  --envs                          Output Environment Variable options.
  --rm                            Remove the virtualenv.
  --bare                          Minimal output.
  --man                           Display manpage.
  --support                       Output diagnostic information for use in
                                  GitHub issues.
  --site-packages / --no-site-packages
                                  Enable site-packages for the virtualenv.
                                  [env var: PIPENV_SITE_PACKAGES]
  --python TEXT                   Specify which version of Python virtualenv
                                  should use.
  --clear                         Clears caches (pipenv, pip).  [env var:
                                  PIPENV_CLEAR]
  -q, --quiet                     Quiet mode.
  -v, --verbose                   Verbose mode.
  --pypi-mirror TEXT              Specify a PyPI mirror.
  --version                       Show the version and exit.
  -h, --help                      Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check         Checks for PyUp Safety security vulnerabilities and against
                PEP 508 markers provided in Pipfile.
  clean         Uninstalls all packages not specified in Pipfile.lock.
  graph         Displays currently-installed dependency graph information.
  install       Installs provided packages and adds them to Pipfile, or (if no
                packages are given), installs all packages from Pipfile.
  lock          Generates Pipfile.lock.
  open          View a given module in your editor.
  requirements  Generate a requirements.txt from Pipfile.lock.
  run           Spawns a command installed into the virtualenv.
  scripts       Lists scripts in current environment config.
  shell         Spawns a shell within the virtualenv.
  sync          Installs all packages specified in Pipfile.lock.
  uninstall     Uninstalls a provided package and removes it from Pipfile.
  update        Runs lock, then sync.
  upgrade       Resolves provided packages and adds them to Pipfile, or (if no
                packages are given), merges results to Pipfile.lock
  verify        Verify the hash in Pipfile.lock is up-to-date.

とあり、requirementsコマンドがあるじゃないですか。

説明も「Generate a requirements.txt from Pipfile.lock.」とあるので、求めていたもののようです。

ということで、

pipenv requirements > requirements.txt 

を実行することで無事に requirements.txt に出力することができました。

--devオプションもあるので、仕様が変わったようですね。

ちなみに、私の環境は Mac で、Homebrew を使って Pipenv をインストールしています。

Pipenv のバージョンは「2023.7.23」でした。

以上です。

スポンサーリンク

Pipenv の仮想環境 にインストールした外部ライブラリをrequirements.txt に出力したい

この記事が気に入ったら
フォローしてね!

よかったらシェアしてね!
  • URLをコピーしました!

コメント

コメントする

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

目次