AZURE HEROES
  • Home-Updates
  • Blog
    • Azure Blog
    • Azure Heroes Events >
      • Azure Heroes Sessions #1
      • Azure Heroes Sessions #2
      • Azure Heroes Sessions #3
      • Azure Heroes Sessions #4
      • Azure Heroes Sessions #5
      • Azure Heroes Sessions #6
      • Azure Heroes Sessions #7
  • Who We Are!
  • eBooks
  • Azure All In One!
    • Azure Disk & Storage
    • Azure Network
    • Azure VPN
    • Azure VMs
  • Free Azure Support!
  • Contact Us
  • Events
    • Beginners Event
    • Developers Event
    • Special Event
    • Azure Workshop #4
    • Azure Workshop #5
    • Azure Workshop #6
    • Azure Workshop #7
    • Azure Workshop #8
    • Azure Heroes Sessions #9
    • Azure Heroes Sessions #10
    • Azure Heroes Sessions #11
    • Azure Heroes Sessions #12
    • Azure Heroes Sessions #13
    • Azure Heroes Sessions #14
    • Azure Heroes Sessions #15
    • Azure Heroes Sessions #16
    • Azure Heroes Sessions #17
    • Azure Heroes Sessions #18
  • Registration Form
  • Privacy Policy
  • Home-Updates
  • Blog
    • Azure Blog
    • Azure Heroes Events >
      • Azure Heroes Sessions #1
      • Azure Heroes Sessions #2
      • Azure Heroes Sessions #3
      • Azure Heroes Sessions #4
      • Azure Heroes Sessions #5
      • Azure Heroes Sessions #6
      • Azure Heroes Sessions #7
  • Who We Are!
  • eBooks
  • Azure All In One!
    • Azure Disk & Storage
    • Azure Network
    • Azure VPN
    • Azure VMs
  • Free Azure Support!
  • Contact Us
  • Events
    • Beginners Event
    • Developers Event
    • Special Event
    • Azure Workshop #4
    • Azure Workshop #5
    • Azure Workshop #6
    • Azure Workshop #7
    • Azure Workshop #8
    • Azure Heroes Sessions #9
    • Azure Heroes Sessions #10
    • Azure Heroes Sessions #11
    • Azure Heroes Sessions #12
    • Azure Heroes Sessions #13
    • Azure Heroes Sessions #14
    • Azure Heroes Sessions #15
    • Azure Heroes Sessions #16
    • Azure Heroes Sessions #17
    • Azure Heroes Sessions #18
  • Registration Form
  • Privacy Policy

Create a Virtual Environment in Python

2/8/2024

1 Comment

 
If you've been working with Python, you may have heard of virtual environments. They're a fantastic tool to isolate your projects and manage dependencies effectively. Here’s a guide on how to create a virtual environment in Python and why you should consider using them.
Picture
What Is a Virtual Environment in Python?
A virtual environment is a self-contained directory that includes a Python interpreter and the libraries required for your project. The main advantage is isolation—it allows you to work on multiple Python projects with different dependencies or even Python versions without affecting your global Python installation.

Why Should You Use a Virtual Environment?1. Environment IsolationA virtual environment ensures that dependencies for one project won’t interfere with another or with your global Python setup.
2. Dependency ManagementYou can install specific versions of libraries needed for your project without affecting other projects.
3. Shareable Project DependenciesVirtual environments make it easy to generate a list of dependencies that your project requires (via requirements.txt).
4. ReproducibilityWhen you share your project, others can recreate the exact environment, ensuring consistency.

Setting Up a Virtual Environment in PythonBefore starting, make sure Python is installed on your system. If not, you can download it from the official Python website.




Step 1: Install venvPython’s built-in module venv makes it easy to create virtual environments.
To install it, run:

pip install virtualenv

Step 2: Create a Virtual EnvironmentNavigate to the directory where you want your virtual environment and run:

python -m venv my_env

Here, my_env is the name of the virtual environment.
You’ll notice that a new directory my_env is created. It contains the Python interpreter and libraries for the virtual environment.
Step 3: Activate the Virtual EnvironmentActivate your virtual environment:
  • On macOS/Linux:
source my_env/bin/activate
  • On Windows:
my_env\Scripts\activate

Installing and Using Python PackagesWith the virtual environment active, you can install packages without affecting your global Python installation.
For example, to install pandas:
pip install pandas

Run a Python ScriptSuppose you have the following Python script, script.py:

import pandas as pd
# Create a simple DataFrame
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)

Run it within your virtual environment:
python script.py

Creating and Using a requirements.txt FileTo share your project, generate a list of installed dependencies:
pip freeze > requirements.txt

The requirements.txt file will look something like this:
pandas==2.1.0
numpy==1.26.0

To recreate the environment elsewhere:
  1. Create a new virtual environment.
  2. Install dependencies using:
pip install -r requirements.txt

Deactivating the Virtual EnvironmentWhen done, deactivate the virtual environment with:

deactivate

Your terminal prompt will return to its default state.
Conclusion
Using Python virtual environments is a simple yet powerful way to manage dependencies across multiple projects. It prevents conflicts and ensures that your projects are reproducible. Whether you’re working on one project or several, leveraging virtual environments will make your Python development much smoother. Give it a try, and see how it simplifies your workflow!


1 Comment
karmen
11/25/2024 02:40:28 am

very useful, good explanation

Reply



Leave a Reply.

    Author

    Mohammad Al Rousan is a Microsoft MVP (Azure), Microsoft Certified Solution Expert (MCSE) in Cloud Platform & Azure DevOps & Infrastructure, An active community blogger and speaker. Al Rousan has over 11 years of professional experience in IT Infrastructure and very passionate about Microsoft technologies and products.

    Picture
    Picture
    Top 10 Microsoft Azure Blogs

    Archives

    January 2025
    December 2024
    November 2024
    October 2024
    September 2024
    July 2024
    June 2024
    May 2024
    April 2024
    February 2024
    September 2023
    August 2023
    May 2023
    November 2022
    October 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    February 2022
    January 2022
    December 2021
    November 2021
    May 2021
    February 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    June 2020
    April 2020
    January 2020
    July 2019
    June 2019
    May 2019
    February 2019
    January 2019

    Categories

    All
    AKS
    Azure
    Beginner
    CDN
    DevOps
    End Of Support
    Fundamentals
    Guide
    Hybrid
    License
    Migration
    Network
    Security
    SQL
    Storage
    Virtual Machines
    WAF

    RSS Feed

    Follow
    Free counters!
Powered by Create your own unique website with customizable templates.