Updated:

Python2とPython3を共存させる Manage multiple Python versions with pyenv and isolate packages with venv

モチベーション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

  1. デフォルトのPythonのバージョンを確認するConfirm the default Python version.

    $ python --version
    Python 2.7.16
  2. brewpyenvをインストールするInstall pyenv via brew.

    $ brew install pyenv
  3. PATHなどを設定するSet the environment variable and add pyenv init to your shell.

    export PYENV_ROOT="${HOME}/.pyenv"
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
  4. pyenvで使用可能なPythonのバージョン一覧を確認するSee installable Python versions.

    $ pyenv install -l
    Available versions:
    2.1.3
    2.2.3
    2.3.7
    :
    :
  5. 任意のバージョンをインストールするInstall a specific Python version.

    $ pyenv install 3.9.0
    :
    Installed Python-3.9.0 to /Users/xxxx/.pyenv/versions/3.9.0
  6. 切替可能なバージョンを確認する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

  1. pyenvでPythonを任意のバージョンに切り替えるSwitch to a specific Python version with pyenv.

  2. インストール済みのパッケージを確認するSee installed packages.

    $ pip list
    Package    Version
    ---------- -------
    pip        20.3.3
    setuptools 49.2.1
  3. 仮想環境<target directory>を作成するCreate a virtual environment.

    $ python -m venv <target directory>
    
    $ ls <target directory>
    bin		include		lib		pyvenv.cfg
  4. 仮想環境を実行するActivate the virtual environment.

    $ source <target directory>/bin/activate
    
    (target directory name) $
    
  5. 何かしらのパッケージをインストールし,仮想環境にパッケージがインストールされたことを確認する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
  6. 仮想環境を終了するDeactivate the virtual environment.

    (target directory name) $ deactivate
    
    $
  7. 元の環境と仮想環境のパッケージが分離されていることを確認するConfirm packages are isolated between the original and virtual environments.

    $ pip freeze
    $

pip freezepip listの違いDifference between pip freeze and pip list

  • pip listは,その環境にインストールされているパッケージをすべて出力する.一方,pip freezeは,setuptoolsなどの内部的なパッケージ群は出力せず,ユーザが本来必要としているパッケージ名とそのバージョンの一覧を出力する.pip list shows all packages installed in the environment. pip freeze omits internal packages like setuptools and outputs only the packages the user actually needs.
  • pip freeze > requirements.txtにて,パッケージ名とバージョン一覧をrequirements.txtに書き出し,pip install -r requirements.txtを実行することで,パッケージ一覧を一括でインストールすることができる.pip freeze > requirements.txt saves the package list to requirements.txt, which can then be installed in bulk with pip install -r requirements.txt.

関連記事 Related Posts

B!