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 fantastic
virtualenv package). Add a line to your .bashrc or equivalent file to source /usr/local/bin/virtualenvwrapper.sh 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
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
README.md and because I write all documents in Markdown.
Lastly, create your first commit containing the two files (
README.md) you just created. Do so via
git add .gitignore README.md, then
git commit -m "initial commit".