[HN Gopher] Python's many command-line utilities
___________________________________________________________________
Python's many command-line utilities
Author : todsacerdoti
Score : 16 points
Date : 2024-06-03 21:03 UTC (1 hours ago)
(HTM) web link (www.pythonmorsels.com)
(TXT) w3m dump (www.pythonmorsels.com)
| aaronbrethorst wrote:
| 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"
| analog31 wrote:
| 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.
| fforflo wrote:
| 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
| LeoPanthera wrote:
| 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.
| smitty1e wrote:
| 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.
| fforflo wrote:
| My favorite and probably most useful? `python3 -m venv ./venv`
| Honestly, forget about conda, poetry, virtualenvwrapper etc and
| just use that one.
| PyWoody wrote:
| 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
___________________________________________________________________
(page generated 2024-06-03 23:01 UTC)