Skip to main content
HomeTutorialsPython

Virtual Environment in Python

In this tutorial, you'll learn about the Virtual Environment and two different ways of creating it.
Mar 5, 2020  · 7 min read

python datacamp banner

Virtual Environment in Python

Learn about the Virtual Environment and two different ways for creating it: Pipenv is mostly used by web developers and Anaconda distributions for data scientists where Virtual Environment is created from 'conda' through 'Anaconda Prompt'. It also has an alternative option to create from Anaconda Navigator.

Virtual Environment is used to create a container or isolated environment where the Python-related dependencies are installed for a specific project. One can work with many different versions of Python with its various packages. Data scientists tend to use Anaconda distribution, which comes with many useful pre-installed packages, which are easy to install and manage. A web developer who uses Django, Flask, and other Python-related frameworks can use Pipenv as their Virtual Environment.

Pipenv

Pipenv is a new and popular way of automatically creating a 'virtualenv' for the project. It creates a Pipfile, which helps to manage the package and can be installed or removed easily. Through Pipenv, 'pip' and 'virtualenv' can be used together to create a Virtual Environment, Pipfile works as the replacement of the 'requirement.txt.' which tracks the package version according to the given project.

This tutorial uses Git Bash as the terminal in the Windows Operating System. However, for Mac users, Homebrew could be used, which is the package management tool, and similarly, LinuxBrew could be used for Linux users.

Let's create a project using the 'mkdir project-name' command, which stands for making a directory with the project name as 'new-project' and move to the newly created directory by using 'cd' command.
new project

Windows users can use the following command to install 'pipenv'.

pip install pipenv

Linux/Mac Users can use the following command to install 'pipenv' after installing LinuxBrew.

brew install pipenv

Creating a virtualenv for the project

For creating the 'virtualenv' for the project, use the following command.

pipenv shell The command 'pipenv' creates a new 'virtualenv' for the project along with Pipfile side by side. virtualenv

Installing/Uninstalling required packages for the project

You'll install the two package named 'requests' and 'flask' in your Virtual Environment using the following command.
pipenv install requests
pipenv install flask
The following changes are seen after the installation of the two packages. installation For the uninstallation of the package, use the following command.
pipenv uninstall flask

Exiting from the Virtual Environment

You can use the following command to deactivate form the current environment. exit

Anaconda

Anaconda is the most popular platform used by data scientists and machine learning engineers. It consists of 'Conda', which helps in managing the environment, libraries, and dependencies.

Installing Anaconda for Python Virtual Environment Manager

  • Windows users can head over to the following tutorial to set up the Anaconda distribution.

Install Anaconda Windows

  • Linux/Mac Users then head over to the following tutorial to set up the Anaconda distribution.

Install Anaconda Mac/Linux
Use the Anaconda version of 4.6 or newer. There are slight changes in the command for the previous version. If you are running older versions, use the following command for an update.
conda update conda One of two ways can be used in the creation of the Virtual Environment, which is shown below.

    • Anaconda Prompt-It is a command-line tool that comes after the installation of Anaconda distribution.
    • Anaconda Navigator-It is a Graphical User Interface that serves as an alternative tool to launch and manage packages in Anaconda.

This tutorial uses Windows Operating System but works with any Operating System. There might be some minor changes in the Anaconda command, but the overall creation process for Virtual Environment is the same. You can open "Terminal" in Mac/Linux to achieve the following result. According to the requirements needed for the projects, the following guides serve more detail in the creation of a Virtual Environment.
Managing environments

Anaconda Prompt

Press the "Windows" icon in the lower corner of the screen to open the "Search" box. Type "Anaconda Prompt" and then hit "Enter" to open it. Use conda to check Anaconda has been successfully installed in your system; the following changes could be seen. anaconda prompt

      1. Creating a new Virtual Environment.
        The following command takes '-n' as a flag, which is for creating a new environment with its name as 'env' and the specific Python version of '3.7'. conda create -n env python = 3.7

      2. Activating the Virtual Environment.
        The command below activates the Virtual Environment, which changes the prompt where the 'env' is shown in parenthesis. conda activate env activating environment

      3. Install the required package.
        For example, the 'numpy' package is installed where 'env' is the specific Virtual Environment.
        conda install -n env numpy
        OR
        Also, Python Package manager could be used to install 'numpy'. pip install numpy
        pip install numpy

      4. Listing all of the installed packages inside a Virtual Environment.
        The following command can list the package specific to the Virtual Environment. conda list
        packages in virtual environment

      5. Listing out all of the created Virtual Environment.
        All of the environments created will be listed by the following command. conda env list command

      6. Deactivating the Virtual Environment.
        The following command will deactivate the current environment 'env' and will change to 'base'. conda deactivate

      7. Removing the Virtual Environment.

The following command removes the 'myenv' Virtual Environment with all its packages at the same time. conda env remove -n myenv remove packages As you can see after listing with 'conda env list', only two Virtual Environments are shown. virtual environments

Anaconda Navigator

      1. Press the "Windows" icon in the lower corner of the screen to open the "Search" box. Type "Anaconda Navigator" and then hit "Enter" to open it.
      2. Move to "Environments," where two virtual environments are shown.' base' is the default, whereas 'env' is the previously created Virtual Environment. anaconda navigator

      3. Click "Create" and Fill up the required information, as shown below, to create a new Virtual Environment called "new-env." create new environment

      4. Move to the "new-env" and select "All" to install the required package, i.e., numpy in our case. new env

      5. Select 'new-env' at the home directory so that launching an application will be specific to that particular virtual environment. new env 2

Conclusion

Congratulations on finishing the tutorial!

You've successfully learned about the Virtual Environment and creation through Pipenv and Anaconda.

You can look over to the following courses created by Anaconda in DataCamp platform to learn more:

References:
Installing pipenv

Topics

Learn more about Python

Course

Introduction to Python

4 hr
5.5M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

tutorial

Inner Classes in Python

In this basic Python tutorial, you'll learn about why and when you should use inner classes.

Hafeezul Kareem Shaik

5 min

tutorial

Python Setup: The Definitive Guide

In this tutorial, you'll learn how to set up your computer for Python development, and explain the basics for having the best application lifecycle.

J. Andrés Pizarro

15 min

tutorial

Python Dictionaries Tutorial

Learn how to create a dictionary in Python.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

if…elif…else in Python Tutorial

Learn how you can create if…elif…else statements in Python.
DataCamp Team's photo

DataCamp Team

4 min

tutorial

Python Tutorial for Beginners

Get a step-by-step guide on how to install Python and use it for basic data science functions.
Matthew Przybyla's photo

Matthew Przybyla

12 min

tutorial

Introduction to Python Metaclasses

In this tutorial, learn what metaclasses are, how to implement them in Python, and how to create custom ones.
Derrick Mwiti's photo

Derrick Mwiti

6 min

See MoreSee More