How to use command line / python tools
Apr. 29th, 2023 03:54 pmI can't find a good rundown of how to get started with python / command-line tools that I like / that is as thorough as I would like, so I'm putting one together here. This is mostly a list of links to other guides / documentation that I happen to like, with some commentary. This is aimed at folks who have never used a python / command-line tool before, or who want to know more about managing python versions or environments (for example for tool development.)
2. https://www.youtube.com/watch?v=jvZLWhkzX-8 is a video that covers basics of the command line.
Why use command line tools?
Command-line tools are cool because they are often free to install and use (without the need for weird ads or sketchy websites) and (if it's your thing) they're also the easiest type of program to create to solve problems in your life (because they don't require a graphical user interface or running a website / server).
Here are a few python / command-line tools I use frequently:
Here are a few python / command-line tools I use frequently:
- youtube-dl (or the fork, yt-dlp): For archiving videos.
- srt, for quickly correcting timing on subtitle files
- cleancredits, for removing text overlays from video clips
- ffmpeg, for handling all kinds of video manipulation and conversion
Intros to the command line
1. https://tutorial.djangogirls.org/en/intro_to_command_line/ This is my favorite guide on how to use a command line. It's part of a larger tutorial about making a website with python but the page stands on its own as a solid intro to the command line.2. https://www.youtube.com/watch?v=jvZLWhkzX-8 is a video that covers basics of the command line.
3. Windows users might find something like WSL2 useful, which lets you have a linux-like command-line experience on windows... some command-line programs may work better or only be available in linux.
Installing Python command-line tools
The easiest way to install Python command-line tools is pipx, a package manager for python applications.
On Mac OS, you will need to install homebrew first, which is a general-purpose package manager that you can use to install pipx.
If all you want is to install and run Python command-line tools you can probably stop here!
Python version management
https://tutorial.djangogirls.org/en/python_installation/ covers information about how to install python in a basic sense. Alternately, you can watch https://www.youtube.com/watch?v=pVTaqzKZCdA
However, sometimes it can be useful to have more than one version of python available - for example, some programs might require different versions of Python from each other. My preferred tool for python version management is pyenv. The README has pretty comprehensive information about how it works and how to deal with any issues that come up, which I appreciate.
You can also manage isolated environments manually. They are generally called "virtual environments".
This guide for using virtual environments is pretty good: https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv My one caution would be that the guide is very pro-Pipenv, which is a different tool for managing isolated / repeatable environments. However, in my experience, it doesn't add much compared to a virtual environment and complicates things significantly. But the advice on how to use virtualenv is solid.
The python docs for the venv module (a built-in version of virtualenv) have a lot of interesting background information if you want to know more: https://docs.python.org/3/library/venv.html
However, sometimes it can be useful to have more than one version of python available - for example, some programs might require different versions of Python from each other. My preferred tool for python version management is pyenv. The README has pretty comprehensive information about how it works and how to deal with any issues that come up, which I appreciate.
Isolated Python environments
Isolated Python environments let you install your dependencies and know those are the only dependencies installed. This means that:- You can know that those are the only dependencies that other users of the program would need.
- You can know that the results will be the same even if you install the same code on a different computer - for example, if you set up automated tests on GitHub.
- You can avoid problems if multiple programs are installed that have conflicting dependency requirements.
You can also manage isolated environments manually. They are generally called "virtual environments".
This guide for using virtual environments is pretty good: https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv My one caution would be that the guide is very pro-Pipenv, which is a different tool for managing isolated / repeatable environments. However, in my experience, it doesn't add much compared to a virtual environment and complicates things significantly. But the advice on how to use virtualenv is solid.
The python docs for the venv module (a built-in version of virtualenv) have a lot of interesting background information if you want to know more: https://docs.python.org/3/library/venv.html