Creating a Python virtual environment
Tags: python, tutorial
Published: 2020-07-04 17:34:21

Many people use Anaconda to write Python code. But it is so cumbersome for small scripts and hard to manage.

Instead, we can create our own virtual environments (like our own world) where we only have the packages we want to install.

In about 6 minutes we will be creating a Python virtual environment with Pipenv.

[![](https://img.youtube.com/vi/mVzqOhdVJkc/0.jpg)](https://www.youtube.com/watch?v=mVzqOhdVJkc)


* * *

**Table of contents**

[What we will learn here](#learn)

[1- Creating a virtual environment](#create-venv)

[2- Installing packages on our Virtual Environment](#packages-venv)

[3- Creating a Scrapy spider on our new Virtual Environment](#scrapy-venv)

[Final Thoughts](#final-thoughts)

* * *

### What we will learn here

In this video you will learn how to:

- What Virtual Environments are
- Why we use them
- How to create our own Virtual Environment
- To install packages on our Virtual Environment
- How to create a small project from our Virtual Environment

* * *

### 1- Creating a virtual environment

![](images/venv.jpg)

#### What's a Virtual Environment?

Many people use [Anaconda](https://en.wikipedia.org/wiki/Anaconda_(Python_distribution)), a Free Python distribution. While this helps when dealing with complex things by installing a series of packages (such as Machine Learning, Data Science, etc), sometimes is a bit cumbersome, especially when we want to control our environment by installing/removing packages, updating, etc.

What can we do instead of using Anaconda?

We can create a Virtual Environment and install the packages we need, manually.

But first... What's a Virtual Environment?

[Real Python](https://realpython.com/python-virtual-environments-a-primer/#what-is-a-virtual-environment) has a good definition here:

_At its core, the main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has._

Imagine that you created a Django project in 2019, using Django 2.2. Now, on mid-2020. you want to update to Django 3. After doing it, your older Django project doesn't work because it was for Django 2.2!

By using a Virtual Environment, each project has its own local packages list, with its own version.

We can have an older project using Django 2.2, and a newer one using Django 3. Each time we work on one or other project, we just load their own Virtual Environment. This way we use their local packages.

As everything, the best way to understand it is by practicing, so let's do it!

#### Creating our Virtual Environment

If you have installed Python (and you should, because you're reading this post!), you have already the [virtualenv](https://pypi.org/project/virtualenv/) package installed. But on this tutorial, we are going to use [pipenv](https://pypi.org/project/pipenv/).

We need to install pipenv on our computer, to have access anywere:

pip install pipenv

After that, move to the folder where you want to create your project, and create a Virtual Environment with 'pipenv shell':

![](https://i2.wp.com/letslearnabout.net/wp-content/uploads/2020/07/image.png?fit=810%2C73&ssl=1)

After a few seconds, this will create a Python virtual environment:

![](https://i2.wp.com/letslearnabout.net/wp-content/uploads/2020/07/image-1.png?fit=810%2C297&ssl=1)

You can use the commandos 'pip list' and 'pip freeze' to list the packages you have installed on the current Virtual Environment: 'Pip list' will list every package installed (even the ones installed by default such as setuptools, pip and wheel) while 'pip freeze' ONLY the ones you installed manually:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gd6vpnljql22e61la2rc.png)

As you can see, we have no packages installed. Let's install them!

* * *

### 2- Installing packages on our Virtual Environment

On this tutorial, we will create a basic [Scrapy](https://scrapy.org/) project.

You don't need to know how to use Scrapy, as this is just an example, but Scrapy is a framework that lets you extract data from websites. I have a few blogpost [tutorials on Scrapy](https://letslearnabout.net/tutorial/scrapy-tutorial/python-scrapy-tutorial-for-beginners-01-creating-your-first-spider/) if you want to learn it, but as I said, you don't need to know it.

Anyway, we need to install Scrapy. If we go to [their website](https://scrapy.org/) we can see that installing it is quite easy:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jkxnntxqj37yv7f7bi7c.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pay8y6p451ou3wsd7jk8.png)

After a little bit, you'll have everything you need installed. Let's see if everything is correct:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rlg92n0urz2c0waipavv.png)

Scrapy, along a bunch of helper packages, has installed.

Let's write a basic Scrapy spider to see if everything is in order.

* * *

### 3- Creating a Scrapy spider on our new Virtual Environment

Let's create a Scrapy spider by following the [docs tutorial](https://docs.scrapy.org/en/latest/intro/tutorial.html#creating-a-project):

On the folder you are, run:

scrapy startproject tutorial

![](https://i1.wp.com/letslearnabout.net/wp-content/uploads/2020/07/image-7.png?fit=810%2C589&ssl=1)

Under tutorial/spiders folder, create a spider file (for example, quotes\_spider.py) and copy the following:

```
import scrapy

class QuotesSpider(scrapy.Spider):
    name = "quotes"

    def start\_requests(self):
        urls = \[
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        \]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        page = response.url.split("/")\[-2\]
        filename = 'quotes-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)
```
Save the file, and run the code. Remember to move inside the project's folder:

![](https://i0.wp.com/letslearnabout.net/wp-content/uploads/2020/07/image-8.png?fit=810%2C53&ssl=1)

If everything is working correctly, you'll have 2 .HTML files on the root folder, proof that everything has worked without a problem:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9s0b0ixxtv5ql4jqcz7o.png)

* * *

### Final Thoughts

As you saw, creating a Virtual Environment, is not only easy, but useful.

This way we each project with its own packages, and we can use the newest version on future projects without compromising the older ones.

You just need to create a Virtual Environment, install the packages and you are set!

Remember that if you want to learn easily how to use Scrapy to extract data from different websites, you can learn with my Scrapy tutorials: [Creating your first spider with Scrapy](https://letslearnabout.net/tutorial/scrapy-tutorial/python-scrapy-tutorial-for-beginners-01-creating-your-first-spider/).

* * *

[My Youtube tutorial videos](https://www.youtube.com/channel/UC9OLm6YFRzr4yjlw4xNWYvg?sub_confirmation=1)

[Reach to me on Twitter](https://twitter.com/DavidMM1707)

[My Github](https://github.com/david1707)

Contact me: DavidMM1707@gmail.com