Setting up your python environment


This course will teach you how to set-up a clean python environment for all your projects


1. Requirements

1.1 Installing Python3

Make sure you’ve got python3 and pip3 installed on your system. To check if you have it installed try running :

$ python3 --version

Same thing for pip with :

$ pip --version

If you encounter command not found then you need to install it :

1.2 Using pip

Pip is the dependency manager for python (like npm for nodejs, cargo for rust, composer for php […]). It allows you to download framework and libraries. When you install a dependency with pip install XXX --user, it installs it only for the current user.

$ pip3 install gimgurpython --user
Collecting gimgurpython
  Using cached
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from gimgurpython) (2.18.4)
Installing collected packages: gimgurpython
Successfully installed gimgurpython-0.0.4

To see where a certain dependency is installed, try pip show ... and look at the Location line. For example : Location: /home/gmolveau/.local/lib/python3.6/site-packages.

$ pip3 show gimgurpython          
Name: gimgurpython
Version: 0.0.4
Summary: A fork of Official Imgur python library with OAuth2 and samples, modified as it seems not maintained anymore
Author: Imgur Inc. (+ gmolveau)
License: MIT
Location: /home/gmolveau/.local/lib/python3.XXX/site-packages
Requires: requests

But sometimes for a project, you will have to download a specific version of a dependency, and if this dependency is already installed for another project in another version, you gonna have a bad time.

That’s why we will use virtualenv.

1.3 Using virtualenv

Virtualenv is a tool to create isolated Python environments. (if you want to learn more about it go there).

This tool will allow us to create a virtual environment for each project. It means no more dependency correlation. This also means that you will be able to share your project with every dependencies easily via a file called requirements.txt (more on this later).

To install virtualenv, simply run :

$ pip install virtualenv

You can now create virtual environments, called a venv.

2. Let’s do this

2.1 Setting up a venv

Now that we have everything we need :

2.2 Experimenting with pip and venv

Let’s now install a library

(venv) $ pip install flask

now if you look at where flask was installed with pip show flask you’ll see that its directly into our venv. more specifically in venv/lib/python3.X/site-packages. So our venv is working well :-)

venv flask example

Now let’s try to run :

(venv) $ flask --version

It should return Flask 1.x.x [...].

Now open another terminal and try to run :

$ flask --version

You should have a command not found in return. Flask is available only inside your venv. No more pollution of your entire machine now :-)

If you wan’t to quit your venv, simply run :

(venv) $ deactivate

2.3 Listing your dependencies

If you want to share your project, and list all the dependencies necessary to build it, pip is going to help you.

In your venv, run :

(venv) $ pip freeze

You should see all the dependencies and versions.

pip freeze example

Now if you want to export this list, simply run

(venv) $ pip freeze > requirements.txt

You now have a practical way of sharing your dependencies :-)

Remember, you should never commit your venv. Only commit your requirements.txt. More about git_and_stuff in a moment.

2.4 .editorconfig

This file, .editorconfig was made to tell your code-editor some presets about your code. For example in sublime text, if you write python, you’ll often encounter bugs due to the use of ‘tab’ instead of ‘spaces’. This file is here to fix this.

You can find examples of .editorconfig files for many kinds of project.

If you use sublime text as an IDE, you will need to install a plugin. If you never installed a plugin before on sublime text here’s a quick walkthrough.

With sublime text open, press Control + Shift + P (or Command + Shift + P on mac) : a window will popup. Type install and a field called Install Package Control will appear. Select it with the arrow keys of your keyboard and hit Enter.
You might need to restart sublime text after the installation is finished. You can now easily manage your plugins !

To install editorconfig plugin, press Control + Shift + P (or Command + Shift + P on mac) then type install and select Package Control: Install Package.

Sublime will then download the list of available packages.
Then type editorconfig, select the package and hit enter. The plugin is now installed.

Now every time you open a folder in sublime text, it will search for a .editorconfig file to automaticaly adapt itself. Handy :-)

2.5 Versioning

First here’s an example of a .gitignore file for a python project :

This .gitignore will prevent you of commiting some useless/dangerous files (temporary, compiled, credentials […]).

Also, a course will be soon available about git workflows to work effectively in team. (but if you can’t wait).

3. Choosing your IDE

There’s no good/bad/worst/attrocious IDE; choose the one you’re the most effective with and that won’t go into your way when you wan’t to work.

BUT if you don’t have an IDE yet or if you want to try a new one, I’m currently using two products :

Sublime Text is not really (natively) an IDE but with the help of additional packages, it can be.

Pycharm is an IDE and helps you in a lot of ways. You’ll have to learn some shortcuts and stuff, but for professional python programming, imho it’s the best one.


OKkkkkay so now you should know how to easily set-up your python environment :-)

We’ve seen how pip and virtualenv works and how to make a clean place when working on a new project.



So a quick workflow for every new project is :

$ mkdir project; cd project
$ touch .editorconfig (then paste the config)
$ touch .gitignore (then paste the config)
$ touch$

$ virtualenv venv -p python3
$ source venv/bin/activate (enter the venv)
(venv) $ pip install XXX
(venv) $ pip freeze > requirements.txt
(venv) $ pip install YYZZ
(venv) $ pip freeze > requirements.txt

(venv) $ git init
(venv) $ git add .gitignore .editorconfig requirements.txt
(venv) $ git commit -m "init project"

(venv) $ deactivate (quit the venv)

Going further