The Wayback Machine - https://web.archive.org/web/20241030135037/https://www.geeksforgeeks.org/python-virtual-environment/
Open In App

Python Virtual Environment | Introduction

Last Updated : 11 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python. You can set up your own libraries and dependencies without affecting the system Python. We will use virtualenv to create a virtual environment in Python.

What is a Virtual Environment?

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use.

Why do we need a virtual environment?

Imagine a scenario where you are working on two web-based Python projects one of them uses Django 4.0 and the other uses Django 4.1 (check for the latest Django versions and so on). In such situations, we need to create a virtual environment in Python that can be really useful to maintain the dependencies of both projects.

When and where to use a virtual environment?

By default, every project on your system will use these same directories to store and retrieve site packages (third-party libraries).

How does this matter? Now, in the above example of two projects, you have two versions of Django. This is a real problem for Python since it can’t differentiate between versions in the “site-packages” directory. So both v1.9 and v1.10 would reside in the same directory with the same name.

This is where virtual environments come into play. To solve this problem, we just need to create two separate virtual environments for both projects.

The great thing about this is that there are no limits to the number of environments you can have since they’re just directories containing a few scripts.

A virtual Environment should be used whenever you work on any Python-based project. It is generally good to have one new virtual environment for every Python-based project you work on. So the dependencies of every project are isolated from the system and each other.

Create Virtual Environment in Python

We use a module named virtualenv which is a tool to create virtual environment Python, isolated from the system environment Python.

virtualenv creates a folder that contains all the executables necessary to use the packages that a Python project would need.

Installing virtualenv

$ pip install virtualenv

Test your installation:

$ virtualenv --version

Create Python Environment

You can create a virtualenv using the following command:

$ virtualenv my_env

After running this command, a directory named my_env will be created. This is the directory that contains all the necessary executables to use the packages that a Python project would need.

This is where Python packages will be installed. If you want to specify the Python interpreter of your choice, for example, Python3, it can be done using the following command:

$ virtualenv -p /usr/bin/python3 virtualenv_name

Activating a Virtual Environment in Python

Now after creating a virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command:

Activate a Virtual Environment on Windows

To activate virtual environment using windows command prompt change directory to your virtual env, Then use the below command

$ cd <envname>
$ Scripts\activate 

Note: source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).

Activate a virtual environment on Linux

$ source virtualenv_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal.

activate virtual environment in Python

This will let you know that the virtual environment is currently active.

Installing Dependencies in Virtual Environment Python

In the image below, venv named virtual environment is active. Now you can install dependencies related to the project in this virtual environment.

For example, if you are using Django 1.9 for a project, you can install it like you install other packages.

(virtualenv_name)$ pip install Django==1.9

The Django 1.9 package will be placed in virtualenv_name folder and will be isolated from the complete system.

Deactivate Python Virtual Environment

Once you are done with the work, you can deactivate the virtual environment by the following command:

(virtualenv_name)$ deactivate

deactivate virtual environment in Python

Now you will be back to the system’s default Python installation.

We have covered virtual environment in Python, How to create virtual environment in Python?, how to activate and deactivate virtual environment? and now to install dependencies.

This covers all the basic concepts of Python virtual environment and you can use it on your personal PC.

Also Read:



Similar Reads

Creating Python Virtual Environment in Windows and Linux
A Virtual Environment is a Python environment, that is an isolated working copy of Python that allows you to work on a specific project without affecting other projects So basically it is a tool that enables multiple side-by-side installations of Python, one for each project. Creating a Python virtual environment in Linux If pip is not in your syst
2 min read
Using mkvirtualenv to create new Virtual Environment - Python
Virtual Environment are used If you already have a python version installed and you want to use a different version for a project without bothering the older ones. it is good practice to use a new virtual environment for different projects. There are multiple ways of creating that, today we will create one using mkvirtualenv command. virtualenvwrap
2 min read
Create virtual environment using venv | Python
Managing multiple Python projects that have conflicting dependencies can be a daunting task. However, virtual environments can help you solve this problem. With virtual environments, each project can thrive in its own sandboxed paradise with its libraries and settings. By learning how to create virtual environments using Venv, you can say goodbye t
6 min read
How to Set Up a Python Virtual Environment in Visual Studio on Windows
Creating a Python virtual environment is a fundamental practice for managing dependencies and ensuring project isolation. This guide will walk us through the steps to set up a Python virtual environment in Visual Studio on a Windows machine, providing a solid foundation for our Python project development. Why is a Virtual Environment Important?Supp
3 min read
Using Jupyter Notebook in Virtual Environment
In this article, we are going to see how to set Virtual Environment in Jupyter. Sometimes we want to use the Jupyter notebook in a virtual environment so that only selected packages are available in the scope of the notebook. To do this we have to add a new kernel for the virtual environment in the list of kernels available for the Jupyter notebook
2 min read
Setting Up a Virtual Environment in Django
Setting up a virtual environment in Django is essential for isolating your project's dependencies and ensuring consistent behavior across different environments. A virtual environment allows you to install packages locally without affecting the global Python installation. Here's how to set up a virtual environment for your Django project. Setting U
2 min read
Add packages to Anaconda environment in Python
Let's see some methods that can be used to install packages in the Anaconda environment. There are many ways one can add pre-built packages to an anaconda environment. So, let's see how to direct the path in the anaconda and install them. Add packages to the Anaconda environment using the navigator Step 1: Open your Anaconda navigator Step 2: Go to
2 min read
Python - Setting up the Bokeh Environment
Bokeh is supported on CPython versions 3.6+ only both with Standard distribution and Anaconda distribution. Other Python versions or implementations may or may not function. Current version of Bokeh is 2.0.2 . Bokeh package has the following dependencies: 1. Required Dependencies PyYAML>=3.10python-dateutil>=2.1Jinja2>=2.7numpy>=1.11.3p
1 min read
PYTHONPATH Environment Variable in Python
Python's behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules. The PYTHONPATH variable holds a string with the
2 min read
Environment Variables in Python
In Python, its behavior is highly influenced by the setup of the environment variables. There is a fixed number of environment variables that Python recognizes and these generally are processed before the command line switches. Whenever a conflict arises between the environmental variable and the command line switches, the environment variable gets
4 min read
Python Falcon - Environment Setup
Falcon is a Python library renowned for its proficiency in crafting indispensable REST APIs and microservices. Capable of accommodating both the WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface) standards, Falcon has gained widespread recognition since its inception. Originally conceived by Kurt Griffiths in Janua
3 min read
Python Pyramid - Environment Setup
Python Pyramid, often simply referred to as Pyramid, is an open-source web framework used for building web applications and web services in Python. Whether you're constructing a website, or a sophisticated web application Pyramid offers the resources and adaptability to complete the task effectively. Environment Setup for Python PyramidTo begin wit
3 min read
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-value pairs. In this article, we will see how to use
3 min read
Why can't we access Python from a Conda Environment?
Conda environments are isolated spaces where we can install specific Python versions and packages without interfering with other projects. But sometimes, some issues may arise when trying to activate or use these environments. In this article, we will learn about accessing issues of Python from a conda environment. Problem Statement:You've created
2 min read
Read Environment Variables with Python dotenv
Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible software development practices. Introduction to python-d
4 min read
Top 9 Anaconda Alternatives for Python Environment Management
Anaconda is a famous platform for facts technological know-how and Python development, known for its ease of environment control and bundled packages like numpy, pandas, and matplotlib. However, a few builders are seeking options because of its large size, useful resource intake, or due to the fact they want greater lightweight or specialised gear.
3 min read
Set up Opencv with anaconda environment
If you love working on image processing and video analysis using python then you have come to the right place. Python is one of the major languages that can be used to process images or videos. Requirements for OpenCV and Anaconda - 32- or a 64-bit computer. - For Miniconda—400 MB disk space. - For Anaconda—A minimum 3 GB disk space to download and
3 min read
Kotlin Environment setup for Command Line
To set up a Kotlin environment for the command line, you need to do the following steps: Install the Java Development Kit (JDK): Kotlin runs on the Java virtual machine, so you need to have the JDK installed. You can download the latest version from the official Oracle website.Download the Kotlin compiler: You can download the latest version of the
2 min read
Kotlin Environment setup with Intellij IDEA
Kotlin is a statically typed, general-purpose programming language developed by JetBrains that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011. Kotlin is object-oriented language and a better language than Java, but still be fully interoperable with Java code. Let's see how to setu
2 min read
Environment setup for CherryPy
CherryPy is a popular framework of Python. Using CherryPy, web applications can be built in a faster and more reliable way. It is also called a web application library. It is known for its simplicity as it is based on object-oriented Python programming, resulting in smaller source code in less time. Note: For more information, refer to Introduction
2 min read
Environment Setup for Matplotlib
Matplotlib is an overall package for creating static, animated, and interactive visualizations in Python. It literally opens up a whole new world of possibilities for you! Especially when it is used with Numpy or Pandas library, one can do unimaginable things. The plots give may give a new insight altogether. Now, the question arises i.e. How to ma
1 min read
Setup OpenCV With PyCharm Environment
Introduction:If you love working on image processing and video analysis using python then you have come to the right place. Python is one of the key languages which is used to process videos and images. Requirements for OpenCV:32- or a 64-bit computer.Windows, macOS or Linux.Python 2.7, 3.4, 3.5 or 3.6.PyCharmPyCharm is a cross-platform IDE used in
2 min read
How to setup Conda environment with Jupyter Notebook ?
Anaconda is open-source software that contains Jupyter, spyder, etc that are used for large data processing, data analytics, heavy scientific computing. Anaconda works for R and python programming language. Spyder(sub-application of Anaconda) is used for python. Opencv for python will work in spyder. Package versions are managed by the package mana
2 min read
How to Delete an Environment in Conda
If you have an environment in Anaconda and you no longer need it in your Anaconda, in this article we will see how to deactivate an environment in Anaconda and delete it. Firstly, let's know what is an anaconda environment. Anaconda EnvironmentAnaconda environments commonly known as Environment in Conda is generally the directory that contains the
3 min read
How to Setup Anaconda Path to Environment Variable?
Anaconda is open-source software that contains Jupyter, spyder, etc that are used for large data processing, data analytics, and heavy scientific computing. Anaconda works for R and Python programming languages. Spyder(a sub-application of Anaconda) is used for Python. for Python will work in Spyder. Package versions are managed by the package mana
4 min read
Best Practices for Managing Django Secret Keys and Environment Variables
In Django, the SECRET_KEY is a crucial component used to ensure the security of various operations and cryptographic processes within a Django project. This key plays a vital role in protecting the integrity of your application, particularly in terms of session management, password resets, and cryptographic signing. Understanding Django SECRET_KEYP
3 min read
Python | Vkeyboard (virtual keyboard) in kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. Vkeyboard: VKeyboard is an onscreen keyboard for Kivy. Its operation is intended to be transparent to the user. Us
2 min read
Build a Virtual Assistant Using Python
Virtual desktop assistant is an awesome thing. If you want your machine to run on your command like Jarvis did for Tony. Yes it is possible. It is possible using Python. Python offers a good major library so that we can use it for making a virtual assistant. Windows has Sapi5 and Linux has Espeak which can help us in having the voice from our machi
8 min read
Python Virtual Machine
The Python Virtual Machine (VM) is a crucial component of the Python runtime environment. It executes Python bytecode, which is generated from Python source code or intermediate representations like Abstract Syntax Trees (ASTs). In this article, we'll explore the Python Virtual Machine, discussing its architecture, bytecode execution process, and i
3 min read
Managing Virtual environments in Python Poetry
Poetry helps you declare, manage, and install dependencies of Python projects, ensuring you have the right stack everywhere. Poetry is a tool for dependency management and packaging in the PHP programming language that helps in managing project dependencies and creating virtual environments. Unlike other tools like pip and virtualenv, Poetry is a s
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg