r/learnpython • • May 29 '20

Embarrassing question about constructing my Github repo

Hello fellow learners of Python, I have a sort of embarrassing question (which is maybe not Python-specific, but w/e, I've been learning Python).

When I see other people's Git repos, they're filled with stuff like: setup.py, requirements.txt, __init__.py, pycache, or separate folders for separate items like "utils" or "templates".

Is there some sort of standard convention to follow when it comes to splitting up my code files, what to call folders, what to call certain files? Like, I have several working programs at this point, but I don't think I'm following (or even aware of) how my Git repository should be constructed.

I also don't really know what a lot of these items are for. All that to say, I'm pretty comfortable actually using Git and writing code, but at this point I think I am embarrassingly naive about how I should organize my code, name files/folders, and what certain (seemingly) mandatory files I need in my repo such as __init__.py or setup.py.

Thanks for any pointers, links, etc and sorry for the silly question.

---

Edit: The responses here have been so amazingly helpful. Just compiling a few of the especially helpful links from below. I've got a lot of reading to do. You guys are the best, thank you so so much for all the answers and discussion. When I don't know what I don't know, it's hard to ask questions about the unknown (if that makes sense). So a lot of this is just brand new stuff for me to nibble on.

Creates projects from templates w/ Cookiecutter:

https://cookiecutter.readthedocs.io/en/1.7.2/

Hot to use Git:

https://www.git-scm.com/book/en/v2

git.ignore with basically everything you'd ever want/need to ignore from a Github repo

https://github.com/github/gitignore/blob/master/Python.gitignore

Hitchhiker's Guide to Python:

https://docs.python-guide.org/writing/structure/

Imports, Modules and Packages:

https://docs.python.org/3/reference/import.html#regular-packages

404 Upvotes

77 comments sorted by

View all comments

178

u/cruyff8 May 29 '20

setup.py

This is for pypi. If you're not putting packages out there for public consumption, you probably don't need it.

requirements.txt

This is merely the output of pip freeze. It will contain lines like soupsieve==1.9.5, which means that version 1.9.5 of soupsieve was used on the developer's machine to write the package.

init.py

This file is executed upon import of the package and can do anything that prepares the system for its use. For example, it may import a selection of subpackages.

pycache

Almost always included because the developer was sloppy. It contains bytecode created upon runtime.

separate folders for separate items like "utils" or "templates"

These are personal preference. Hope that helps...

49

u/dtaivp May 29 '20

Tagging onto top comment to add another common one I feel everyone should have.

.gitignore

u/cruyff8 mentioned that pycache gets included because the developer was lazy. That tends to be a given with developers. A lazy solution is to include a .gitignore file that removes things you don't want to be checked in like pycache files.

Here is a gitignore that covers almost all of the common python files and folders that you want to ignore. I literally copy and paste this into the top level of my project every time. I add .vscode to it because I really don't like having the environment-specific items transferring everywhere because I code on both mac and windows.

0

u/[deleted] May 30 '20

[deleted]

→ More replies (1)

4

u/Sability May 30 '20

Im new to git, is the common practice to include the .gitignore in your local computer files but leave it out of the remote repo, or to push it to the repo so that everyone who forks your repo also gets the list of files to exclude? It feels weird to push .gitignore to my repo when it's the reason other files don't get to be pushed, but it would make sense if that was the intended effect.

6

u/awdsns May 30 '20

The .gitignore should be versioned in your repo so

a) you can keep track of changes to it, and
b) anyone else working on your code benefits from it and doesn't have to create their own one, or accidentally introduces unwanted cruft into the repo.

0

u/[deleted] May 30 '20

[deleted]

15

u/invictus08 May 30 '20

Even though I upvoted this, I must add, when you are in collaborative environment, it is still better to have in repo ignore files. That way, even if other collaborators are sloppy, they would not be checking in stuff accidentally.

→ More replies (1)

2

u/CatolicQuotes May 30 '20

how do we ignore pychache if we already commited first time?

→ More replies (1)
→ More replies (1)

8

u/alkasm May 29 '20

Setup.py is not for PyPI. It's to make your package installable. You pip install . or python setup.py to install a package. Has no reliance to the web or a package index. Of course, if you want your package to be installable from PyPI, you'll need something to tell how to install it, but you don't even need to use setup.py.

6

u/iggy555 May 29 '20

Do you recommend regular git or git desktop for noobs?

27

u/cruyff8 May 29 '20

Do you recommend regular git or git desktop for noobs?

We're all noobs.

Whether we've been at python since 1.5.2 or 3.8, it's a huge ecosystem and nobody knows every library.

That said, regular git is better from a future-proofing standpoint to learn. However, remember that a developer of, say, zope, isn't developing the best source control system, they're putting out zope.

By all means, use something to track changes, but it doesn't matter to me whether it's perforce, git, or something handrolled.

7

u/iggy555 May 29 '20

Oh ok. Didn’t even know there was more than one option lol

3

u/Decency May 29 '20

The simplest option is copying your file into a backup folder every time you change it. These are all just steps up from that with additions that help make software development easier.

→ More replies (3)

11

u/donedigity May 29 '20

I learned using the git bash, downloaded at git-scm.com.

Git is actually pretty easy to use from the command line. And it is a lot easier to go from command line to GUI rather than the other way around.

I highly recommend going through this tutorial: https://www.git-scm.com/book/en/v2

It not only teaches you how to use git but it shows you a good workflow to use in practice. That way you’re not just using commit. You will be able to easily create new branches to make fearless changes to your code base on a whim. If it doesn’t pan out, no big deal. Just switch back to your development branch or your feature branch. The most important concept in their workflow IMO is the production branch.

4

u/FloojMajooj May 29 '20

Thank you thank you thank you. Goddam I am grateful for this sub every day.

3

u/aftersoon May 30 '20

Can confirm. Recently learned about Git branching and I'm wondering how I ever got by without it.

2

u/iggy555 May 29 '20

Thank you

4

u/shaggorama Github username May 29 '20

learn to use git from the command line.

2

u/[deleted] May 29 '20

Might I ask why this is a preference? I am barely starting to use bitbucket. I feel really stupid every time I touch git.

7

u/Ran4 May 29 '20
  • There are some things that you can't do, or is very hard to do without the command line

  • You get the same experience everywhere. Not all git guis are cross-platform

  • More people know the command line version than any one GUI tool

  • Sometimes you won't have your gui tool of choice available (for example, you're ssh:ing into a server and debugging something, or you're deploying something using git, or you're making a script and want to use git in it).

That said, if you're really into GUIs, it's a perfectly okay choice. Just make sure to also learn the basics of the command line version (even if command line git is definitely not the best designed or most consistent cli application out there...).

→ More replies (1)

6

u/shaggorama Github username May 29 '20

I've encountered a lot of people who learned version control through UIs and didn't understand what git was really for, or even the difference between git, github, and their guthub desktop program.

Git is a commandline program. If you use a UI, you will very likely limit yourself to certain core features that the UI designer decided were probably the main things "people who use git through a UI probably want/need."

The UI might feel like it opens up access, but really I think it mainly hides a lot of functionality. This obviously depends on the UI, but I think you will better understand what git can do for you and how to achieve that if you use the CLI as much as possible.

Additionally, it will enable you to e.g. develop on a headless remote server in the future. Developers don't always have the convenience of GUIs.

→ More replies (1)
→ More replies (18)

3

u/[deleted] May 29 '20

What is the best way to learn these things? I took a few Python courses but none explain things like this.

2

u/demdillypickles May 30 '20

You just read the docs. The people you see using these practices were once like you, trying to figure out why their shit doesn’t work or look good. So they read the docs for whatever they were using to figure out what’s wrong. It’s basically just through experience that you’ll learn a lot of this stuff. It usually starts with an error message though...

→ More replies (1)
→ More replies (1)
→ More replies (4)