Last active
October 9, 2024 05:11
-
-
Save EvilSupahFly/4a453cc8c505ee9b3a1114ec9b7627b1 to your computer and use it in GitHub Desktop.
Python Virtual Environment Manager using PythonZ
This file contains hidden or 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
| RESET="\033[0m" # Normal | |
| RED="\033[1m\033[1;31m" # Red | |
| GREEN="\033[1m\033[1;32m" # Green | |
| WHITE="\033[1m\033[1;37m" # White | |
| pyvenv() { | |
| local venv_name=${1:-$(read -p "Enter the virtual environment name: " name && echo "$name")} | |
| local venv_home="$HOME/python-virtual-environs/$venv_name" | |
| if [[ -z "$venv_name" ]]; then | |
| echo -e "\n${RED}Error: Virtual environment name must be provided.${RESET}\n" | |
| return 1 | |
| fi | |
| if [[ ! -d "$venv_home" ]]; then | |
| echo -e "${RED}$venv_home ${WHITE}doesn't exist: creating now." | |
| local python_version=${2:-$(read -p "Enter the Python version: " version && echo "$version")} | |
| local usepy="$(pythonz locate "$python_version")" | |
| if [[ -z "$usepy" ]]; then | |
| echo -e "\n${WHITE}Python version ${RED}$python_version ${WHITE}is not currently installed.\nInstalling now.\n" | |
| echo -e "${GREEN}pythonz install ${WHITE}$python_version${RESET}\n" | |
| pythonz install "$python_version" | |
| usepy="$(pythonz locate "$python_version")" | |
| fi | |
| echo -e "\n${GREEN}$usepy -m venv \"${WHITE}$venv_home${GREEN}\"\n" | |
| "$usepy" -m venv "$venv_home" | |
| else | |
| local usepy="$venv_home/bin/python3" | |
| local python_version=$("$usepy" --version 2>&1 | awk '{print $2}') | |
| fi | |
| echo -e "${WHITE}\n \$usepy=$usepy" | |
| echo -e " \$venv_name=$venv_name" | |
| echo -e " \$venv_home=$venv_home" | |
| echo -e " \$python_version=$python_version" | |
| echo -e "${GREEN}\n source ${WHITE}$venv_home/bin/activate${RESET}" | |
| if source "$venv_home/bin/activate"; then | |
| echo -e "\nType 'deactivate' to exit this venv." | |
| echo -e "${RESET} " | |
| else | |
| echo -e "\n${RED}Well crap. Looks like something went wrong.${RESET}" | |
| return 1 | |
| fi | |
| } | |
| echo -e "${WHITE}Using PythonZ, function pyvenv starts and enters a Python VENV, creating it if it doesn't exist." | |
| echo -e "If 'venv_name' and 'python_version' are not supplied, pyvenv will ask you to provide them.\n" | |
| echo -e "${GREEN} pyvenv venv_name python_version${RESET}\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment