Created
January 31, 2016 07:16
-
-
Save balloob/c7ff7f91458350b548c7 to your computer and use it in GitHub Desktop.
Detect Python virtual environments venv and virtualenv
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(): | |
""" Return if we run in a virtual environtment. """ | |
# Check supports venv && virtualenv | |
return (getattr(sys, 'base_prefix', sys.prefix) != sys.prefix or | |
hasattr(sys, 'real_prefix')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from a shell script,
python -c "import sys; sys.exit(getattr(sys, 'base_prefix', sys.prefix) == sys.prefix)"
and check for
$?
value. It'll be0
for being inside a virtualenvEDIT: tested with python3.6.