Skip to content

Instantly share code, notes, and snippets.

@cupdike
Created July 8, 2022 17:27
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 cupdike/6a9caaf18f30250364c8fcf6d64ff22e to your computer and use it in GitHub Desktop.
Save cupdike/6a9caaf18f30250364c8fcf6d64ff22e to your computer and use it in GitHub Desktop.
Python Disposable Virtual Environment Using Venv Package
function venvtemp {
# Inspired by: https://gist.github.com/csinchok/9714005#file-bash_profile
THROWAWAY_DIR=$(mktemp -d -t venv);
cd $THROWAWAY_DIR;
python3 -m venv venv;
source venv/bin/activate;
printf "\n\nMaybe run:\n\tpython -m pip install --upgrade pip wheel setuptools\n\n"
printf "Consider:\n\texport PIP_DEFAULT_TIMEOUT=100 && python -m pip install <PACKAGES> \\\t
--trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org\n\n"
}
@cupdike
Copy link
Author

cupdike commented Jul 8, 2022

Some different design choices from:
https://gist.github.com/csinchok/9714005#file-bash_profile

  • Using venv package
  • Not removing the environment (reboots should wipe them out)
  • Adding some usage hints (specific to my needs... YMMV)

@cupdike
Copy link
Author

cupdike commented Jul 12, 2022

And actually, I figured why not use this for permanent virtualenvs by having it take an optional directory name (but still create on in a temp dir if param is not provided:

venvmake () {
	if [[ "$1" != "" ]]
	then
		local venv_dir=$1 
		mkdir "$venv_dir"
	else
		local venv_dir="$(mktemp -d -t venv)" 
	fi
	cd $venv_dir
	python3 -m venv venv
	source venv/bin/activate
	printf "\n\nMaybe run:\n\tpython -m pip install --upgrade pip wheel setuptools\n\n"
	printf "Consider:\n\texport PIP_DEFAULT_TIMEOUT=100 && python -m pip install <PACKAGES> \\\t  --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org\n\n"
}

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