×

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python. We will focus on the steps and commands needed to install the virtual environment in python as well as windows operating system.

What is a Virtual Environment?

Keeping Python user-friendly has been the main idea behind its development. So, just for the ease of developers, a virtual environment has been provided.

The virtual environment is a folder structure that provides the user with everything needed to run an isolated lightweight code.

It also serves as a tool that assists in maintaining dependencies needed in different projects detached from one another. It creates a unique project virtual environment for each project.

Dependencies

It is important to know what dependency is. Dependencies are the software components needed to run the code smoothly without encountering a runtime error.

Dependencies are managed through methods such as venv and pipenv.

Need for Virtual Environment

When a user wants to create two projects on different versions using the Django framework. The virtual environment plays a key role in maintaining the dependencies of both projects.

Python does not apply limitations on the number of a virtual environment to be created. Site packages in python are stored and recovered in the same packages by default. Python is unable to differentiate the versions of the Django framework. It will store the files in the same location. In such instances, the virtual environment plays a crucial role by creating separate environments for both or any number of Django frameworks with different frameworks.

It is a good practice to create a new virtual environment while working on any projects based on python.

How to work with a Virtual Environment

Now, let us understand how a virtual environment can be used in python.

To create a remote python environment, module virtualenv is used.

This module contains the compulsory workable packages needed by a python project.

The following steps are involved to be able to use the virtual environment in Linux as well as Windows operating systems.

1. Installation

For installation in Linux, the $ pip install virtualenv command is used.

To check the version of the installed environment, the $ virtualenv --version command is used.

For installation in Windows, the PS> python -m venv venv command is used.

Here, PS denotes the Power shell prompt in windows.

2. Creation

For the creation of a virtual environment,the $ virtualenv directory_tech command is used.

The directory named directory_tech gets created on running the above command. This is the site where the python package will be installed.

Even the interpreter’s name of the user’s choice can be fed here.

To create a Python 2 virtual environment, the following command is used.

$ virtualenv -p /usr/bin/python2 virtualenv_name

In the windows operating system, the following command is used to create a virtual environment in python version 3.

PS> C:\Users\Name\AppData\Local\Programs\Python\Python3\python -m venv venv

3. Activation

In Python, After the creation of an environment, it needs to be activated. The following command is used to activate.

$ source virtualenv_name/bin/activate

If the environment gets successfully activated, its name will appear on the terminal screen.

In Windows, the following command is used.

PS> venv\Scripts\Activate.ps1

4. Installation

Now, the virtual environment is all set to install the dependencies related to the project. It can be installed easily, just like other packages get installed.

The following command is used to install it.

(venv) $ python -m pip install <package-name>

Every python package will be installed in this environment instead of global python site-packages until the user closes the terminal.

In windows, (venv) PS> python -m pip install <package-name> command is used for installation purpose.

5. Deactivation

After finishing up working with this environment in Linux, one can deactivate through a command (venv) $ deactivate

In windows, the command (venv) PS> deactivate is used to deactivate.

This command brings a user out of the created virtual environment.

To be able to revisit the created environment, one needs to activate the environment again.

Summary

In this tutorial, we studied how to set up the virtual environment in Windows and Linux operating systems.


Related Topics

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python Set difference() Method

Python Set difference() Method The set.difference() method in Python returns the set difference of two sets(A-B). Syntax set.difference(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return This method returns the difference of the two specified...

2 minutes read.

Shallow Copy and Deep Copy in Python

Shallow Copy and Deep Copy in Python In this section, we will learn about the Shallow Copy and Deep Copy in the Python program. But before going through the topic, we...

6 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

3 minutes read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Python Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.