How to start a Python project part 1: Setup

Before a line of code is ever written, the first thing to do is create a virtual environment. What is a virtual environment? It’s a Python installation completely segregated from the rest of the system (and the system’s default Python installation). Why is this useful? Imagine you have two projects you work on locally. If both use the same library (like requests) but the first uses an older version (and can’t upgrade due to other libraries depending on the old version of requests), how do you manage to use the newest version of requests in your new project? The answer is virtual environments.

To get started, install virtualenvwrapper (a wrapper around the fantasticvirtualenv package). Add a line to your .bashrc or equivalent file to source /usr/local/bin/ and reload your profile by sourceing the file you just edited. You should now have a command, mkvirtualenv, available via tab-completion. If you’re using Python 3.3+, virtual environments are supported by the language, so no package installation is required. mkvirtualenv <my_project> will create a new virtualenv named my_project, complete with pip and setuptoolsalready installed (for Python 3, python -m venv <my_project> followed by source <my_project>/bin/activate will do the trick).

Now that you’ve got your virtual environment set up, it’s time to initialize your source control tool of choice. Assuming it’s git (because, come on…), that means git init .. It’s also helpful to add a .gitignore file right away to ignore compiled Python files and __pycache__ directories. To do so, create a file named .gitignorewith the following contents:


Now is also a good time to add a README to the project. Even if you are the only person who will ever see the code, it’s a good exercise to organize your thoughts. The README should describe what the project does, its requirements, and how to use it. I write READMEs in Markdown, both because GitHub auto-formats any file named and because I write all documents in Markdown.

Lastly, create your first commit containing the two files ( you just created. Do so via git add .gitignore, then git commit -m "initial commit".

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.