Skip to content

Instantly share code, notes, and snippets.

@cywf
Created February 25, 2023 16:32
Show Gist options
  • Save cywf/821b88830060798bded966b1b4af688b to your computer and use it in GitHub Desktop.
Save cywf/821b88830060798bded966b1b4af688b to your computer and use it in GitHub Desktop.
Explanation guide to python virtual enviornments

Understanding Python Virtual Enviornments

alt text

venv is a built-in module in Python 3 that allows you to create and manage virtual environments. A virtual environment is a self-contained directory that contains its own Python installation and packages, isolated from the system-wide Python environment. This makes it easier to manage dependencies and ensure that your project runs on the specific version of Python and package versions that you need.

To create a virtual environment using venv, follow these steps:

  1. Open a terminal or command prompt.
  2. Navigate to the directory where you want to create the virtual environment.
  3. Run the following command: python3 -m venv <env_name>, replacing <env_name> with the name you want to give your virtual environment.
  4. Activate the virtual environment by running the appropriate command for your operating system: - On Unix or Linux: source <env_name>/bin/activate - On Windows: <env_name>\Scripts\activate
  5. Once the virtual environment is activated, you can install packages using pip as you normally would. The packages will be installed only in the virtual environment, not in the system-wide Python environment.
  6. When you are finished working in the virtual environment, you can deactivate it by running the command deactivate.

Here is an example of how you could create a virtual environment named my_env and activate it on Unix or Linux:

cd my_project
python3 -m venv my_env
source my_env/bin/activate

After activating the virtual environment, you can install packages using pip as you normally would, for example:

(my_env) $ pip install numpy

When you are finished working in the virtual environment, you can deactivate it by running the command deactivate:

(my_env) $ deactivate

You can create a bash script that prompts the user with a menu system to set up and configure their virtual development environment using venv. Here's an example script that you can use:

#!/bin/bash

echo "Virtual Environment Setup Menu"
echo "=============================="
echo "1. Create a new virtual environment"
echo "2. Activate an existing virtual environment"
echo "3. Deactivate the current virtual environment"
echo "4. Exit"

read -p "Enter your choice: " choice

case $choice in
  1) # Create a new virtual environment
    read -p "Enter the name of your virtual environment: " env_name
    python3 -m venv $env_name
    echo "Virtual environment created: $env_name"
    ;;
  2) # Activate an existing virtual environment
    read -p "Enter the name of the virtual environment to activate: " env_name
    source $env_name/bin/activate
    echo "Virtual environment activated: $env_name"
    ;;
  3) # Deactivate the current virtual environment
    deactivate
    echo "Virtual environment deactivated"
    ;;
  4) # Exit
    exit 0
    ;;
  *) # Invalid option
    echo "Invalid choice: $choice"
    exit 1
    ;;
esac

This script presents a menu of options to the user, allowing them to create a new virtual environment, activate an existing one, deactivate the current one, or exit the script. When the user selects an option, the script prompts them for any additional information needed, such as the name of the virtual environment.

To use this script as an alias, you can add the following line to your .bashrc or .bash_profile file:

alias venv-setup='/path/to/venv-setup.sh'

Replace /path/to/venv-setup.sh with the actual path to the script file. Once you have added the alias, you can run venv-setup from the command line to start the script.

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