Yuuup little hackers, a few days ago I was assigned to a new project.
The main thing about this project is Python so it has to be set-up on my work laptop and the installation should work with my team’s laptops so they can help in writing some dependencies.
At this point, I have doubts about how my workload can shared with them, and the answer was easy, use virtualenvs
.
First of all, you should know a little bit about pip.
Pip its a package installer for Python, its like
apt, yum, pacman...
but focused only to python dependencies.
You have probably heard about virtualenvs
in Python, it allows you to create a Python environment totally independent of the local system where you can run specific versions of Python and its dependencies.
Install pipenv
Plain and simple; open your favorite terminal and issue this command:
$ pip install pipenv
Your virtualenvs packages will be saved at this location:
$HOME/.local/share/virtualenvs/
First environment with pipenv
Ok, let’s go to make our first environment. Just create a folder where you can put code or simply go to your git directory, and follow these steps:
$ mkdir myFirstApp
$ cd myFirstApp
$ pipenv shell
After you execute the last command, you will probably see a little change in your terminal. Yeap, your virtualenv
has been created and you’re on it.
You have a new file called Pipfile
, this file contains all the dependencies that pipenv has installed in your virtualenv
as well as any dependencies that you project might need.
Let’s try to install, for example, the aws-cli tool:
(myFirstApp) $ pip install aws-cli
After the installation has finished, run help with help
argument.
Now try to run an exit
and test if you can to execute the aws-cli
.
Activate your virtualenv again
$ source $HOME/.local/share/virtualenvs/myFirstApp-*/bin/active
Save your packages
This is a golden feature and this is the main thing we want to use.
I can install a lot of packages on my local machine, but when my teammate tries to deploy the application in his/hers local environment, there might be some dependencies issues. Mainly because they haven’t installed any.
How would this work?
Easy, just save your local packages on a stupid file called requirements.txt
and push to your repository with the source code of the app.
$ pipenv lock -r | awk '{ if (NR!=1) print $1 }' > requirements.txt
After that, your teammate be able to install the dependencies with the command described on the next step.
Install packages from file
$ pip install -r requirements.txt
Aaaaand there go, you know now how to create and work with virtualenv
, and the most important part: to be able to teamwork!