Created
June 25, 2019 18:41
-
-
Save gwpantazes/e6b24e0dd90f1acda9431e37a8c0ff2c to your computer and use it in GitHub Desktop.
Check if you are in a Python Virtual Environment
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
# 100% surefire way to detect a virtual environment: Check for the real_prefix system attribute. | |
echo $(python <<EOF | |
import sys | |
if(hasattr(sys, 'real_prefix')): | |
print("Yes, you are in a virtual environment. (" + getattr(sys, 'real_prefix') + ")") | |
else: | |
print("No, you are not in a virtual environment.") | |
EOF | |
) | |
# Not-as-good way, documented for posterity. See https://stackoverflow.com/a/1883251/2291928 | |
# The $VIRTUAL_ENV environment variable gets set by the virtualenv active script, | |
# but that's not 100% reliable since you can start python in a virtual environment manually without activate. | |
# echo $VIRTUAL_ENV |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the script. However, it doesn't seem to work with new Python versions and latest
virtualenv
. For my needs, I have modified it to:To ensure support for older versions as well, look at https://stackoverflow.com/a/1883251/793930.