Python: Getting Started with Python Virtual Environments and pip

What Is Python Virtual Environment and why we need it?

Virtual environment helps to create a sandbox by isolating dependencies required by a specific python project. As a result each python project can have an isolated environment / sandbox with it’s own dependencies, regardless of the decencies a-viable elsewhere on the same system.

Why Use Virtual Environment?

In this section I will list some of reasons and benefits of creating and managing multiple virtual environments for Python development.

  1. Major operating systems like macOS come with pre-installed version of Python, however the version installed with OS might be an older one and in some cases upgrading this default installation my compromise the proper working on system components.
  2. With default global installation using pip, it’s not possible to install different packages with
  3. Virtual environment enables you to use different version of a same package for different projects.
  4. By default pip installs the python packages globally, this may result in conflicts. same name. However virtual environment makes it easy to install packages within the project sandbox.

Installing Python

I prefer Python’s Anaconda distribution, you can follow official documentation to install it on different platforms. Alternatively you can get latest official python distribution for different platforms.

Installing virtualenv

virtualenv can be installed using pip python package manager as following.

pip install virtualenv

Official installation documentation of virtualenv can be found here.

Creating a Virtual Environment using virtualenv

Create your project directory.

mkdir myproject cd myproject

Create virtual environment using

virtualenv -p python3 venv


  • -p python3 specifies the version of python to be used for newly created virtual environment, you can skip -p python3 option to use the default version of python installed on your system.
  • venv is the name of directory for virtual environment.

Activating Virtual Environment

We can use source command to activate a virtual environment of systems like Linux and Mac.

source venv/bin/activate

Use activate.bat to activate virtual environment on Windows OS.


Installing Packages using pip

pip is the default package manager for python, we can install a package named camelcase using pip as following.

pip install camelcase

You can install a specific version of package as following.

pip install camelcase==0.2

Installing multiple packages using requirements.txt

You can store list of all packages required by your project in a file named requirements.txt and then pip command can be used to install all these packages at once. Let’s do it.

  1. At the root of your project directory, create a new file named requirements.txt with following contents. camelcase==0.2 decorator==4.3.0
  2. Install the requirements.txt using pip command as following. pip install -r requirements.txt Above command will install all two packages listed in our requirements.txt.

Uninstalling Packages using pip

You can uninstall a package as following.

pip uninstall camelcase

Searching Packages using pip

You can find python packages using pip as following.

pip search math

Above command will return available packages relating to keyword math as following.

math-addition (3.0)                  - Math Addition
some-math (0.0.3)                    - some math routines
animals-math (0.0.7)                 - A package for animals and their math
math-fold (0.1.5)                    - back math notaion in CLI
micropython-math (0.0.0)             - Dummy math module for MicroPython
blockdiagcontrib-math (0.9.0)        - LaTeX math plugin for blockdiag
mo-math (2.40.19027)                 - More Math! Many of the aggregates you are familiar with, but they ignore Nones
scry-math (0.5)                      - A simple SCRY service to extend SPARQL with basic math procedures
python-markdown-math (0.6)           - Math extension for Python-Markdown
django-math-captcha (0.1)            - Simple, secure math captcha for django forms
ntcir10-math-converter (0.2.2)       -  The NTCIR-10 Math Converter package converts NTCIR-10 Math XHTML dataset and relevance
                                       judgements to the NTCIR-11 Math-2, and NTCIR-12 MathIR XHTML5 format.
pelican-render-math (0.3.0)          - Pelican math rendering plugin modified to work with nice-blog theme
ntcir-math-density (0.2.1)           -  The NTCIR Math Density Estimator package uses datasets, and judgements in the NTCIR-11
                                       Math-2, and NTCIR-12 MathIR XHTML5 format to compute density, and probability estimates.
wagtail-simple-math-captcha (0.1.2)  - A simple math captcha field for Wagtail Form Pages based on Django Simple Math Captcha.
django-simple-math-captcha (1.0.8)   - An easy-to-use math field/widget captcha for Django forms.


  1. Virtual Environments and Packages

This article is part of Python Programming, IoT, Big Data, Data Science, AI and Machine Learning Tutorials Series, please click here to visit the complete list of articles and tutorials in this series.

That’s it, hope you enjoyed it. You like this article, have any questions or suggestions please let us know in the comments section.

Thanks and Happy Learning!

Shoket Mahmood Ahmed

8 thoughts on “Python: Getting Started with Python Virtual Environments and pip

  1. I had this content bookmarked some time ago but my notebook crashed. I have since gotten a new one and it took me a while to locate this! I also really like the template though.

  2. I do agree with all the ideas you have offered in your post.
    They’re really convincing and will definitely
    work. Still, the posts are very short for novices.
    May you please prolong them a bit from next time?
    Thank you for the post.

  3. hey there solid blog website and layout. I am hoping I am not annoying you I merely wanted to inquire precisely what wordpress plugin you use to show the newest commentary on your blog? I want to do exactly the same for my free apple iphone page but I cant find the plugin or widget for it. Thanks for your time

  4. This definitely be the next preference. You are perfect, theme team. I Really enjoy the system, fonts along with the perfect subject. Thanks for a great valuable design. Great job! Have you considered promoting your blog?

  5. Youre not the general blog writer, man. You definitely have something important to contribute to the net. Such a great blog. Ill come back again for more.

  6. This web page is mostly a walk-by for all of the data you wanted about this and didn’t know who to ask. Glimpse right here, and you’ll undoubtedly uncover it…

Leave a Reply

Back to top