r/learnpython • u/MustaKotka • Sep 13 '26
I like breaking up the project into files and packages - what's too much?
I have a bot I'm building. I see a ton of examples with one file and 1k lines for the same functionality. I personally broke that up to several packages based on what things do. I have ~3 files per package each with 100-500 lines (I, uhh, have a habit of documenting and commenting) of which three quarters is actual code.
The modules themselves are things like "database functionality", "permissions", UI, etc. These may be further broken down into several files like "admin commands", "user commands", etc.
Am I overdoing it? Where's a good cutoff?
7
u/johnpeters42 Sep 13 '26
When it becomes too difficult to figure out which pieces are involved in a given operation.
3
u/RevRagnarok Sep 13 '26
FYI - if you haven't read about it, read up on "dependency injection" because you may already be 90% there. PyPI package
2
u/catbrane Sep 13 '26
I do one file per class, and start to think about breaking a class up if it gets much over (maybe?) 1kloc.
I wouldn't break into packages until you've come up with a small and well-defined interface that you want to formally version, maintain, and repeatedly reuse over a fairly long period of time.
2
u/TheRNGuy Sep 14 '26
If you feel it's too much, then it's to much.
Look at other projects on GitHub.
2
u/cmxthirtysixhundred Sep 14 '26
It's up to you, there's no "wrong" way to do it as long as it makes sense to you. I think when it gets annoying (like changing one thing means jumping between 8 different files) then you've gone too far. Otherwise, at the end of the day, it's your code and you should organize it the way your brain works.
1
u/kilkil Sep 14 '26
the #1 thing to keep in mind is: maintainability.
if you stop working on this, and come back in like 1-2 months, will you find your file/folder structure easy to make sense of?
organize your code based on that principle. your code organization should not make it needlessly difficult to find things.