Using Python for Scripting
51–60 of 112 posts
Re: Using Python for Scripting
#52Odd, I don't see any mention of subprocess.run, the workhorse of python scripting. Quick rundown for the unfamiliar: Give it a command as a list of strings (e.g., subprocess.run(["echo", "foo"]).) It takes a bunch of flags, but the most useful (but not immediately obvious) ones are: check=True: Raise an error if the command fails capture_output=True: Captures stdout/stderr on the CompletedProcess text=True: Automatic…
Basically you can just `from sh import [command]` and then have an installed binary command available as function
from sh import ifconfig
print(ifconfig("eth0"))Re: Using Python for Scripting
#53Dupe: https://news.ycombinator.com/item?id=46176113
Re: Using Python for Scripting
#54Earlier quoted context omitted.
What does Bash use for its packaging? Use that.
Ok tbh bash uses the system package manager to install command-line utilities, and using the system package manager for python packages HAS been tried, and the parallel use of system packaging and independent package managers is part of why python package distribution is such a mess.
Re: Using Python for Scripting
#55Re: Using Python for Scripting
#56Earlier quoted context omitted.
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.
I’m so thankful to see a flake.nix file in every single cool project on code forges.
Re: Using Python for Scripting
#57Odd, I don't see any mention of subprocess.run, the workhorse of python scripting. Quick rundown for the unfamiliar: Give it a command as a list of strings (e.g., subprocess.run(["echo", "foo"]).) It takes a bunch of flags, but the most useful (but not immediately obvious) ones are: check=True: Raise an error if the command fails capture_output=True: Captures stdout/stderr on the CompletedProcess text=True: Automatic…
Re: Using Python for Scripting
#58Earlier quoted context omitted.
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.
It’s like Vim, you learn it once, and you keep using it forever once you’re used to it. I’m so thankful to see a flake.nix file in every single cool project on code forges.
Seeing that flake.nix badge of complexity lets me know a project will be a nightmare to set up and will break every other week. It's usually right next to the Cargo.toml badge with 400 dependencies underneath.
Re: Using Python for Scripting
#59Odd, I don't see any mention of subprocess.run, the workhorse of python scripting. Quick rundown for the unfamiliar: Give it a command as a list of strings (e.g., subprocess.run(["echo", "foo"]).) It takes a bunch of flags, but the most useful (but not immediately obvious) ones are: check=True: Raise an error if the command fails capture_output=True: Captures stdout/stderr on the CompletedProcess text=True: Automatic…
One thing I can recommend that makes scripting in python with external commands a lot easier is the `sh` module: https://pypi.org/project/sh/ Basically you can just `from sh import [command]` and then have an installed binary command available as function from sh import ifconfig print(ifconfig("eth0"))
https://docs.astral.sh/uv/guides/scripts/#using-different-py...
Re: Using Python for Scripting
#60I like using Go for “scripting”. To each their own I suppose.