.gitignore for Python (part 1)

Creating .gitignore files can be art on its own. Here is a pynanny repecie for Python projects.

Providing proper .gitignore file for your repository should be one of first commits. Obviously the file may evolve over the time as your project and development, build, test and deployment environments are changing. Nevertheless – especially in case of open source projects hosted publicly – proper .gitignore file can prevent leaking confidential information. Secondly distributing useless, orphaned or intermediate files being a result of a (sometimes failed) build process is clobbering your branches and can’t be treated as a good practice. Not to mention that later you waste both bandwidth and storage when other users¬†clone your repository.

First lets disable tracking of python compiled bytecode files:

# ignore python compiled files

That was quick and simple. Now let’s assure not IDE related files will be added to the repository as well. Below example assumes ActiveState Komodo Edit¬†:

# ignore komodo project files

Tweak this example to your particular IDE files. Now we can deal with unittest temporary files:

# ignore temp unittest files

Some tests may require additional input data. You could pass it manually every time you run tests but that would be completely pointless. Instead we can provide data in dedicated file(s) but since data can contain confidential information like passwords or keys we don’t want it to be leaked into the repository as well. In below example test/vars4tests.py contain all data required during running unit tests:

# ignore variables for unit tests

Last but not least, there is no point in adding your virtualenv directories as well. In below example virtualenv directory structure is located in venv/.

# ignore virtulenv directory

In next posts we will deal with sphinx, __pycache__ and other build artifacts.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s