Skip to content

Instantly share code, notes, and snippets.

@balloob
Created January 31, 2016 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balloob/c7ff7f91458350b548c7 to your computer and use it in GitHub Desktop.
Save balloob/c7ff7f91458350b548c7 to your computer and use it in GitHub Desktop.
Detect Python virtual environments venv and virtualenv
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'))
@brainstorm
Copy link

Hm, not sure this would work with https://www.continuum.io/anaconda-overview :/

@ticapix
Copy link

ticapix commented Oct 10, 2018

from a shell script,

python -c "import sys; sys.exit(getattr(sys, 'base_prefix', sys.prefix) == sys.prefix)"

and check for $? value. It'll be 0 for being inside a virtualenv

EDIT: tested with python3.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment