Created
July 5, 2023 05:12
-
-
Save Enchan1207/4a07dffe3d0f927c4fcdf242f4f6b477 to your computer and use it in GitHub Desktop.
仮想環境かどうか判定する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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