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.
How to create a virtual environment in Python?
There are several ways to create a virtual environment in Python, we will learn the most common methods which are using venv
module using CLI and extention in VSCode.
1. Using venv
module in CLI
The venv
module is the built-in tool for creating virtual environments in Python. Here's how you can create a virtual environment using venv
in the command line:
- Open your 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 named
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, butyou 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 directory containing it.
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, butyou can verify the activation by runningwhere.exe python
orwhich python3
command. It should point to the Python interpreter within the virtual environment. - If the above did't work for you, use Python extension in VSCode to select the interpreter from the virtual environment. https://marketplace.visualstudio.com/items?itemName=donjayamanne.python-environment-manager
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. By following the steps outlined above, you can quickly set up a virtual environment for your project and start developing with confidence.
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!
Top comments (1)
How does the Python extension in VSCode compare in performance and ease of use to using the CLI method for creating virtual environments? Would love to hear more about pros and cons in another post!