A virtual environment in Python is a self-contained directory that contains a specific Python interpreter and a set of installed packages. It allows you to isolate your Python project's dependencies from the system-wide Python installation and other projects.
By creating a virtual environment, you can have different versions of Python and different sets of packages for each project, without worrying about conflicts or dependencies between them. This is particularly useful when working on multiple projects that require different versions of packages or when collaborating with others who may have different setups.
When you activate a virtual environment, it modifies the system's PATH environment variable to prioritize the Python interpreter and packages within the virtual environment. This ensures that when you run Python commands or install packages, they are specific to that environment.
Creating Virtual Environment
There are several ways to create a virtual environment in Python, we will learn the most common methods using the venv
module with the CLI and extension in VSCode.
1. Using CLI with venv
Module
The venv
module is the built-in tool for creating virtual environments in Python. Here's how to create a virtual environment using venv
on the command line:
Open a terminal or command prompt.
Navigate to the directory where you want to create the virtual environment.
Run the following command to create a virtual environment called
myenv
:Windows:
python -m venv myenv
macOS/Linux:
python3 -m venv myenv
To activate the virtual environment, run the following command:
Windows:
myenv\Scripts\activate
macOS/Linux:
source myenv/bin/activate
This will run a script that modifies your shell's PATH to prioritize the Python interpreter and packages within the virtual environment.
You should see the name of the virtual environment
(myenv)
in your terminal prompt, indicating that the virtual environment is active.Sometimes in VS Code you may not see the
(myenv)
in the terminal prompt, but you can verify the activation by runningwhere.exe python
orwhich python3
command. It should point to the Python interpreter within the virtual environment.You can now install packages using
pip
or run Python scripts within this virtual environment.To deactivate the virtual environment, simply run the
deactivate
command in the terminal which will be found in theenv > Scripts
folder in the project.To delete the virtual environment, you can simply delete the generated directory.
That's it! You have successfully created and activated a virtual environment in Python using
venv
.
2. Using the Python extension in VSCode
Open your Python project in Visual Studio Code (VSCode).
Click on the Python interpreter in the bottom left corner of the VSCode window.
Click on Create Virtual Environment.
Choose
venv
as the virtual environment type.Select the base interpreter (Python version) to use for the virtual environment.
The virtual environment will be created in a
.venv
directory within your project.Now if you open a new terminal in VSCode, it should automatically activate the virtual environment.
Sometimes in VS Code you may not see the
(myenv)
in the terminal prompt, but you can verify the activation by runningwhere.exe python
orwhich python3
command. It should point to the Python interpreter within the virtual environment.To make the process even easier, use Python extension in VSCode to select and manage the virtual environment using GUI.
Conclusion
Creating a virtual environment in Python is a simple yet powerful way to manage your project's dependencies and ensure that they are isolated from other projects and the system-wide Python installation.
I hope this article has been helpful in understanding how to create a virtual environment in Python. If you have any questions or feedback, please feel free to reach out. Happy coding!
Do share this article with your friends and colleagues who might find it useful. Thank you for reading