• beeng@discuss.tchncs.de
    link
    fedilink
    arrow-up
    42
    ·
    1 month ago

    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.

  • Gutek8134@lemmy.world
    link
    fedilink
    arrow-up
    21
    ·
    1 month ago

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

  • fubarx@lemmy.ml
    link
    fedilink
    arrow-up
    14
    arrow-down
    1
    ·
    1 month ago

    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.

  • drislands@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    1 month ago

    If you’re on Linux (or Mac), add an alias to your .bashrc:

    alias activate="source env/bin/activate"
    

    Now you can activate your venv by just running activate in the project root!

  • grue@lemmy.world
    link
    fedilink
    English
    arrow-up
    8
    ·
    1 month ago

    Use fewer loops and more comprehensions.

    Also, for the love of $DEITY, embrace EAFP!

    • QuizzaciousOtter@lemm.ee
      link
      fedilink
      arrow-up
      15
      ·
      1 month ago

      EAFP - “Easier to ask for forgiveness than for permission”.

      For those who are (like me) unfamiliar with this… acronym?

  • computergeek125@lemmy.world
    link
    fedilink
    English
    arrow-up
    7
    ·
    1 month ago

    I forget where I originally found this and Google on my phone was unhelpful.

    My favorite annoying trick is x -=- 1. It looks like it shouldn’t work because that’s not precisely a valid operator, but it functions as an increment equivalent to x += 1

    It works because -= still functions are “subtract and assign”, but the second minus applies to the 1 making it -1.

  • dneaves@lemmy.world
    link
    fedilink
    English
    arrow-up
    6
    ·
    edit-2
    1 month ago

    You can feign immutablility on class attributes by playing with __setattr__. I don’t remember the exact way to do it, but its roughly like this:

    class YourClass:
        def __setattr__(self, name, value):
            if not hasattr(self, name):
                super().__setattr__(name, value)
           else:
                # handle as you wish if the
                # attr/value already exists.
                # pass, raise, whatever
    

    I say “feign immutability” because there are still cases in which an attr value can change, such as:

    • the underlying attribute contains a list and appending to the list
    • the underlying attribute is a class and modifying the class’s attributes
    • pretty much anything that does “in place” changes, because so much of python is referential and has side effects.
  • superweeniehutjrs@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    1 month ago

    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.

    • bjorney@lemmy.ca
      link
      fedilink
      arrow-up
      9
      ·
      edit-2
      1 month ago

      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

  • litchralee@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 month ago

    If using asyncio is too impenetrable, try using Trio instead. It’s a sensibly-designed asynchronous library, to the point that you’ll find it’s easier to write non-trivial Python programs in Trio from the start, rather than bolting-on async support later.

    Asyncio is just plain weird, IMO, exposing more low-level concerns than is customary for Python. Whereas Trio lets you get things done intuitively.