Last active
May 7, 2020 19:38
-
-
Save chrisjbillington/f0dce8cfb31a23ac878b0e88a6618677 to your computer and use it in GitHub Desktop.
venv activate snippet
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
#! /usr/bin/python | |
import sys | |
from pathlib import Path | |
__doc__ = """Print the path of the activate script for a venv found in the current | |
directory or an ancestor directory. Intended use is to put this in your $PATH and add a | |
function to your bashrc like: | |
activate(){ | |
activate_script="$(find-venv-activate)" || return | |
source "$activate_script" | |
} | |
Then you can type "activate" from some project folder within some subdirectory of | |
wherever the venv is. | |
""" | |
path = Path().absolute() | |
while True: | |
for name in path.iterdir(): | |
try: | |
if (path / name / 'pyvenv.cfg').exists(): | |
print(path / name / 'bin' / 'activate') | |
sys.exit(0) | |
except PermissionError: | |
pass | |
if path.parent == path: | |
sys.stderr.write("No venv found in any parent directory\n") | |
sys.exit(1) | |
path = path.parent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment