Live data from Hacker News

Using Python for Scripting

hypirion.com

1–10 of 112 posts

Re: Using Python for Scripting

#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 example:

https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-...

Re: Using Python for Scripting

#4
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…

What can you do with bash that isn't in the stdlib of python?

Generally the only nontrivial scripting I ever do is associated with a larger project, so I often already have a pyproject.toml and a UV environment, and I just add the dependencies to the dev group.

Re: Using Python for Scripting

#5
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…

What can you do with bash that isn't in the stdlib of python? Generally the only nontrivial scripting I ever do is associated with a larger project, so I often already have a pyproject.toml and a UV environment, and I just add the dependencies to the dev group.

Well, that's kind of what I mean. For scripts in a python project, you can freely use whatever packages you need. But for one-off scripts, if you need bs4 or something, you're screwed. Either your script now has external dependencies or it requires special tooling.

It just feels strange that C# of all languages is now a better scripting tool than Python, at least out of the box. I did notice uv has exactly the feature I'm looking for, though it's obviously third-party:

https://docs.astral.sh/uv/guides/scripts/#declaring-script-d...

Is everyone just using uv now instead of pip, perhaps? Or is just another alongside pipenv, conda, poetry, etc.? (Python's not my main these days, so I'm out of the loop.)

Re: Using Python for Scripting

#6
post #5

Earlier quoted context omitted.

What can you do with bash that isn't in the stdlib of python? Generally the only nontrivial scripting I ever do is associated with a larger project, so I often already have a pyproject.toml and a UV environment, and I just add the dependencies to the dev group.

Well, that's kind of what I mean. For scripts in a python project, you can freely use whatever packages you need. But for one-off scripts, if you need bs4 or something, you're screwed. Either your script now has external dependencies or it requires special tooling. It just feels strange that C# of all languages is now a better scripting tool than Python, at least out of the box. I did notice uv has exactly the featur…

UV is taking over really fast, it seems to be much more popular any other option.

I suspect conda still has some market share too but I've never needed it.

Re: Using Python for Scripting

#7
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…

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

Re: Using Python for Scripting

#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

Re: Using Python for Scripting

#9
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…

https://peps.python.org/pep-0723/, e.g.:

  # /// script
  # requires-python = ">=3.11"
  # dependencies = [
  #   "requests

Re: Using Python for Scripting

#10
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…

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

This works really well in my experience, but it does mean you need to have a working internet connection the first time you run the script.

  # /// script
  # dependencies = [
  #     "cowsay",
  # ]
  # ///
  import cowsay
  cowsay.cow("Hello World")
Then:

  uv run cowscript.py
It manages a disposable hidden virtual environment automatically, via a very fast symlink-based caching mechanism.

You can also add a shebang line so you can execute it directly:

  #!/usr/bin/env -S uv run --script
  #
  # /// script
  # dependencies = ["cowsay"]
  # ///
  import cowsay
  cowsay.cow("Hello World")
Then:

  chmod 755 cowscript
  ./cowscript
Post reply on HN