Skip to content

Instantly share code, notes, and snippets.

@Enchan1207
Created July 5, 2023 05:12
Show Gist options
  • Save Enchan1207/4a07dffe3d0f927c4fcdf242f4f6b477 to your computer and use it in GitHub Desktop.
Save Enchan1207/4a07dffe3d0f927c4fcdf242f4f6b477 to your computer and use it in GitHub Desktop.
仮想環境かどうか判定する
def is_virtual_env() -> bool:
"""venv, virtualenvその他仮想環境で動いているかどうかを調べる
Returns:
bool: 仮想環境ならTrue、そうでなければFalse
"""
# base_prefixはインストール元のpython, prefixは仮想環境のpythonを指すパス
# - https://docs.python.org/ja/3/library/sys.html#sys.base_prefix
# - https://docs.python.org/ja/3/library/sys.html#sys.prefix
# これらの値が同一でなければ、仮想環境での実行と判断する
return sys.base_prefix != sys.prefix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment