Skip to content

Instantly share code, notes, and snippets.

@mrmurphy
Created June 21, 2013 19:14
Show Gist options
  • Select an option

  • Save mrmurphy/5833589 to your computer and use it in GitHub Desktop.

Select an option

Save mrmurphy/5833589 to your computer and use it in GitHub Desktop.
A little python script to find a .venv file in a parent directory and activate that virtual environment. Requirements: virtualenv virtualenvwrapper Notes: Does not search above the user's home directory.
#! /usr/bin/python
import os
HOME = os.getenv("HOME")
VENVDIR = os.path.join(HOME, ".venvs") # <-- Change this to your venv dir.
VENVFILENAME = ".venv"
def main():
pwd = os.getcwd().split(os.sep)
home = HOME.split(os.sep)
if home[-1] in pwd:
while pwd != home:
lookin = os.path.join(os.sep, *pwd)
if VENVFILENAME in os.listdir(lookin):
with open(os.path.join(lookin, VENVFILENAME), "r") as f:
env = f.read().strip()
return "source {}".format(os.path.join(VENVDIR, env,
"bin", "activate"))
pwd.pop()
return ""
# return "echo 'No {} file found between this directory" \
# " and your home directory.'".format(VENVFILENAME)
if __name__ == '__main__':
print(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment