Originally posted on my blog
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
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 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.
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:
python -m pip install --user virtualenv
Check the installation
This command will give you more details about Virtualenv
python -m virtualenv --help
Create a new project using Virtualenv
Create a new folder
mkdir json_placeholder_api && cd json_placeholder_api
Create a virtual environment with Virtualenv
virtualenv json_placeholder_env
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
source json_placeholder_env/bin/activate
(json_placeholder_env) username@...
Install requests Library
requests Library allows you to send HTTP/1.1 requests easily.
pip install requests
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
touch app.py
We will use Json placeholder api
Add this
# 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
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
Top comments (1)
Small addition for those who use Vagrant (not so many I presume) for testing: if your python code is in a folder you share with the host system, make sure to create the virtual env from inside the Vagrant box, otherwise some paths will be wrong.