モチベーションMotivation
- Python2.X系とPython3.X系やpipとpip3を共存させて,必要に応じて切り替えて使いたい.Want to switch major version of Python (i.e. between Python 2 and Python 3).
- Pythonのマイナーバージョンを細かく切り替えたい.Want to switch minor version of Python (i.e. between Python3.A and Python3.B).
- pipでパッケージをインストールすると環境が汚れてしまうのでプロジェクトごとにパッケージを管理したい.Want to isolate packages for every project.
- 「プロジェクトごとにDockerコンテナを立ち上げてそこでPythonを実行する」というような大げさな管理はしたくない.Don’t want to spin up a new Docker container for each Python project.
pyenvでPythonのバージョンを切り替えるHow to switch any version of Python with pyenv
-
デフォルトのPythonのバージョンを確認するConfirm the default Python version.
$ python --version Python 2.7.16 -
brewでpyenvをインストールするInstall pyenv viabrew.$ brew install pyenv -
PATHなどを設定するSet the environment variable and add
pyenv initto your shell.export PYENV_ROOT="${HOME}/.pyenv" export PATH=${PYENV_ROOT}/bin:$PATH eval "$(pyenv init -)" -
pyenvで使用可能なPythonのバージョン一覧を確認するSee installable Python versions.
$ pyenv install -l Available versions: 2.1.3 2.2.3 2.3.7 : : -
任意のバージョンをインストールするInstall a specific Python version.
$ pyenv install 3.9.0 : Installed Python-3.9.0 to /Users/xxxx/.pyenv/versions/3.9.0 -
切替可能なバージョンを確認するSee the current and installed versions.
$ pyenv versions \* system (set by /Users/xxxx/.pyenv/version) 3.9.0
システム全体のPythonバージョンを切り替えるSwitch the global Python version
Switch to a specific version globally.
$ pyenv global 3.9.0
$ pyenv versions
system
\* 3.9.0 (set by /Users/xxxx/.pyenv/version)
$ python --version
Python 3.9.0
$ pip -V
pip 20.2.3 from /Users/xxxx/.pyenv/versions/3.9.0/lib/python3.9/site-packages/pip (python 3.9)
特定のディレクトリ内のみに特定のPythonバージョンを設定するSwitch the local Python version
Apply a specific Python version to a specific directory only.
$ mkdir sandbox; cd sandbox
$ pyenv versions
\* system
3.9.0 (set by /Users/xxxx/.pyenv/version)
$ python --version
Python 2.7.16
$ pyenv local 3.9.0
$ cat .python-version
3.9.0
$ pyenv versions
system
\* 3.9.0 (set by /Users/xxxx/sandbox/.python-version)
$ python --version
Python 3.9.0
venvでパッケージを分離するIsolate packages with venv
-
pyenvでPythonを任意のバージョンに切り替えるSwitch to a specific Python version with pyenv.
-
インストール済みのパッケージを確認するSee installed packages.
$ pip list Package Version ---------- ------- pip 20.3.3 setuptools 49.2.1 -
仮想環境
<target directory>を作成するCreate a virtual environment.$ python -m venv <target directory> $ ls <target directory> bin include lib pyvenv.cfg -
仮想環境を実行するActivate the virtual environment.
$ source <target directory>/bin/activate (target directory name) $ -
何かしらのパッケージをインストールし,仮想環境にパッケージがインストールされたことを確認するInstall a package and confirm it is installed in the virtual environment.
$ pip install selenium : Successfully installed selenium... $ pip freeze selenium==3.141.0 urllib3==1.26.2 -
仮想環境を終了するDeactivate the virtual environment.
(target directory name) $ deactivate $ -
元の環境と仮想環境のパッケージが分離されていることを確認するConfirm packages are isolated between the original and virtual environments.
$ pip freeze $
pip freezeとpip listの違いDifference between pip freeze and pip list
pip listは,その環境にインストールされているパッケージをすべて出力する.一方,pip freezeは,setuptoolsなどの内部的なパッケージ群は出力せず,ユーザが本来必要としているパッケージ名とそのバージョンの一覧を出力する.pip listshows all packages installed in the environment.pip freezeomits internal packages likesetuptoolsand outputs only the packages the user actually needs.pip freeze > requirements.txtにて,パッケージ名とバージョン一覧をrequirements.txtに書き出し,pip install -r requirements.txtを実行することで,パッケージ一覧を一括でインストールすることができる.pip freeze > requirements.txtsaves the package list torequirements.txt, which can then be installed in bulk withpip install -r requirements.txt.