×

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

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

Sklearn linear Model in Python

The most effective and reliable Python machine learning library is Sklearn. Through a Python consistency interface, it offers a variety of effective tools for statistical modeling and machine learning, including...

5 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

Python Coroutine

Introduction In Python, Coroutines are defined as the special type of function that freely allows control to its caller without losing the state. Coroutines and generates are similar but coroutines consist...

3 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

How to Download Python

How to Download Python Python is the most renowned programming language and developers across the globe are working on projects that are made using Python and its libraries. Here we will...

4 minutes read.

Create Table Using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's...

4 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

Difference between Perl and Python

Control and presently utilized for a great many undertakings, including framework organization, web improvement, network programming, and GUI improvement, and the sky is the limit from there. About Perl? Perl is a...

4 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

What does base case mean in recursion

Base cases and recursive stages are used to define recursive functions. In a primary subject, we compute the outcome right away, given the function call's arguments. In a recursive step, we...

4 minutes read.