47 comments

[ 3.4 ms ] story [ 76.4 ms ] thread
I recognize this is the most trivial example cited, but on a Mac, you can also use the `open` command to open an URL:

    open "http://example.com"
(comment deleted)
Some Linux distros offer a similar (though far less memorably named) "xdg-open".

It doesn't do any of the other really handy things the MacOS "open" command can do, like "open -f" slurping stdin into a fresh instance of your default text editor.

Recent distros have `open` as a symlink to `xdg-open` instead of the old `openvt`.
I believe that’ll be deprecated by `gio open` soon.
AIUI xdg-open is the standard tool which then forwards to the (equivalent of the?) desktop-specific tools. For KDE it's `kioclient exec`.

I wouldn't be surprised the GNOME people try to break this standard tool too.

This is correct. Unfortunately, xdg-tools is a set of almost forgotten scripts that need some more eyes and collaboration to polish. I've seen many problems there. Nobody is actively maintaining it, but a couple developers could get the ball rolling. (I'm interested)
That’s not what httpx does.
(comment deleted)
httpx was not referenced in the original article.
Was this maybe in reference to my top-level comment? If so, I just used httpx as an example of a module most people would have installed.
also, open . opens your current directory in Finder
I could probably figure it out, or find it documented, but it would be nice to have concise instructions on creating a Python command-line utility.
if you want complex subcommands and a truly fluent CLI interface, go with a click. https://click.palletsprojects.com/

else: argparse is more than enough https://docs.python.org/3/library/argparse.html

I've not personally stumbled on a UI where Click was used to make something nicer than argparse could. I'm not saying those don't exist, just that I haven't seen one.

Every time I've seen someone use Click at work, it was to implement some simple args that argparse could handle identically. "Why are we using Click here?" "Because it's nice!" "In what way?" "It's nice!" "But we're not actually using the nice parts." "Click is nice!"

Click is nice. IMO it's used way too often in cases where argparse would do just as well.

As long as your python program checks for __name__ like this:

  if __name__ == "__main__":
      # Your code here
Then it should mostly "just work". Use the argparse module to parse command line arguments if necessary. Create a setup.py and use pip to install it, if you want.
There is also a (unofficial) Google project called fire [1]:

> Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.

[1] https://github.com/google/python-fire

I subscribed to pythonmorsels, and I must say that Trey Hunner gives great product. Simple, one-page exercises that nevertheless make one think, and a polished user experience. Worth a look for anyone who'd come here.
Thank you so much for your kind words! I'm really glad you found Python Morsels useful.
Trey Hunner has created a ton of useful and interesting Python articles. Although I've been programming in it for 20+ years, I always learn new things. He's a national treasure.

Pythonmorsels is a great subscribe.

My favorite and probably most useful? `python3 -m venv ./venv` Honestly, forget about conda, poetry, virtualenvwrapper etc and just use that one.
I like to add `--upgrade-deps` as well. This automatically updates pip and setuptools within the venv after creation.
Exactly. People complain about Python's package management story, but for 99% of use cases a workflow that consists of Python's built-in venv, pip, requirements.txt, and constraints.txt is completely fine.
Even more if you use the legendary "pip-chill" to trim down the requirements file ;-)

Sorry. Shameless plug.

Poetry tracks your direct vs transient dependencies for you, and poetry run is stateless and easier to work with than a stateful activated venv
Or `uv venv`, `uv pip install` for a free, pretty extreme speedup. https://github.com/astral-sh/uv/
To save 4 seconds when installing dependencies?
My work saw a 5x speedup (~2min to ~20s) with a cold cache and like 200-500x (~20s to <0.1s) with a hot cache in our CI pipelines when we switched to uv.
Another handy trick is to use "-c" for executing the string as a command[0]. You can separate the commands by newlines or ";" if you're lazy (like me).

This is really handy when you're moving around environments and you want to get the path or location for a module:

>>> python3 -c "import httpx; print(httpx.__path__)"

or

>>> python3 -c "import httpx; print(httpx.__file__)"

[0] https://docs.python.org/3/using/cmdline.html

Found this one recently:

    If you have python and curl you can extract tag and its contents out of html file curl -s http://www.google.com/ | python3 -c 'import sys; s=sys.stdin.read(); e=""; print(s[s.find(e)+len(e):s.find("")])'
You can also call '__import__' as a function to save yourself a few characters worth of rsi

  >>> python3 -c "print(__import__('httpx').__path__)"
If you need to mess with semantic versioning, `python -m semver`.
It's a third party package, though.
(comment deleted)