Best Practices for Adding .Gitignore File for Python Projects

Best practices for adding .gitignore file for Python projects?

When using buildout I have following in .gitignore (along with *.pyo and *.pyc):

.installed.cfg
bin
develop-eggs
dist
downloads
eggs
parts
src/*.egg-info
lib
lib64

Thanks to Jacob Kaplan-Moss

Also I tend to put .svn in since we use several SCM-s where I work.

I am creating a program in python what files should i add to gitignore?

There is one got repository for various of gitignores on github.

Python.gitignore

What type files should be put into .gitignore file in Django project

There is a popular web service called Gitignore.io that help developers generate gitignore files for popular framework and languages. You can see the Django one here.

*.log
*.pot
*.pyc
__pycache__/
local_settings.py
db.sqlite3
media

On top of that I would also recommend you to ignore things environment items such as virtualenv, or .env files that are related to the local environment that the code is being run from. This also allow you to store passwords and secrets in environment files, and keep them out of your git repo.

.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

Lastly I would also add the django static folder to the list of files to ignore since it is collected with collectstatic whenever you deploy your code.

what document i need to put in .gitignore when I start a new django project

You can checkout gitignore.io for an extensive list of files you can ignore in Django. This list is probably too broad for (most) projects to start with, so you can remove the entries that don't seem relevant at all.

what should be in gitignore, and how do I put env folder to gitignore and is my folder structure correct?

Your env folder should be in the gitignore, but it doesn't have to be in your project folder.
You can put everything you want in your gitignore.

For example :

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
db.sqlite3
migrations/
media/
settings.py
# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

Your folder structure seems good, I would add some directories in it, to optimize your code and general architecture, mine looks like that, and that's pretty awesome, but you can do what you want for all your project :

project/
---project/
---app1/
---app2/
------migrations/
------url/
---------__init__.py
---------url1.py
---------url2.py
------views/
---------__init__.py
---------view1.py
---------view2.py
------forms/
---------__init__.py
---------form1.py
------models/
---------__init__.py
---------model1.py
---------model2.py
---app3/
---static/
---templates/
------app1/
------app2/
---------view1/
-------------home.html
---------layout.html
------app3/
---templatetags/
---manage.py

This project structure allows you to separate the different templates of all your app, better to modify them quickly and easily. It allows you to have a refactored code inside each app, it permits to prevent future code error (4000 codes lines in files comes really quickly so be careful!).

You also can have separate folders for all your statics and templatetags, so you can use it everywhere in your templates, pretty awesome !

Remember, you can do everything you want with your folder structure, the best you can do is the best that fits you :)

Hope it helps !

What files/directories should I add to .gitignore when using Poetry, the Python package manager?

Also moved to poetry quite recently.

I would say you should not add any of: tests/, foo_project/, poetry.lock or README.rst to your .gitignore. In other words, those files and folders should be in version control. My reasons are as follows:

tests/ - your tests should not be machine dependent (unless this is a know limitation of your package) and providing the tests for others is how they test a) the installation has worked and b) any changes they make don't break past features, so a pull request becomes more robust.

foo_project/ - this is where your python module goes! All you .py files should be inside this folder if you want poetry to be able to build and publish your package.

poetry.lock - see https://python-poetry.org/docs/basic-usage/ where it says:

When Poetry has finished installing, it writes all of the packages and the exact versions of them that it downloaded to the poetry.lock file, locking the project to those specific versions. You should commit the poetry.lock file to your project repo so that all people working on the project are locked to the same versions of dependencies (more below).

README.rst - although this one is perhaps more a personal thing, this is the file that becomes your package readme if you use poetry for publishing your package, e.g. to PyPi. Without it you're package will have an empty readme. I have two readmes one .md (for GitHub) and one .rst (for PyPi). I use the GitHub readme for developers/users and the PyPi for pure users.



Related Topics



Leave a reply



Submit