PEP 405 -- Python Virtual Environments に Python の仮想環境について書かれています。 Python 3.3 からは venv が標準ツールとなり (但し、推奨は pyvenv
)、Python 3.5 からは venv
が推奨ツールになりました。 venv
の基本的な使い方をメモしておきます。
作業用ディレクトリの作成
作業用のディレクトリを作成します。
mkdir example cd example/
Python 仮想環境の作成
venv
を使って仮想環境を作ります。 構文例は以下の通りです。
python -m venv [ENVNAME] . [ENVNAME]/bin/activate
仮想環境の名前にこだわりがなければ venv
のままでも良いと思います。
python -m venv venv . venv/bin/activate
仮想環境を activate
するとプロンプト表示の先頭に仮想環境名が追加されます。 尚、activate
すると $PATH
環境変数が venv
のディレクトリを参照するよう、追加されます。
(venv) user@localhost:~/example$
仮想環境にインストールされているパッケージ名の表示
仮想環境にインストールしたパッケージを表示した場合は pip freeze
で確認します。
(venv)user@localhost:~/example$ pip install fastapi (venv)user@localhost:~/example$ pip freeze fastapi==0.48.0 pydantic==1.4 starlette==0.12.
グローバルにインストールしたパッケージも含め、表示する場合は pip list
を実行します。
(venv) user@localhost:~/example# pip list Package Version ---------- ------- fastapi 0.48.0 pip 20.0.2 pydantic 1.4 setuptools 45.2.0 starlette 0.12.9
仮想環境の無効化
仮想環境を無効化するには deactivate
を実行します。
(venv) user@localhost:~/example$ deactivate user@localhost:~/example$
参考
venv/bin/activate サンプル
# This file must be used with "source bin/activate" *from bash* # you cannot run it directly deactivate () { # reset old environment variables if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then PATH="${_OLD_VIRTUAL_PATH:-}" export PATH unset _OLD_VIRTUAL_PATH fi if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" export PYTHONHOME unset _OLD_VIRTUAL_PYTHONHOME fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then hash -r fi if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then PS1="${_OLD_VIRTUAL_PS1:-}" export PS1 unset _OLD_VIRTUAL_PS1 fi unset VIRTUAL_ENV if [ ! "${1:-}" = "nondestructive" ] ; then # Self destruct! unset -f deactivate fi } # unset irrelevant variables deactivate nondestructive VIRTUAL_ENV="/root/example/venv" export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH" PATH="$VIRTUAL_ENV/bin:$PATH" export PATH # unset PYTHONHOME if set # this will fail if PYTHONHOME is set to the empty string (which is bad anyway) # could use `if (set -u; : $PYTHONHOME) ;` in bash if [ -n "${PYTHONHOME:-}" ] ; then _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" unset PYTHONHOME fi if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then _OLD_VIRTUAL_PS1="${PS1:-}" if [ "x(venv) " != x ] ; then PS1="(venv) ${PS1:-}" else if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" else PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" fi fi export PS1 fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then hash -r fi