58 points

My secret to high uptime:

while True:
    try:
        main()
    except:
        pass
permalink
report
reply
9 points

that was hilarious xD

permalink
report
parent
reply
3 points

Lurking beginner here, why is this bad?

permalink
report
parent
reply
24 points

Basically sweeps errors under rug.

No error handling and go again.

permalink
report
parent
reply
13 points

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
permalink
report
parent
reply
2 points

Thank you for that answer! That makes sense.

permalink
report
parent
reply
4 points

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.

permalink
report
parent
reply
1 point
Deleted by creator
permalink
report
parent
reply
4 points

Someone is absolutely going to think this is a real recommendation and do it.

permalink
report
parent
reply
4 points

Flask developer?

permalink
report
parent
reply
13 points

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.

permalink
report
reply
2 points

Yeah, I wish I knew this about a year ago. Thanks.

permalink
report
parent
reply
5 points

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.

permalink
report
reply
12 points

You can use pyenv. it will handle everything. https://github.com/pyenv/pyenv

permalink
report
parent
reply
2 points

I started using hatch lately and really like how I can manage everything from the pyproject.toml file

https://github.com/pypa/hatch

permalink
report
parent
reply
2 points
9 points
*

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

permalink
report
parent
reply
44 points

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.

permalink
report
reply
0 points

Fuck yeah thanks man

permalink
report
parent
reply
13 points

You don’t need to use venv at all, break the mold and do it all global

permalink
report
parent
reply
4 points

/s

permalink
report
parent
reply
22 points

Walrus operator - := - envious of the C devs being able to simultaneously assign and return a value? Envy no more, we’ve got it.

permalink
report
reply
7 points

I am the Walrus

permalink
report
parent
reply
15 points

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.

permalink
report
parent
reply
7 points

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).

permalink
report
parent
reply

Programming

!programming@programming.dev

Create post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Community stats

  • 3.9K

    Monthly active users

  • 1.7K

    Posts

  • 29K

    Comments