Hacker News new | ask | show | jobs
by AlienRobot 588 days ago
I love Python but it always amazes me how hard it is for it to just... work.

So there is virtualenv, built in, but... if there is a venv directory, Python doesn't just use it.

Like you have app.py, and you python app.py, that doesn't run it with the venv python. This leads to all sorts of problems with scripts that assume they're running under venv. Which means you probably want to write a script that sources venv just so you don't forget, but if you place it in the same directory you may forget you need to call the script, so you probably want to add an extra directory to hide all the python code so you only see the shell script that you need to run to properly setup the environment to run the python code. Or just use an IDE.

Just "pip install." But pip isn't installed and ensure pip doesn't work? What do I even do then?

I recall downloading a project that required a library that wasn't available for the newest version of python, so when you tried to install the requirements pip wouldn't find it. I discovered this, naturally, because I updated my operating system so the python version changed which means the project that used to work stopped working! What is the solution for installing multiple python versions side by side? Hint: it's not an official project by the Python organization but something you can find on github.

3 comments

My recent workflow is to use a great program called mise. You have a config file in your directory and hey presto, python venvs work, they install themselves if they don't already exist, and it will install the exact version of python you specify in your config. On top of that is will set environment variables for you and unload them when you change directory. If you combine this with uv (just tell mise you want uv installed in the config) you can run uv pip sync and instantly reflect any changes in your requirements file directly into your venv very quickly.
For the past 4-5 years this is what has worked exceptionally well for me:

- pyenv for installing multiple versions of python on my machine

- direnv for managing environments (env variables, python version, and virtual environment)

- pip for installing dependencies (pinning versions and only referencing primary packages in requirements.txt - none of their dependencies)

This makes everything extremely easy to work with. When I cd into a project directory direnv loads everything necessary for that environment.

Each project directory has a .env and a .envrc file. The .envrc looks something like this:

    layout python ~/.pyenv/versions/3.11.0/bin/python3
    dotenv .env
Absolutely no headaches working on dozens of local python projects.
> Absolutely no headaches working on dozens of local python projects.

The other day, I moved over to a new container base image that's supposed to run Ansible inside of it. Almost immediately, when trying to manage a RHEL8 compatible host, I got this error: https://github.com/ansible/ansible/issues/82068

I've had issues not only with Python projects that I write, but also with software that's relying on it. Then again, while there are both problems and ways around those, my experience has been similar with pretty much every tech stack out there: from Java apps that refuse to run on anything newer than JDK 8 (good luck updating dozens of Spring dependencies across approx. half a million lines of code in a codebase that's like a decade old), to hopelessly outdated PHP versions or software that works on ancient Yarn versions but not on newer ones and doesn't even build correctly when you move over to Node with npm or software that's stuck on old Gulp versions. Same for Ruby and Rails versions that will run, or .NET and ASP.NET codebases, where the framework code ends up being tightly coupled to the business logic, don't even get me started on front ends that rely on Angular (or AngularJS), Vue (2 to 3 migrations) or React. I've had Debian updates break GRUB, AMD video drivers for the iGPU on the 200GE preventing it from booting, differences between Oracle JDK and OpenJDK having a 10x impact on performance, Nextcloud updates corrupting the install, the same happening with GitLab installs, just a day ago I had a PostgreSQL instance refuse to start up with: PANIC: could not locate a valid checkpoint record. PostgreSQL, of all software.

Sometimes churn feels unavoidable if you don't want code to rot and basically everything is brittle. All software kind of sucks, sometimes certain qualities just suck a bit more than the average. Containers and various version management tools make it suck a bit less, though!

yeah I wrote python-wool and set it as my local alias for python so it does just look for a venv in the called program's path, and use that.

https://github.com/fragmede/python-wool