4 ms·
Python's many command-line utilities
- aaronbrethorst 2y agoI 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"
- deleted 2y ago[deleted]
- RodgerTheGreat 2y agoSome 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.
- o11c 2y agoRecent distros have `open` as a symlink to `xdg-open` instead of the old `openvt`.
- ttyprintk 2y agoI believe that’ll be deprecated by `gio open` soon.
- o11c 2y agoAIUI 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.
- PinkSheep 2y agoThis 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)
- loloquwowndueo 2y agoThat’s not what httpx does.
- yismail 2y agoalso, open . opens your current directory in Finder
- adamsilkey 2y agoOn Windows, you can do this from the run dialogue! [WIN]+R -> https://google.com https://google.com
- analog31 2y agoI 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.
- fforflo 2y agoif you want complex subcommands and a truly fluent CLI interface, go with a click. https://click.palletsprojects.com/ https://click.palletsprojects.com/ else: argparse is more than enough https://docs.python.org/3/library/argparse.html https://docs.python.org/3/library/argparse.html
- amlozano 2y agoOr go a step beyond and get typer (which builds on click IIRC) https://typer.tiangolo.com/ https://typer.tiangolo.com/ If you use Poetry and a pyproject.toml, you can even make your package installable with something like pipx straight from Github. Its a trick I use often for little command line utilities. https://python-poetry.org/docs/pyproject/#scripts https://python-poetry.org/docs/pyproject/#scripts
- kstrauser 2y agoI'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.
- smitty1e 2y agoI 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.
- th 2y agoThank you so much for your kind words! I'm really glad you found Python Morsels useful.
- smitty1e 2y agoRES IPSA LOQUITUR
- john-tells-all 2y agoTrey 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.
- fforflo 2y agoMy favorite and probably most useful? `python3 -m venv ./venv` Honestly, forget about conda, poetry, virtualenvwrapper etc and just use that one.
- scrlk 2y agoI like to add `--upgrade-deps` as well. This automatically updates pip and setuptools within the venv after creation.
- noitpmeder 2y agoDidn't know about this .. thanks!
- rubee64 2y agoCompletely agree
- selcuka 2y agoExactly. 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.
- rbanffy 2y agoEven more if you use the legendary "pip-chill" to trim down the requirements file ;-) Sorry. Shameless plug.
- eternityforest 2y agoPoetry tracks your direct vs transient dependencies for you, and poetry run is stateless and easier to work with than a stateful activated venv
- kungp 2y agoOr `uv venv`, `uv pip install` for a free, pretty extreme speedup. https://github.com/astral-sh/uv/ https://github.com/astral-sh/uv/
- PyWoody 2y agoAnother 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 https://docs.python.org/3/using/cmdline.html
- rnewme 2y agoFound 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("")])'
- anitil 2y agoYou can also call '__import__' as a function to save yourself a few characters worth of rsi >>> python3 -c "print(__import__('httpx').__path__)"
- kickingvegas 2y agoIf you need to mess with semantic versioning, `python -m semver`.
- selcuka 2y agoIt's a third party package, though.
- mrrajjogdi 2y ago[flagged]
- deleted 2y ago[deleted]