The advantages of using python -m

31 views
Skip to first unread message

Edward K. Ream

unread,
Sep 22, 2021, 8:58:41 AM9/22/21
to leo-editor
A few days ago I had one of those "Why didn't I see this before?" moments.

Invoking a python package via `python -m package` is simpler and more general. For example:

python -m black -h

There is no need to specify the path to black!

We only need to ensure that `python` invokes python 3. We can do this with an alias (linux) or .cmd file (windows).  Here is my python.cmd file:

"C:\Users\Edward Ream\Python\Python39\python.exe" %*

Now (in devel) Leo's "blacken-files" command invokes black this way:

g.execute_shell_commands(
  f"&python -m black --skip-string-normalization {path}")

Edward

tbp1...@gmail.com

unread,
Sep 22, 2021, 9:05:50 AM9/22/21
to leo-editor
I use -m all the time, including (mostly) running Leo. and running pip. Another advantage with pip is that you always get the right version of pip, which is really good when you have several versions of Python installed.

jkn

unread,
Sep 23, 2021, 1:45:45 PM9/23/21
to leo-editor
I use 'python3 -m pylint...' rather that just 'pylint'. I was sure there was a better way, but that dependes on getting pip/pip3 etc. sorted, and life is too short...

tbp1...@gmail.com

unread,
Sep 23, 2021, 2:04:24 PM9/23/21
to leo-editor
Yes indeed.  I used to have Python 2.7 installed, and the system path pointed to its directories rather than to the Python 3 ones.  I actually had to write a batch file to jam the 3.x directories first on the path when I ran Python 3.x.  Even then I still used python3 -m pip to make sure I ran the right version.

Now it's a habit.

Reply all
Reply to author
Forward
0 new messages