Virtual Environment
In Python, a virtual environment is a solitary workspace that contains its own Python interpreter and installed packages. It allows developers to manage dependencies for different projects independently, avoiding version conflicts between libraries and ensuring that the project has the specific dependencies it requires.
Why Use a Virtual Environment?
Here are some advantages of using a virtual environment:
- Avoid Dependency Conflicts: Different projects may require different versions of the same package. A virtual environment isolates dependencies for each project.
- Clean Environment: It prevents polluting the global Python environment with unnecessary or conflicting packages.
- Reproducibility: It ensures that the exact environment can be replicated by others, improving collaboration and deployment consistency.
Creating a Virtual Environment
Here’s the syntax for creating a virtual environment:
python -m venv <name_of_virtual_environment>
name_of_virtual_environment
: The name of the directory to store the virtual environment.
For example, if a user wants to create a virtual environment named myenv
, the command will look like this:
python -m venv myenv
This creates a virtual environment named myenv
in the current directory.
Activating a Virtual Environment
To activate a virtual environment on Windows, the following command is used:
.\venv\Scripts\activate
To activate a virtual environment on macOS/Linux, the following command is used:
source venv/bin/activate
After the virtual environment is activated, users can proceed to install packages and run Python scripts within the isolated environment.
Deactivating a Virtual Environment
To deactivate a virtual environment, users can run the following command:
deactivate
This deactivates the current virtual environment, returning to the global Python environment.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours