How to Create Python Virtual Environment & Install Packages

1. Python Virtual Environment and Package

In this blog, we will discuss what a Python virtual environment or Python Virtualenv is, and how to create one. We will also see how to install packages using pip. We talked about these in our tutorial on installing Python. If you still haven’t installed Python, do it right away.

Python virtualenv and Packages

Python Virtual Environment and PAckages

2. Introduction to Python Virtualenv

Developing a Python application, you may want to use modules that don’t ship with the standard library. Or sometimes, you need a specific version of a library for a bug to be fixed, or for some other reason.

Does this mean that installing Python once isn’t enough for every application you craft? Maybe application X needs version 1.0, but application Y needs version 2.0. This leaves one of them unable to run.

To work with this, we create a virtual environment in Python. In essence, it is a self-contained directory tree containing a Python installation for a particular version of Python. Apart from that, it also has a list of additional packages. This way, application X can have its virtual environment with version 1.0, and B can have its own with version 2.0. Lets start with Create Python Virtual Environment.

3. Create a Virtual Environment in Python

To create virtualenv Python 3 and manage it, we use the module venv. Normally, it will install the latest version of Python for you, but you can choose that. But before we begin, let’s find out which version of Python we are using.

To do this, type the following in your Command Prompt (for Windows; type ‘cmd’ in search):

python –version

This is what it will look like:

Microsoft Windows [Version 10.0.16299.248]

(c) 2017 Microsoft Corporation. All rights reserved.


C:\Users\lifei>python –version

Python 3.6.0


Now, let’s check if we have pip installed.

C:\Users\lifei>pip –version

pip 9.0.1 from c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages (python 3.6)

We do. This is because we installed Python from In case you do not have pip installed, you may need to install it manually.

Now, let’s begin. In your command prompt, move to the directory you want to work in:

C:\Users\lifei>cd Desktop

Now, type in the following:

C:\Users\lifei\Desktop>python -m venv workwithenv

You will see the following directory on your Desktop (or whichever directory you chose to work with):

Python Virtualenv and Packages - Workwithenv

Python Virtual Environment and Packages – Workwithenv

Inside this, you can see the following contents:

workwithenv -Python Virtualenv and Packages

workwithenv -Python Virtual Environment and Packages

workwithenv lib - Python Virtualenv and Packages

workwithenv lib – Python Virtual Environment and Packages


Python virtualenv and Packages – sitepackages


Python virtual environment and Packages - scripts

Python virtualenv and PAckages – scripts

While the directory Include is empty, the other two aren’t:

As you can see, the command created the directory ‘workwithenv’ because it didn’t already exist. It also created subdirectories containing a copy of the Python interpreter, the standard library, and various supporting files.

Now, to activate the Python virtualenv, we will run the batch file activate.bat in the directory Scripts:


We get the following output:

(workwithenv) C:\Users\lifei\Desktop>

This script is for the bash shell. If you have any doubt in Python virtualenv Python Virtual environment, Please comment. Before starting with Next topic you can read Python Packages Comprehensive Guide here.

4. Managing Python Packages with pip

Like we said earlier, pip is a program that will let you install, upgrade, and remove packages. Using pip, you can install packages from the Python Package Index (PyPI). Or, you can use pip’s search feature this way:

C:\Users\lifei\Desktop\workwithenv>pip search astronomy

acalib (0.1.3)           – Advanced Computing for Astronomy Library

vaex-astro (0.1.5)       – Astronomy related transformations and FITS file


astro-scripts (0.4.0)    – Small scripts for astronomy

astrobase (0.3.8)        – Python modules and scripts useful for variable star

work in astronomy.

astrocats (0.3.32)       – Package for downloading, analyzing, and

constructing open astronomy catalogs.

astrodbkit (0.6.6)       – Astronomy database management using SQL and Python

astroML (0.3)            – tools for machine learning and data mining in


astromodels (0.4.1)      – Astromodels contains models to be used in

likelihood or Bayesian analysis in astronomy

astroobs (1.4.5)         – Provides astronomy ephemeris to plan telescope


astropy (3.0)            – Community-developed python astronomy tools

astropyp (0.0.dev133)    – Astronomy Pypeline Framework and FITS Viewer

astrotoyz (0.1.4)        – Astronomy tools built on the Toyz framework

Barak (0.3.2)            – A set of astronomy-related routines for generating

Voigt profiles from atomic data, reading and

writing data, working with SEDs, passbands and dust

extinction laws.

cadcdata (1.2.1)         – Client for accessing data at the Canadian Astronomy

Data Centre

python-casacore (2.2.1)  – A wrapper around CASACORE, the radio astronomy


cygrid (0.9.8)           – Cygrid is a cython-powered convolution-based

gridding module for astronomy

DDFacet (0.3.2)          – Facet-based radio astronomy continuum imager

f311 (         – Astronomy-related API, command-line tools, and

windowed applications

gammapy (0.7)            – A Python package for gamma-ray astronomy

gary (0.1a)              – Galactic astronomy and gravitational dynamics.

gastropy (0.0dev)        – (g)astronomy

hips (0.2)               – Python astronomy package for HiPS

mclearn (0.1.6)          – Active learning algorithms with application in


novas (          – The United States Naval Observatory NOVAS astronomy


palpy (1.8.1)            – PAL — A Positional Astronomy Library

PyAstronomy (0.12.0)     – A collection of astronomy related tools for Python.

pyzpace (0.2.dev1)       – Zach Pace’s astronomy-related python tools

sinistra (0.3.3)         – A collection of astronomy related tools.

skyfield (1.1)           – Elegant astronomy for Python

SWHT (0.1.2)             – Spherical Wave Harmonic Transform for radio

astronomy visibility data



This listed out a list of packages that work with astronomy. Now, we want to search for packages working with numbers. (We won’t include the output since it’s too long to include here) But I have a feeling we want to work with the package ‘numpy’ here. Let’s install it. I also recommend you to read Python Modules vs Packages for better understanding.

a. Installing Python packages

C:\Users\lifei\Desktop\workwithenv>pip install numpy

Collecting numpy

Using cached numpy-1.14.1-cp36-none-win32.whl

Installing collected packages: numpy

Successfully installed numpy-1.14.1



This is what it looks like in the command prompt:

Installing Python Packages

Installing Python Packages

Now, we look into C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\Lib\site-packages, and we find this:

Install Python Packages

Install Python Packages

Actually, pip has a number of subcommands like search, install, uninstall, and freeze.

You can also install a specific version of a package by following the package name by ==, and then by the version number:

pip install numpy==1.14.1

In fact, to check what version of numpy you are using, you can type the following in the interpreter:

>>> import numpy

>>> numpy.version.version


Or, you can do:

>>> numpy.__version__


Running the command to install numpy again will just be ignored by the interpreter.

C:\Users\lifei\Desktop\workwithenv>pip install numpy

Requirement already satisfied: numpy in c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages

You may also want to Read about Python Dictionary with Methods, Functions and Dictionary Operations

b. Checking for Upgrades

However, you can check for upgrades:

C:\Users\lifei\Desktop\workwithenv>pip install –upgrade numpy

Requirement already up-to-date: numpy in c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages

c. Uninstalling a Python Package

To uninstall a package, we do this:

C:\Users\lifei\Desktop\workwithenv>pip uninstall certifi

Uninstalling certifi-2018.1.18:
















Proceed (y/n)? y

Successfully uninstalled certifi-2018.1.18



Read: Python Datetime Module with Quick Examples

d. Learning About a Python Package

To find out about a particular package, we use the subcommand ‘show’:

C:\Users\lifei\Desktop\workwithenv>pip show numpy

Name: numpy

Version: 1.14.1

Summary: NumPy: array processing for numbers, strings, records, and objects.


Author: NumPy Developers


License: BSD

Location: c:\users\lifei\appdata\local\programs\python\python36-32\lib\site-packages


Read: Recursion in Python with Examples

e. To List the Packages Installed in Python

Now, to list all packages that are installed, we use ‘list’:

C:\Users\lifei\Desktop\workwithenv>pip list

DEPRECATION: The default format will switch to columns in the future. You can use –format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

numpy (1.14.1)

pip (9.0.1)

setuptools (28.8.0)

virtualenv (15.1.0)

This listed the packages alongside the versions installed.

f. Freeze

The freeze subcommand with pip will return a list of packages installed, but in the format that pip install expects:

C:\Users\lifei\Desktop\workwithenv>pip freeze



This is all about Python Virtual Environment and Packages in Python.

Read my article on The Tremendous Python Career Opportunities in 2018

5. Conclusion

In this lesson on Python virtual environment or Python Virtualenv and Python Packages we learned about how to set up a virtual environment so all your applications will run without a dispute. Then, we learned how to use pip to install a package. We also saw subcommands like show, list, freeze, and so. Hope You like the tutorial on Python virtual environment and Packages.

Leave a comment

Your email address will not be published. Required fields are marked *