Virtual Environments in Python (84/100 Days of Python)
Virtual environments are an essential tool for any Python developer. They allow you to create isolated environments where you can install packages and dependencies without affecting the global Python installation on your machine. This is particularly useful when you have multiple projects with different dependencies, or when working on a team where each member has a different development environment. In this tutorial, we will cover the basics of virtual environments in Python, how to configure them, and some best practices for working with virtual environments.
What are Virtual Environments?
A virtual environment is a self-contained Python environment that allows you to install packages and dependencies without affecting the global Python installation on your machine. Each virtual environment has its own Python executable and package manager, which makes it possible to have different versions of packages and dependencies for different projects. This isolation helps to avoid conflicts between different projects and allows you to work with a consistent set of packages and dependencies for each project.
Creating a Virtual Environment
Python comes with a built-in module for creating virtual environments called venv
. To create a new virtual environment, open a command prompt or terminal window and navigate to the directory where you want to create the environment. Then run the following command:
python -m venv myenv
This command creates a new virtual environment named myenv
in the current directory. You can replace myenv
with any name you want.
Activating the Virtual Environment
To start using the virtual environment, you need to activate it. To activate the virtual environment, navigate to the directory where you created the virtual environment and run the activate script:
On Windows
myenv\Scripts\activate.bat
On Linux or macOS
source myenv/bin/activate
Once activated, the name of the virtual environment will appear in the command prompt or terminal prompt. This indicates that you are now working inside the virtual environment.
Installing Packages in the Virtual Environment
Once you have activated the virtual environment, you can install packages and dependencies using pip, the Python package manager. To install a package, simply run the following command:
pip install package_name
This command installs the package in the virtual environment. If you have multiple projects with different dependencies, you can install the packages specific to each project in its virtual environment. This ensures that each project has a consistent set of packages and dependencies.
Deactivating the Virtual Environment
When you are done working in the virtual environment, you can deactivate it by running the following command:
deactivate
This command deactivates the virtual environment and returns you to the global Python environment.
Best Practices for Working with Virtual Environments
- Always create a virtual environment for each project. This ensures that each project has a consistent set of packages and dependencies.
- Always activate the virtual environment before working on a project.
- Always include the
requirements.txt
orsetup.py
file in your project repository. This file contains a list of all the packages and dependencies required for the project and makes it easy for other developers to set up the virtual environment. - Always update the requirements when you install or update packages.
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course
- Full series: 100 Days of Python
- Previous topic: Working With Third-Party Libraries
- Next topic: Unit Testing in Python with Pytest