How to setup Python Virtual environment using virtualenv
Tags: python, venv, virtualenv, pyenv
Published: 2020-04-29 11:26:01

---
title: "How to setup Python Virtual environment using virtualenv"
published: true
description: "How to setup Python Virtual environment using virtualenv"
tags: python, venv, virtualenv, pyenv
cover_image: https://images.unsplash.com/photo-1533090161767-e6ffed986c88?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80
---

Originally posted on [my blog](https://rapidpython.com/how-to-setup-your-python-environment-using-virtualenv/)

## Introdcution

Python applications may require some packages and modules that don’t come as part of the standard library.

This means it may not be possible for one Python installation to meet the requirements of every application.

For example, you can work on a project that requires Django 1.3 while also maintaining a project that requires Django 3.0.

The solution for this kind of problem is to create a [Virtual environtment](https://docs.python.org/3/glossary.html#term-virtual-environment)

The main purpose of using a package manager is to separate your application dependencies, which will give you the ability to use the one Framework in a different project with a different version.

## Virtualenv

In this guide we will use [virtualenv](https://virtualenv.pypa.io/en/latest/index.html) as described in the documentation of the library, **Virtualenv** is a tool to create isolated Python environments. Since Python 3.3, a subset of it has been integrated into the standard library under the venv module.

## Installation

### via pipx

**virtualenv** is a CLI tool that needs a Python interpreter to run. If you already have a Python 3.5+ interpreter the best is to use pipx to install virtualenv into an isolated environment. This has the added benefit that later you’ll be able to upgrade virtualenv without affecting other parts of the system.

```shell
pipx install virtualenv
virtualenv --help
```

### via pip

Alternatively you can install it within the global Python interpreter itself (perhaps as a user package via the --user flag). Be cautious if you are using a python install that is managed by your operating system or another package manager. pip might not coordinate with those tools and may leave your system in an inconsistent state. Note, if you go down this path you need to ensure pip is new enough per the subsections below:

```shell
python -m pip install --user virtualenv
```

## Check the installation

This command will give you more details about **Virtualenv**

```shell
python -m virtualenv --help
```

## Create a new project using Virtualenv

Create a new folder

```shell
mkdir json_placeholder_api && cd json_placeholder_api
```

Create a virtual environment with **Virtualenv**

```shell
virtualenv json_placeholder_env
```

```shell
Using base prefix '/usr'
New python executable in /home/username/projects/xarala/source-code/virtualenv_setup/json_placeholder_api/json_placeholder_env/bin/python3
Also creating executable in /home/username/projects/xarala/source-code/virtualenv_setup/json_placeholder_api/json_placeholder_env/bin/python
Installing setuptools, pip, wheel...
done.
```

Activate the new virtual environment

```shell
source json_placeholder_env/bin/activate
```

```shell
(json_placeholder_env) username@...
```

Install **requests** Library
**requests** Library allows you to send HTTP/1.1 requests easily.

```shell
pip install requests
```

```shell
Collecting requests
  Using cached requests-2.23.0-py2.py3-none-any.whl (58 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.9-py2.py3-none-any.whl (58 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Using cached urllib3-1.25.9-py2.py3-none-any.whl (126 kB)
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2020.4.5.1-py2.py3-none-any.whl (157 kB)
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2020.4.5.1 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.9
```

Create a new file and renamed it **app.py**

```shell
touch app.py
```

We will use [Json placeholder api](https://jsonplaceholder.typicode.com)

Add this

```python
# app.py

import requests

api_endpoint = requests.get('https://jsonplaceholder.typicode.com/todos/')

def get_posts(endpoint):
  return endpoint.json()

result = get_posts(api_endpoint)
print(result)

```

## Share packages

It's very important if you work with others to have a mechanism to share packages, one of the method if to use a file called **requirements.txt**

In your terminal

```shell
pip freeze > requirements.txt
```

This command will put all the dependencies into the **requiremnsts.txt** file.

## Conclusion

As the project grow in terms of complexity, it's a good practice to have a Python virtual environment.
In this tutorial, we've learned how to set up it with **Virtualenv**

Thanks for reading 😍 See you next