My secret to high uptime:
while True:
try:
main()
except:
pass
There you go:
# Start an infinite loop because True will always be True
while True:
# try to run the main function, usually where everything happens
try:
main()
# in the case an exception is raised in the main function simply discard (pass) and restart the loop
except:
pass
This gives some better context. https://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice
But essentially ignoring every single error a program could generate is not great. It’d be better to know what those errors are and fix/prevent them from occurring in the first place.
If using pyenv to support multiple python versions, when creating venvs, make sure to pass --copies
to it.
% python3 -m venv venv --copies
Ordinarily, venv uses symbolic links back to the current version of the python binary. A lot of tools and IDEs don’t traverse symbolic links. That flag actually copies the real binaries over to the venv.
This avoids a metric ton of hard-to-diagnose misery later on.
Can I request a hack? How do I handle several different versions of Python installed, which one is used for pip stuff, and how sudo/running as services changes all of this.
You can use pyenv. it will handle everything. https://github.com/pyenv/pyenv
I started using hatch lately and really like how I can manage everything from the pyproject.toml file
There are like 10,000 different solutions, but I would just recommend using what’s built in to python
If you have multiple versions installed you should be able to call python3.12
to use 3.12, etc
Best practice is to use a different virtual environment for every project, which is basically a copy of an existing installed python version with its own packages folder. Calling pip with the system python installs it for the entire OS. Calling it with sudo puts the packages in a separate package directory reserved for the operating system and can create conflicts and break stuff (as far as I remember, this could have changed in recent versions)
Make a virtual environment with python3.13 -m venv venv
the 2nd one is the directory name. Instead of calling the system python, call the executable at venv/bin/python3
If you do source venv/bin/activate
it will temporarily replace all your bash commands to point to the executables in your venv instead of the system python install (for pip, etc). deactivate
to revert. IDEs should detect the virtual environment in your project folder and automatically activate it
Don’t need to activate your venv to use it.
Just use venv/bin/python my-file.py
from a script or a terminal from your project root.
Walrus operator - := - envious of the C devs being able to simultaneously assign and return a value? Envy no more, we’ve got it.
For those curious about the drama & lack of wide adoption surrounding the walrus operator
https://dev.to/renegadecoder94/the-controversy-behind-the-walrus-operator-in-python-4k4e
It’s a shame because it’s a really nice feature.
You need to see it to believe it I think. I was generally on the side of “too complex” but then came across instances perfect for it and used it right away and found it pleasant.
I’m still generally on the side of “too complex” though, and think there are probably better things for PSF to work on (cough packaging cough).