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.
Updated 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

Topics

Learn more about Python

Certification available

Course

Introduction to Python

4 hr
5.4M
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

Mastering the Pandas .explode() Method: A Comprehensive Guide

Learn all you need to know about the pandas .explode() method, covering single and multiple columns, handling nested data, and common pitfalls with practical Python code examples.

Adel Nehme

5 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More