What’s the correct process to install and run a .py application and its dependencies? Where should I save the .py file, where should I run it from, and can it interfere with the rest of my system?

Often there is an application/script I’d like to use and it is provided as a .py file download, along with a list of other applications/scripts that need to be installed separately for it to work. Often not all of these dependencies are available in my distro’s repository. There seems to be an assumption of prior knowledge as to how to get set up to run .py files, and it is therefore not documented on developers pages. Can anyone fill me in?

I’d like to install this application. Perhaps it could be used as an example to help explain the process.

My distro is Debian 13, in case that’s relevant.

Thanks!

  • doodoo_wizard@lemmy.ml
    link
    fedilink
    arrow-up
    5
    ·
    7 hours ago

    Hey when pip doesn’t work:

    You’re using Debian, so if you don’t want to set up virtual environments the best bet is to install your dependencies using apt like a normal person. All the python stuff in apt will show up under the prefix python3-. So you’ll need python3-numpy, python3-tiffile etc.

    Apt supports tab completion so something like “apt install python3-num” then the tab key would show a list of possible completions (and jump forward any letters that are common between completions).

    If you want to use venvs there’s a bunch of posts explaining how to do that.

    When you want to “install” the .py doohicky you just downloaded, put it in your path! $PATH will tell you what locations get scanned for executable files when you type something and you can add a local directory like ~/.bin to it, then put your .py file in there. If you go with venvs, put the .py file in the right place to run inside the venv, then make a one liner script that runs it from the venv with the file name set to what you wanna type to run the .py, put it in your local path directory and you’re off to the races!

    I also use Debian and am coerced into using python software so reply with any questions and I’ll set you straight.

    • Da Oeuf@slrpnk.netOP
      link
      fedilink
      arrow-up
      1
      ·
      2 hours ago

      Thanks for the help! For some reason tab autocompletion doesn’t work for me but that’s an issue for another day…

      put the .py file in the right place to run inside the venv

      This could be where I’ve been getting lost. How do I figure out what the right place is?

      • doodoo_wizard@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        1 hour ago

        One man’s right place is another man’s evidence of clinical insanity. You could just leave them on your desktop and invoke them through the venv. You could make a folder called Folder For Python Scripts That Don’t Run Good and put all your different python things there.

        You can also put the target python script inside the folder its respective venv lives in.

        Really the world is your oyster because ultimately you’re gonna make another script that does something like “read $a; ~./<venv_location>/python3 <target_.py_file> $a;” and naming it what you wanna type to run your .py and put it in your local $PATH directory.

        Don’t trust that one liner btw it’s definitley wrong.

  • Jerkface (any/all)@lemmy.ca
    link
    fedilink
    English
    arrow-up
    19
    ·
    edit-2
    10 hours ago

    This is probably a problem with how the question is being asked, but…

    A .py file is not an application. It might be a component of an application but there is no general way to “install” a .py file. If you are coming from microsoft, you can think of a .py file as similar to a .bat file, but it might also be more like a .dll file.

    If the .py file contains a script meant to be run like a .bat file, you can to run it from wherever you saved it using the Python interpreter. That is what is occurring in this example from your page:

    python3 rectarg.py R230122W.cht R230122W.txt output.tif --target_dpi 300 --background GS10 --label_axis_visible X=B

    The user is using the python3 command to run the rectarg.py script from the current directory, and passing it arguments with the rest of the commandline. This doesn’t require installing rectarg.py, just knowing the path (or in this case, being in that path).

    • Strit@lemmy.linuxuserspace.show
      link
      fedilink
      arrow-up
      6
      ·
      9 hours ago

      You also need to make sure all the dependencies are installed. Those are usually listed in a requirements.txt file and can be installed with pip.

  • ejs@piefed.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    5 hours ago

    I’d recommend installing those python dependencies using apt, so that when you update your system packages, the python libraries get updated, too. Pip, on the other hand, is useful for development but is detatched from apt and you will definitely forget to pip update unlike apt update which you hopefully do frequently. Use the names of the packages the readme provides in the pip install … instruction. For example, for numpy, you can install this.

    Then, since that python script has a shebang at the top, you can add it to a directory in your $PATH and mark it executable with chmod, and you can invoke the script in your shell from any directory with just the file name.

      • corsicanguppy@lemmy.ca
        link
        fedilink
        English
        arrow-up
        1
        ·
        2 hours ago

        As popularity doesn’t equal validity, repo size isn’t a measure of safety.

        Ask a system person about the massive validation hole in pips (and dockers and CPAN and composer bits and venvs and crates and gems) and why enterprise people and the safety-aware avoid this like it’s toxic.

  • residentoflaniakea@discuss.tchncs.de
    link
    fedilink
    English
    arrow-up
    11
    ·
    edit-2
    11 hours ago

    You can run it in an virtual environment:

    python -m venv someproject

    source someproject/bin/activate

    Then you can run the commands in the git README you posted.

    pip …

    Will will install the include files needed within this virtual environment. Alternatively you can do

    apt install …

    Instead of using pip. Your python script should run within the virtual environment.

      • corsicanguppy@lemmy.ca
        link
        fedilink
        English
        arrow-up
        1
        ·
        2 hours ago

        Apt: safest for Debian

        Pip/pipx: kids dig it. See “supply chain attack”

        You do what you want to support and maintain.

      • residentoflaniakea@discuss.tchncs.de
        link
        fedilink
        English
        arrow-up
        2
        ·
        8 hours ago

        That might work but it is recommended to not install packages outside of virtual environments as it might lead to conflicts between versions across different projects.

        • IrritableOcelot@beehaw.org
          link
          fedilink
          arrow-up
          1
          ·
          5 hours ago

          Just FYI, pipx does use a virtual environment behind the scenes. The idea with pipx or uvx is to install a python script as a standalone script.

  • fizzle@quokk.au
    link
    fedilink
    English
    arrow-up
    8
    ·
    10 hours ago

    I dislike installing python dependencies globally.

    It’s like keeping your vacuum cleaner, lawnmower, and washing machine in your living room.

    Someone mentioned virtualenv which I expect is the best way to encapsulate a set of python dependencies.

    That said, nix package manager on debian (not nixos) will provide the same encapsulation in a language agnostic way. As in, you can use it for python or node or rust or binaries or cli tools or GUI apps or anything really.

    Learning about nix just to run this one script is an over engineered solution, but I feel like its worth a mention because its definitely the best way to run anything you want in a way that won’t clutter up your living room.

    The details for creating a shell with python dependencies are here:

    https://wiki.nixos.org/wiki/Python

  • tourist@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    11 hours ago

    Other comments did a good job

    Just making sure, how familiar are you with using the terminal?

    The GitHub repository you linked is terminal-only

    • Da Oeuf@slrpnk.netOP
      link
      fedilink
      arrow-up
      1
      ·
      1 hour ago

      Good question. I’m quite familiar with the terminal, but I still get confused by things in it that I’m not familiar with, if that makes sense!

  • just_another_person@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    10 hours ago

    You don’t need to “Install” them like in Windows. Everything in Linux is just a call to launch something, and for Python is just python someprogram.py.

    If you’re regularly going to be calling this program, you can put it anywhere makes sense for you, but generally somewhere in your PATH, and then you can make an alias for your preferred terminal to launch it easily.

    If that’s super confusing, give this a try (cross platform): https://github.com/InkAurora/PythonAliasManager

  • emb@lemmy.world
    link
    fedilink
    arrow-up
    3
    arrow-down
    1
    ·
    edit-2
    10 hours ago

    If you actually just download a .py file, you need to run it with python. Like in the example you linked, something like

    python3 _filename_
    

    (and if that’s the case, you might need to add a line at the top of the file, telling it where the interpreter is. But probably you actually want the following)

    To install, you use the python installer program, pip. Something like

    pip _package-name_
    

    Before you do either, you need to install python and pip. I don’t know exactly (look for correct package names), but you need to run something like

    sudo apt install python3 pip
    
    • infeeeee@lemmy.zip
      link
      fedilink
      arrow-up
      7
      ·
      10 hours ago

      No, pip is for libraries, and for running scripts in virtual environments (venv). Recent versions of pip don’t work outside venvs. For installing packages systemwide use something like pipx, which creates venvs automagically and runs the script there.