Creating and Managing Virtual Environments in Python
Table of Contents
Assalamu Alaikum! In this Python programming lecture series, today we will focus on how to create virtual environments in Python.
First, let's discuss:
- What are virtual environments?
- Why do we need them?
- Why is it important to learn them?
The Problem Virtual Environments Solve
When working on multiple Python projects, a common challenge arises:
- Suppose you are working on Project A, which requires Library X (v1.3).
- At the same time, Project B needs Library X (v2.3).
- If both versions are installed globally, conflicts occur, and neither project will run correctly.
Solution:
Virtual environments allow us to:
- Isolate dependencies for each project.
- Use different Python versions per project.
- Avoid conflicts between package versions.
Step 1: Creating a Virtual Environment
1. Open Anaconda Prompt
- Press Windows Key and search for "Anaconda Prompt".
- (Screenshot Suggestion: Show Anaconda Prompt in the Start Menu.)
2. Navigate to Your Project Directory
cd Documents
cd Python
(Press Tab to auto-complete folder names and avoid spelling mistakes.)
3. Create a Virtual Environment
conda create --name my_env python=3.10
my_env= Your environment name.python=3.10= Specifies Python version (optional).
(Screenshot Suggestion: Show the command execution in Anaconda Prompt.)
- Type
yto confirm installation. - Conda will install base packages for the environment.
Step 2: Activating the Virtual Environment
conda activate my_env
How to confirm activation?
- Before activation:
(base)appears in the prompt. - After activation:
(my_env)appears.
(Screenshot Suggestion: Show the prompt before and after activation.)
Step 3: Installing Packages Inside the Virtual Environment
Method 1: Using pip
pip install numpy==2.2
(Attempts to install NumPy v2.2. If unavailable, installs the latest version.)
Method 2: Using conda
conda install pandas
(Screenshot Suggestion: Show package installation process.)
Step 4: Checking Installed Packages
conda list
This shows all packages installed in the current environment.
(Screenshot Suggestion: Show the output of conda list.)
Step 5: Deactivating the Virtual Environment
conda deactivate
- The prompt returns to
(base).
Step 6: Managing Multiple Environments
1. Listing All Environments
conda info --envs
(Output shows all created environments.)
2. Creating Another Environment
conda create --name hello_env python=3.11
3. Deleting an Environment
conda remove --name hello_env --all
- Type
yto confirm deletion.
(Screenshot Suggestion: Show environment deletion process.)
Summary of Commands
| Action | Command |
|---|---|
| Create Environment | conda create --name env_name python=3.x |
| Activate Environment | conda activate env_name |
| Install Package (pip) | pip install package_name==version |
| Install Package (conda) | conda install package_name |
| List Installed Packages | conda list |
| Deactivate Environment | conda deactivate |
| List All Environments | conda info --envs |
| Delete Environment | conda remove --name env_name --all |
Conclusion
In this lecture, we learned:
- Why virtual environments are essential.
- How to create and activate them.
- Installing packages using
pipandconda. - Managing multiple environments.
Experiment with different Python versions and packages! If you face issues, leave feedback in the comments.
See you in the next lecture! Allah Hafiz.
Comments
Post a Comment