Well, imagine you have multiple projects that require different versions of the same library or package to run properly. Without using virtual environments, you would need to install those packages globally on your computer, which can cause conflicts and mess up other projects that rely on different versions of those libraries. But with virtual environments, you can create a separate “virtual” space for each project where all its dependencies are isolated from the rest of your system. This means you don’t have to worry about version conflicts or accidentally breaking another project by installing something in one environment and then forgetting that it was installed there when working on a different project. To set up a virtual environment, first navigate to your project directory using Terminal (or Git Bash if you’re on Windows) and run this command:
# This script sets up a virtual environment for a project to avoid conflicts with other projects and their dependencies.
# First, we need to specify which version of Python we want to use for our virtual environment.
# We do this by using the "python3" command followed by the "-m venv" flag, which stands for "module venv".
# This will create a virtual environment named "my_project_venv" in our current directory.
python3 -m venv my_project_venv
# Alternatively, if we are using an older version of Python, we can use the "virtualenv" command instead.
# This will also create a virtual environment named "my_project_venv" in our current directory.
virtualenv my_project_venv
This will create a new directory called `my_project_venv` in your project folder. To activate the environment, run:
# This script creates a new virtual environment called `my_project_venv` in the project folder.
# To activate the environment, run the following commands:
# On Unix-like systems (Linux or macOS):
bash
source my_project_venv/bin/activate # activates the virtual environment by sourcing the `activate` script in the `my_project_venv` directory
# On Windows:
bash
.\my_project_venv\Scripts\activate # activates the virtual environment by running the `activate` script in the `my_project_venv\Scripts` directory
This will add a prefix to your terminal prompt that indicates you’re in the virtual environment. Now, when you install packages using pip inside this environment, they won’t be installed globally and will only affect the current project. To deactivate the environment, run:
# This script is used to deactivate a virtual environment in a project.
# It is important to deactivate the environment after use to avoid any conflicts with other projects.
# The first line is used to deactivate the environment on Unix-like systems such as Linux or macOS.
# The 'deactivate' command is used to exit the virtual environment and return to the global environment.
deactivate
# The second line is used to deactivate the environment on Windows systems.
# The '.\my_project_venv\Scripts\deactivate' command is used to exit the virtual environment and return to the global environment.
.\my_project_venv\Scripts\deactivate
This will remove the prefix from your terminal prompt and return you to your regular environment. And that’s it! You can create as many virtual environments as you need for different projects, each with its own set of dependencies. This helps keep things organized and prevents conflicts between them. It may seem like overkill at first, but trust me, once you start using them regularly, you won’t know how you ever lived without them!