Skip to main content

Virtual Environment in Python

In this tutorial, you'll learn about the Virtual Environment and two different ways of creating it.
Mar 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

Introduction to Python

Beginner
4 hours
4,596,578
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

Intermediate Python

Beginner
4 hours
883,268
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Python Data Science Toolbox (Part 1)

Beginner
3 hours
344,437
Learn the art of writing your own functions in Python, as well as key concepts like scoping and error handling.
See all coursesRight Arrow
Related

The 23 Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.

Abid Ali Awan

22 min

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
DataCamp Team's photo

DataCamp Team

Plotly Express Cheat Sheet

Plotly is one of the most widely used data visualization packages in Python. Learn more about it in this cheat sheet.
DataCamp Team's photo

DataCamp Team

0 min

Getting started with Python cheat sheet

Python is the most popular programming language in data science. Use this cheat sheet to jumpstart your Python learning journey.
DataCamp Team's photo

DataCamp Team

8 min

Python pandas tutorial: The ultimate guide for beginners

Are you ready to begin your pandas journey? Here’s a step-by-step guide on how to get started. [Updated November 2022]
Vidhi Chugh's photo

Vidhi Chugh

15 min

Python Iterators and Generators Tutorial

Explore the difference between Python Iterators and Generators and learn which are the best to use in various situations.
Kurtis Pykes 's photo

Kurtis Pykes

10 min

See MoreSee More