Live data from Hacker News

Using Python for Scripting

hypirion.com

41–50 of 112 posts

Re: Using Python for Scripting

#41

> Python is installed on pretty much every machine > Python 3 is installed on basically every machine out there. > Python will work the same on all the machines you run your script on No, no, and no.

More like, "no, but installation is easy", "yes, if the machine is safe", "depends on what you do"

Re: Using Python for Scripting

#42
post #8

Earlier quoted context omitted.

Nix allows you to do this with any language and required dependency: https://wiki.nixos.org/wiki/Nix-shell_shebang

Whoa! This is a revelation. I already loved Nix and used nix-shell extensively, but this is the missing piece: fully reproducible Python scripts without compromise.

Or install direnv and put your dependencies into a shell.nix, setup the bash hook according the manual, create the .envrc with the content "use nix". Then type "direnv allow".

Then you can do e.g. use other persons python scripts without modifying their shebang.

Re: Using Python for Scripting

#44
post #18

Pretty much anything longer then a throwaway one liner I write in python. Would be cool if python had a pipe operator though. The back ticks in ruby is pretty ergonomic too. Wish python had a simpler way to run commands. Kind of tedious to look up subprocess run arguments and also break things up into arrays.

You can always set shell=True and pass in an entire command line as a string, but… don’t do that. It seems really nice until the first time you get the shell escaping wrong, and then it’s something you tend never to do again.

For example,

  subprocess.run(“rm -rf ~/ some file”, shell=True)
and

  subprocess.run([“rm”, “-rf”,  “~/ some file”])
have significant different behavior.

Re: Using Python for Scripting

#45

Earlier quoted context omitted.

You can specify requirements at the top of file and uv can run the script after automatically installing the dependencies. https://avilpage.com/2025/04/learn-python-uv-in-100-seconds....

As someone who writes a lot of python, I love uv, but isn't on nearly every system like python is, which is one of the arguments for using python here in the first place

Sure but the sub question here was about packages.

If you are installing packages, then starting with installing uv should be fine.

Re: Using Python for Scripting

#46
post #8
post #3

How do you handle packages? I want scripts to a be a single file with a shebang, not a repo with a requirements.txt that I need to run in a venv. To me, this is the biggest blocker to using Python for any non-trivial scripting (which is precisely the kind where I wouldn't want to use bash), but I'd like to know how others deal with it. C# scripts let you reference packages in a comment at the top of the file, for exa…

Nix allows you to do this with any language and required dependency: https://wiki.nixos.org/wiki/Nix-shell_shebang

Now you have to set up Nix first and deal with that nightmare of a learning curve. Just to auto-install some dependencies for a script.

Might as well rewrite all your scripts in Rust too while you're layering on unholy amounts of complexity.

Re: Using Python for Scripting

#47
post #3

How do you handle packages? I want scripts to a be a single file with a shebang, not a repo with a requirements.txt that I need to run in a venv. To me, this is the biggest blocker to using Python for any non-trivial scripting (which is precisely the kind where I wouldn't want to use bash), but I'd like to know how others deal with it. C# scripts let you reference packages in a comment at the top of the file, for exa…

Isn't pipx addressing exactly that?

once the script is non-trivial, 'install' it using pipx, in editable mode when you work on the script and as normal pipx installed cli utility otherwise.

the venv part is then completely under the hood.

Re: Using Python for Scripting

#50

I've never liked shell scripting. Last year, I switched my build system of a Rust project over to Python (Cargo is actually quite limited as a build system). For a newer project, I'm using Rust itself with the XTask pattern. I'm not sure if I prefer the Python or Rust approach yet.

I quite liked SCons back when I wrote C++!
Post reply on HN