pythondashboard

Using python m switch

Core Python developer, Brett Cannon, wrote a compelling case for python -m pip. We can do better. Let's use python -m everywhere. What about building a tooling ecosystem a la golang around python -m?

What does it do?

The short explanation is that it runs the specified module. But of course, one needs to make sure the module can be run at all. The long explanation is that python -m does other things like run packages via a __main__.py file inside that package.

Okay, cool, what is this about then?

Well, this is about shell scripts. You know, when one turns regular Python modules into an executable by using a shebang on the first line? Many packages use setuptools for that. It is usually a shebang followed by the path to the desired Python interpreter. That's where we are focusing.

The problem is that shebang lines have never allowed spaces inside them. This, alone, makes all Python executable scripts, installed via setuptools, on one of my machines worthless. Why? Because OneDrive wants spaces in its folder's. And on that machine, I can't do anything about that!

So python -m fixes the issue?

It sure does! And it fixes the issue so perfectly that I think we should do away with creating executable scripts via setuptools. Whenever a Python executable fails to run, I always check to see if it's a regular module turned into an executable. I look inside the setup.py for that package. It usually tells me what module they set as entry point. Finally doing python -m package.module to run the module always works.

So what is all that talk about tooling ecosystem?

Golang have nice tools like go run, go get, go build, etc. I realized that python -m is our go run! So why not build our tooling around it? There are already several packages in the standard library that does this. But we have not widely adopted that way of running modules.

See, for example, this article for some of them.

January 30th, 2020.