Skip to content

Instantly share code, notes, and snippets.

@pulkitgoyal56
Last active July 10, 2021 02:30
Show Gist options
  • Save pulkitgoyal56/95a4343c83e039067f9874da04947c64 to your computer and use it in GitHub Desktop.
Save pulkitgoyal56/95a4343c83e039067f9874da04947c64 to your computer and use it in GitHub Desktop.
Check if Python interpreter is from a virtual environment
import sys
def get_base_prefix_compat():
"""Get base/real prefix, or sys.prefix if there is none."""
return getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix
def in_virtualenv():
return get_base_prefix_compat() != sys.prefix
in_virtualenv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment