Virtual Environments
In this section you are going to build a virtual environment using python
, and then learn how to use an install.sh
to automate this process.
Step 1: Verify Python is installed
-
Open VSCode, and open the terminal and run
This command checks if Python 3 is installed on your system. If it returns a version number, you're good to go.
On some systems, you may need to run:
Step 2: Create a Virtual Environment
-
Choose a name for your virtual environment (for example,
myenv
) and run:This command creates a directory named myenv containing a self-contained Python installation.
Step 3: Activate the Virutal Envionment
-
Activation is necessary so that when you install packages, they go into this isolated environment rather than your system-wide Python installation.
-
Linux/MacOS :
-
On Windows (Command Prompt):
-
On Windows (Bash):
-
On Windows (Powershell):
-
-
After activation, your command prompt should change (usually showing the environment name), indicating that you're now working inside
myenv
-
You can verify by runnning
pip list
, you should only have one package
Step 4: Install Packages
-
Once your virtual environment is activated, you can install any packages you need. For example:
-
If you later decide to track these dependencies, you can create a
requirements.txt
file by running:
Step 5: Deactivate the Virtual Environment
-
When you’re finished working, simply run:
This returns you to your system's default Python environment.
Step 6: Install.sh
Step 6.1: Initial Setup and OS Detection
-
Create a new file outside of the venv/ called
install.sh
-
Begin your script with a shebang and error handling, then detect the operating system.
#!/usr/bin/env bash set -e # Detect the operating system. OS_TYPE=$(uname -s) echo "Detected OS: $OS_TYPE"
Step 6.2: Checking for Python Installation
-
Ensure that Python (preferably Python 3) is installed before proceeding.
# Check if Python 3 is available. if command -v python3 &> /dev/null; then PYTHON=python3 elif command -v python &> /dev/null; then PYTHON=python else echo "Python is not installed. Please install Python and try again." exit 1 fi echo "Using Python executable: $PYTHON"
Step 6.3: Creating the Virtual Environment
-
Define a directory for your virtual environment and create it if it doesn’t already exist.
# Define the directory for the virtual environment. ENV_DIR="venv" # Create the virtual environment if it doesn't already exist. if [ -d "$ENV_DIR" ]; then echo "Virtual environment directory '$ENV_DIR' already exists." else echo "Creating virtual environment in '$ENV_DIR'..." $PYTHON -m venv "$ENV_DIR" fi
Step 6.4: Determining the pip Executable Path
-
Determine the correct path to the
pip
executable based on the operating system.# Determine the correct pip executable based on OS. if [ "$OS_TYPE" = "Darwin" ] || [ "$OS_TYPE" = "Linux" ]; then PIP="$ENV_DIR/bin/pip" elif [[ "$OS_TYPE" == MINGW* || "$OS_TYPE" == CYGWIN* || "$OS_TYPE" == MSYS* ]]; then PIP="$ENV_DIR\\Scripts\\pip.exe" else echo "Unknown OS. Cannot determine pip path." exit 1 fi
Step 6.5: Installing Packages from requirements.txt
-
If a
requirements.txt
file exists in the directory, use it to installpip
packages. If you remember we had one from Step 4.# Check if requirements.txt exists and install the packages if it does. if [ -f "requirements.txt" ]; then echo "Installing pip packages from requirements.txt..." $PIP install -r requirements.txt else echo "requirements.txt not found. Skipping package installation." fi echo "Pip package installation complete."
Step 6.6: Displaying Activation Instructions
-
Provide the user with the correct command to activate the virtual environment
# Provide activation instructions based on the OS. if [ "$OS_TYPE" = "Darwin" ] || [ "$OS_TYPE" = "Linux" ]; then echo "To activate the virtual environment, run:" echo " source $ENV_DIR/bin/activate" elif [[ "$OS_TYPE" == MINGW* || "$OS_TYPE" == CYGWIN* || "$OS_TYPE" == MSYS* ]]; then echo "For Windows, run:" echo " $ENV_DIR\\Scripts\\activate" else echo "Unknown OS. Please activate the virtual environment manually." fi echo "Setup complete."
Step 6.7: Installer code
install.sh full code
install.sh full code
#!/usr/bin/env bash
# Exit immediately if any command fails.
set -e
# Detect the operating system.
OS_TYPE=$(uname -s)
echo "Detected OS: $OS_TYPE"
# Check if Python 3 is available.
if command -v python3 &> /dev/null; then
PYTHON=python3
elif command -v python &> /dev/null; then
PYTHON=python
else
echo "Python is not installed. Please install Python and try again."
exit 1
fi
echo "Using Python executable: $PYTHON"
# Define the directory for the virtual environment.
ENV_DIR="venv"
# Create the virtual environment if it doesn't already exist.
if [ -d "$ENV_DIR" ]; then
echo "Virtual environment directory '$ENV_DIR' already exists."
else
echo "Creating virtual environment in '$ENV_DIR'..."
$PYTHON -m venv "$ENV_DIR"
fi
# Determine the correct pip executable based on OS.
if [ "$OS_TYPE" = "Darwin" ] || [ "$OS_TYPE" = "Linux" ]; then
PIP="$ENV_DIR/bin/pip"
elif [[ "$OS_TYPE" == MINGW* || "$OS_TYPE" == CYGWIN* || "$OS_TYPE" == MSYS* ]]; then
PIP="$ENV_DIR\\Scripts\\pip.exe"
else
echo "Unknown OS. Cannot determine pip path."
exit 1
fi
# Check if requirements.txt exists and install the packages if it does.
if [ -f "requirements.txt" ]; then
echo "Installing pip packages from requirements.txt..."
$PIP install -r requirements.txt
else
echo "requirements.txt not found. Skipping package installation."
fi
echo "Pip package installation complete."
# Provide activation instructions based on the OS.
if [ "$OS_TYPE" = "Darwin" ] || [ "$OS_TYPE" = "Linux" ]; then
echo "To activate the virtual environment, run:"
echo " source $ENV_DIR/bin/activate"
elif [[ "$OS_TYPE" == MINGW* || "$OS_TYPE" == CYGWIN* || "$OS_TYPE" == MSYS* ]]; then
echo "For Windows, run:"
echo " $ENV_DIR\\Scripts\\activate"
else
echo "Unknown OS. Please activate the virtual environment manually."
fi
echo "Setup complete."