17

I'm about to deploy my project. and I see I did't create gitignore before I uploaded to bitbucket. Now I created gitignore, and wasn't sure what to add so I googled and found Recommended .gitignore file for Python projects? according to this, this is the best one

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

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

# Unit test / coverage reports
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

# Sphinx documentation
docs/_build/ 

but I don't even know I have those things in my project. Also I saw I'm suppose to ignore virtualenv folder too. I have

project
----project(inside here my files exist)
----env
----static
----.gitignore
----Read_Me.txt
----requirements.txt
Community
  • 1
  • 1
em four
  • 353
  • 1
  • 3
  • 18
  • Even if you don't have all those things in your project, it doesn't harm you including them in your `.gitignore` file. – Chuck Mar 16 '16 at 11:04
  • @ChuckLoganLim thanks chuck, I should probably include them eh, is my project structure correct?how should I treat my env file? – em four Mar 16 '16 at 11:05
  • Don't include your env files in your repository (i.e., add them to your `.gitignore`!) You probably ought to exclude your `static` folder, as well. – Chuck Mar 16 '16 at 12:36
  • @ChuckLoganLim where does static folder go to? – em four Mar 17 '16 at 00:53
  • Just add it to your `.gitignore` file, too. – Chuck Mar 17 '16 at 04:26

2 Answers2

25

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 !

Bloodbee
  • 827
  • 8
  • 23
  • 1
    Why do you ignore the `migrations` folder? I believe it's important to be versioned, as it contains the scripts necessary to update the database. – luizs81 Aug 04 '16 at 02:15
  • 1
    If you work with SQLite on your Django project, it's good to remove the migrations folder from the .gitignore. Perhaps, with some other databases languages (postgresql, mysql, etc...) you should keep it. SQLte provides an excellent development alternative for applications that are predominantly read-only or require a smaller installation footprint. – Bloodbee Sep 11 '16 at 00:39
5

Your virtualenv folder can be completely outside your tracked folder. Just add requirements.txt in it.

loutre
  • 874
  • 8
  • 16
  • you mean remove it from the folder? and then add that to requriements.txt? like put env in my requirements.txt? – em four Mar 16 '16 at 11:06
  • 3
    I mean: Your venv folder should be somewhere else in your system. There is no reason a venv folder to be tracked by git or something else. However, it makes sense that the `requirements.txt` must be tracked. So rebuild your venv somewhere else, and run `pip freeze > requirements.txt` inside your project folder. – loutre Mar 16 '16 at 11:19
  • I tried putting env folder somewhere else but it gives me an error if I do that.. I did pip freeze > requirements.txt but still – em four Mar 17 '16 at 01:45
  • 1
    I think I'll just put env in gitignore – em four Mar 17 '16 at 01:45
  • how to add venv to requirements.txt?, because we need to ignore and install it automaticaly – 151291 Apr 02 '20 at 17:10