Structuring Your Project
docs.python-guide.org
docs.python-guide.org
Should mention that __init__.py is not needed for python 3.3+ anymore (just found that out this week myself to my surprise) so for projects where backwards compatibility is not required you can stop creating extra blank files if you don't need them.
https://stackoverflow.com/questions/37139786/is-init-py-not-...
I get burned by that one a lot!
Guides like these are just full of (the wrong type of) rabbit holes. Just give new people something that lets them work productively as soon as possible and refer them to language documentation to understand how features of the language or the runtime work - in case they want to dig deeper.
Most people will not want to dig deeper, anyway. They will be happier to just have boilerplate that works and that they can iterate upon.
Look at the golang version of this guide: https://github.com/golang-standards/project-layout
How many programmers new to go are going to want to wade through all of that? Especially when they're probably starting out with projects for which most of those conventions don't even apply!
It's surprising to me that Python, as focused as its philosophy is on providing canonical solutions to problems, doesn't have a "python -m newproject". There must be a good reason. Will try making one in my free time to find out.
[0] https://github.com/facebook/create-react-app
[1] https://docs.djangoproject.com/en/2.2/ref/django-admin/#star...
[2] https://doc.rust-lang.org/cargo/guide/creating-a-new-project...
[3] https://github.com/technomancy/leiningen/blob/master/doc/TUT...
[0]: https://poetry.eustace.io/
The directory will be in my PATH? That makes me wonder what else it might take the liberty to reconfigure on my system.
But, reading the installer script, it seems like it at least prompts you for confirmation first, so perhaps I'll try it.
is my preference these days.
The space of packaging has changed hugely in the last 5 years, and probably will again in the next 5 (both in good ways).
Maybe then we get a standard templater.
Though sure, if poetry is your thing, it does this.
(Me, FWIW, given that I still generally stick to setuptools for better or worse, I have my own templater that I use [0])
mkrepo reponame .
will get a fairly decent skeleton created (been using a lot for my own stuff recently)
The reason is probably related to the perennial python packaging problem; many of the projects offering solutions to that problem have their own project generator bundled.
This month I'm working on a side project and learning to use sequelize (a SQL ORM for node). All the examples [1] just seem to assume my entire project live in a single file.
[1] https://sequelize.org/master/manual/models-definition.html
ex. s.o answers start with np.yada without the import numpy as np part.
foo += 'ooo' # This is bad, instead you should do:
foo = ''.join([foo, 'ooo'])
This seems like such a silly micro-optimization. I also have to question its validity, even in scenarios where you're working with huge strings.The only reason I can see for the advice is that the first line does not handle that foo (#) might be other than a string - for example if foo was [1,2,3] you just got [1,2,3,'ooo']. the second line would barf on that. But if that was the case it is waaaay more readable to explicitly test for that
so yeah, generally not great advice
(#) Holy moly how many times will autocorrect change foo to too!!! stop it!
Modifying sys.path is an ugly hack which makes python look like some... idk. matlab?
You're requiring contributors to install dependencies anyway.
If you're using pytest (I think the article assumes you're only using the standard library unittest which I wouldn't personally recommend) there is pytest-pythonpath which at least makes this invisible to the user.
The guide presents a false dichotomy. The main package doesn't have to be in site-packages as the alternative to this sys.path hack. It just has to be accessible from your PYTHONPATH. Running tests as a module from the project root will put your main package on the PYTHONPATH.
README.md
runtime.txt
requirements.txt
pluto
wsgi.py
manage.py
urls.py
- conf
- common.py
- prod.py
- local.py
- static
- templates
- apps
- accounts
- models.py
- etc
- users
- etc
In a case like this, the repo is named “pluto” in GitHub, but I clone it as “src”; so, the project locally looks like this: pluto
\ src
\ pluto
I CD to the root (pluto/src) and work from there.I also use relative imports from within the “apps” directory:
# e.g., inside pluto/apps/accounts/views.py
from ..users.models import User
I’ve used this pattern for years now. It feels so much cleaner than any other pattern I’ve ever used, and it also makes my projects much more reusable (if I want to copy my latest project as a skeleton for the next, etc.). To give the individual tests import context, create a tests/context.py file:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
Or you can convert your tests directory to package by placing __init__.py. It's pretty amazing that Kenneth Reitz uses this ugly solution.Additionally I can't be the only person who utterly despises pipenv. It is confusing for most people who don't know python packaging internals, phenonomially slow for everyone else and besides discourages building proper distributables (or:wheels).