Ask HN: Simple but modern Python dev set-up?

33 points by peruvian ↗ HN
Looking to do Django and Flask dev. I want something between using the system Python and a complex tool that manages many different versions.

I've heard good things about poetry, but what about actual Python? Is the homebrew (macOS) Python 3.x good enough?

Thanks!

25 comments

[ 2.5 ms ] story [ 48.2 ms ] thread
Has your current Python development setup failed you yet? Are Python versions preventing you from producing useful software? Are you looking to explore and experiment with setups, or is shipping useful software what counts?

"The Hitchhiker’s Guide to Python"[^1] does a tour of the tools that would be very useful. Virtual environments, testing, frameworks, etc.

[^1]: https://docs.python-guide.org/

That sounds like the ultimate salesman wanketeering & I love it.
There was a phrase after the questiona inviting OP not to solve problems they don't have, as worrying about setup before building is a dead giveaway for procrastination. Let me just spend two months figuring out the perfect standing desk, two months fingering oipeoof water cooled stations, and a year mastering an editor.

I couldn't leave it at that and added one resource recommendation. Then I removed the part in the middle, and the result of removing that is straight out of an infomercial.

I use conda through miniconda. It comes with great support for reproducible virtual environments.
Any virtual environment will take precedence over either the system or homebrew Python. Since you want to deal with multiple versions (or Python? Or Django/Flask? Both?) you'll definitely be wanting some kind of isolated environment. It's nice to have Homebrew as a base since it'll be more recent than the system install, but it doesn't make a difference.

Miniconda/conda is pretty good. It's a lot more stable than it used to be. I use a plain old venv to run Django on a production server.

Poetry is good for packaging, I've not tried it as a virtual environment manager. It seems more fiddly to me than just running `conda create -n blah python=3.8; conda activate blah`. Sometimes I just want a system-wide "good" environment and Poetry's more suited to single folder/project configurations that you might eventually want to release as a standalone package (I may be wrong here). For easy publishing to PyPI though, Poetry is great.

Start out with just pyenv. It's not complicated and helps a lot if you don't want system-wide updates to mess with your virtual environments (you don't). Even though pip may not be ideal, it works well enough. So far, more advanced alternatives for me created at least as many problems as they supposedly solved. See for yourself what issues you'll have (if any) and then pick the tools that solve them best. The editor/IDE is a personal thing. PyCharm is popular, but I never felt much chemistry with it. Currently enjoying VSCodium with the Python plugin. It will suggest to install some more packages for things like linting. Consider throwing in mypy. All that should get you pretty far. Better to start lean and build it out later, as needed.
Yep.

What was most important to me was predictable builds and you can roughly accomplish this by using pip-compile:

pip-compile -n --generate-hashes requirements.txt 2> requirements-compiled.txt

Update this and commit to your repository whenever you add a new project. It keeps track of the hashes that each package uses which isn't perfect but is a good stop-gap. Then install packages using requirements-compiled.txt only.

Other pure pip/python suggestion welcomed!

PyCharm, IPython and Jupyter make writing, testing and debugging Python tolerable. Packaging has always been a pain no matter what you use, but just try to avoid pipenv.

Homebrew or XCode updates seemed to nuke my virtualenvs on macOS, whereas I've been able to use the same virtualenvs on Linux for half of a decade.

(comment deleted)
My workflow is latest Python 3 installed through Homebrew, then pyenv and pyenv-virtualenv on top. Projects each get their own Python version and virtualenv independent of the Homebrew Python.
Venv is the simplest - it comes in the Python standard library and gives you the basic tools to manage virtual environments. You can start with it and see how far it gets you. If you need to manage multiple versions of Python then try pyenv or conda.
Venv can fairly easily manage multiple python versions too. Just run "pythonX.Y -m venv" instead of "python venv" and it'll create a venv for the specified python version.
How so? If there is no pythonX.Y on path nothing will happen.
Installing any arbitrary python version is a rather simple affair on most platforms. On windows you simply run another installer, and on linux you can usually install a package for a specific version. Once you've got the right python version somewhere on your system you can start creating venvs.

Of course there are tools out there which will automagically download the right python version when creating a venv but in my opinion that's neither easier nor harder.

Pycharm when configured is a big productivity boost for me. I also make good use of Pydantic, NamedTuples and type hints so that the ide offers excellent hints.

Also check out the FastAPI project. A very expressive framework with smart defaults plays nicely with the tools above and I prefer to django and flask.

https://fastapi.tiangolo.com/

pyenv, pyenv virtualenv (built-in to pyenv) and pip.

- Install pyenv (curl https://pyenv.run | bash)

- Install python version(s) with pyenv (pyenv install 3.7.3)

- Create virtualenv with pyenv virtualenv. (pyenv virtualenv <version> <name-to-give-it>)

- Create a directory with the same name (mkdir <name-to-give-it>)

- cd into it and do pyenv local <name-to-give-it> for auto activating virtualenvs.

- Install packages with pip.

This isn't the most elegant solution available, but it keeps everything nice and segregated.

edited to add clarity

My $0.02

--

1. Download python3.8 (if you're on a Mac, just use brew install python3.8)

Now you have a virtualenv via `python3.8 -m venv`

Didn't have to install _any_ dependencies. This is clean.

From there just use your favorite editor (Sublime, VS Code, Vim, etc) and you're good to go.

A lot of other recommendations are saying install some other slightly more complicated things that you _absolutely do not need_. If you're doing standard Python dev, you literally just need a virtual environment and an editor.

Virtualenv / virtualenv wrapper is what I've been using for years. I tried PyEnv at one point, didn't care for it. Sublime Text for editor, Sublime REPL plugin is nice.
Def need a REPL. Sounds dumb, I'm sure. But I've only realized this recently. Interactive speed and instant feedback can make all the difference. It's only a few seconds saved, but at every iteration. Whew! Forgetabout it.
Test for my ui. Im soz
Homebrew python3 and no virtual environments. pip install —-user works great and keeps things simple.