-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Concept Entry] Python: Virtual Environments
* Add virtualenv entry * Update virtual-environment.md ---------
- Loading branch information
1 parent
9fd7003
commit f4357f7
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
content/python/concepts/virtual-environment/virtual-environment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
Title: 'Virtual Environment' | ||
Description: 'A virtual environment in Python is a solitary workspace to manage dependencies and avoid conflicts between packages.' | ||
Subjects: | ||
- 'Computer Science' | ||
- 'Data Science' | ||
Tags: | ||
- 'Command Line' | ||
- 'Packages' | ||
CatalogContent: | ||
- 'learn-python-3' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
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: | ||
|
||
```pseudo | ||
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: | ||
|
||
```shell | ||
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: | ||
|
||
```shell | ||
.\venv\Scripts\activate | ||
``` | ||
|
||
To activate a virtual environment on macOS/Linux, the following command is used: | ||
|
||
```shell | ||
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: | ||
|
||
```shell | ||
deactivate | ||
``` | ||
|
||
This deactivates the current virtual environment, returning to the global Python environment. |