/tmp/log/cha_pppo

i love 玄米パン あんなし

PipenvでPythonの仮想環境を作る

Pythonの仮想環境作るのはPipenvだけで良いのでは。

Environment

pipenv, version 2018.11.26

Install

option

インストール済である場合は virtualenv, virtualenvwrapper を削除。

brew uninstall pyenv-virtualenv
pip uninstall package virtualenvwrapper

pipenvのinstall。

pip install pipenv

仮想環境の作成。

cd /path/to/project
pipenv --three
Using /usr/local/opt/python/libexec/bin/python (3.7.4) to create virtualenv…
✔ Successfully created virtual environment!
Virtualenv location: /Users/user/.local/share/virtualenvs/x

# specify version 3.7
pipenv --python 3.7

pip packageのinstall。

pipenv install requests
Installing requests…
Adding requests to Pipfile's [packages]…
✔ Installation Succeeded

# install from requirements.txt
pipenv install -r ./requirements.txt

開発環境のみ利用するpackage類のinstall。

pipenv install --dev requests

pipenvでinstallしたpackageの確認。

pipenv graph
requests==2.22.0
  - certifi [required: >=2017.4.17, installed: 2019.9.11]
  - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
  - idna [required: >=2.5,<2.9, installed: 2.8]
  - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.3]

installしたpackageの脆弱性を確認。

pipenv check
Checking PEP 508 requirements…
Passed!
Checking installed package safety…
All good!

仮想環境でsubshellを起動

pipenv shell
Launching subshell in virtual environment…
 . /Users/user/.local/share/virtualenvs/x/bin/activate

仮想環境のrestore

Pipfile からrestore。

pipenv install

# install packages for dev env
pipenv install --dev

Pipfile.lock からrestore。

pipenv sync
pipenv sync --dev

Custom script shortcuts

Pipenvに [scripts] を定義し、カスタムショートカットを定義可能。

pipenv run <shortcut>

詳しくは https://docs.pipenv.org/en/latest/advanced/#custom-script-shortcuts

仮想環境の削除

pipenv --rm

Trouble shooting

pipenv実行時Locking Failed

Pipfile, Pipfile.lock を作り直してみる。

pipenv clean
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project…
Pipfile: /path/to/Pipfile
Using /usr/local/opt/python/libexec/bin/python (3.7.4) to create virtualenv…
⠼ Creating virtual environment...Already using interpreter /usr/local/opt/python/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/x/.local/share/virtualenvs/x-ajQOom-E/bin/python3.7
Also creating executable in /Users/x/.local/share/virtualenvs/x-ajQOom-E/bin/python
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter /usr/local/opt/python/libexec/bin/python

✔ Successfully created virtual environment!
Virtualenv location: /Users/x/.local/share/virtualenvs/x-ajQOom-E
Pipfile.lock (182788) out of date, updating to (b786b5)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✘ Locking Failed!
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/resolver.py", line 69, in resolve
[pipenv.exceptions.ResolutionFailure]:       req_dir=requirements_dir
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/utils.py", line 726, in resolve_deps
[pipenv.exceptions.ResolutionFailure]:       req_dir=req_dir,
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/utils.py", line 480, in actually_resolve_deps
[pipenv.exceptions.ResolutionFailure]:       resolved_tree = resolver.resolve()
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/utils.py", line 395, in resolve
[pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches io
[pipenv.exceptions.ResolutionFailure]:       No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches io
No versions found
Was https://pypi.org/simple reachable?
[pipenv.exceptions.ResolutionFailure]:       req_dir=requirements_dir
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/utils.py", line 726, in resolve_deps
[pipenv.exceptions.ResolutionFailure]:       req_dir=req_dir,
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/utils.py", line 480, in actually_resolve_deps
[pipenv.exceptions.ResolutionFailure]:       resolved_tree = resolver.resolve()
[pipenv.exceptions.ResolutionFailure]:   File "/usr/local/lib/python3.7/site-packages/pipenv/utils.py", line 395, in resolve
[pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches io
[pipenv.exceptions.ResolutionFailure]:       No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches io
No versions found
Was https://pypi.org/simple reachable?

deactivate後、再度activate出来ない

subshellを抜ける為には deactivate ではなく、sessionを exit 等で閉じる必要がある。

pipenv shell
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.

exit
pipenv shell

Pythonをバージョン指定してインストール出来ない

pipenv --python 3.6.4

Warning: Python 3.6.4 was not found on your system…
You can specify specific versions of Python with:
  $ pipenv --python path/to/python

対象のバージョンをインストールし、パスを指定して再度仮想環境を作成する。

pyenv install 3.6.4
pyenv local 3.6.4

pipenv --python /Users/h.okamoto/.pyenv/versions/3.6.4/bin/python3.6 shell
Using /Users/x/.pyenv/versions/3.6.4/bin/python3.6 (3.6.4) to create virtualenv…
⠦ Creating virtual environment...Already using interpreter /Users/x/.pyenv/versions/3.6.4/bin/python3.6