r/programming • • Nov 03 '19

Why you should use `python -m pip`

https://snarky.ca/why-you-should-use-python-m-pip/
0 Upvotes

13 comments sorted by

4

u/[deleted] Nov 03 '19

Who says you should?

You may. -m option means "module". When you run Python with -m X option, it will look for module named X.__main__ and will execute its code. Traditionally, __main__ module contains code related to command line arguments parsing and the program's entry point. This is also the case with pip.

So, when you call python -m pip, Python will execute pip.__main__ module, which, most likely, contains a call to the same entry point, which you would have been reached if you called pip directly.

However you may have different versions of pip installed, they aren't necessarily tied to the Python interpreter. So, while calling pip when python (or sys.executable) resolves to a Python interpreter you didn't intend, or, if, conversely, you are using the wrong version of pip, which does resolve python correctly, you may end up with broken environment (either missing packages, or conflicting behavior of different pip versions).

Thus, running python -m pip ensures that you are using the Python that you want, and the pip version that was installed together with this Python, and, on top of it, that when pip will run, it will resolve python correctly.


None of this is necessary or important if you only have one instance of either Python or pip installed.

2

u/cyanrave Nov 03 '19

Good advice I guess? I'd rather see people using '-m' to advocate instead for better test execution instead of pip stuff.

If you use subpackaging correctly, with relative imports and the like, this flag really helps. If you're still dumping tests under a 'tests' directory at the root of the project in py3... don't do that.

5

u/edwardsdl Nov 03 '19

😬 I still do that. Can you talk a little more about why that’s a bad idea and what I should be doing instead?

3

u/cyanrave Nov 04 '19 edited Nov 04 '19

Sure, use subpackaging! It's a great feature, and it's an easy concept:

  • root is for the top-level thing, anything adjacent should be for building it, supporting it outside it's code, etc. Tests are inherently code and should be treated as such imo.

  • inside that package, partition like Java does; you might hate that, but it makes sense... choose your verbiage, but I divide 'src' and 'test' as the two sub packages. They are merely namespace packages and don't really do much other than force this separation.

  • do everything else the same; use relative imports; never import test in 'src', but hey, now you can import from 'src' in 'test' because they share the same package root - no sys.path or trickery needed

  • if using unittest, TestSuite and the test runner utils always work for me, and 'just work'. Only use implicit namespaces? No problem. If pytest... ask yourself if you really need it, because pytest has it's own sys.path logic wholly based on __init__.py existence

  • launch your TestRunner or equivalent .py with the '-m' flag, remembering that it's module import syntax

  • profit

  • tell others

  • refactor legacy code that does trickery for the test suite

As to 'why it is bad', you're essentially creating a 'tests' top-level pseudo package that only weakly associates itself with it's source. If you and all the other 'tests' packages got together and had a party, you'd probably leave with someone else's source! It's just muddy and ugly, and outdated to professional development (imo).

1

u/edwardsdl Nov 04 '19

Thanks! I’m gonna play around with this tomorrow morning!

4

u/[deleted] Nov 03 '19

Can you please elaborate on that bit about tests in project root? Not sure I understand what you mean by that. Thx.

3

u/cyanrave Nov 04 '19 edited Nov 04 '19

People consistently plop a folder called 'tests' right next to the package they'd like to test... which is code that describes the expected behavior of the code next door.

I believe it may be the single biggest mistake that people are bringing forward into py3 when they migrate. Subpackaging and implicit name spacing are alive and well in py3 and people still habitually go towards what the the pytest docs says 'because it is pytest'.

Shortly into Okken's modern book on the pytest framework, he clears up this sorry note that exists in the 'official docs', clearly formulating why using subpackaging has organizational advantages.

2

u/[deleted] Nov 04 '19

Ok, will have to revisit Okken's book. Still not sure I understand what organizational paradigm you propose to use.

2

u/cyanrave Nov 04 '19

When I get to work, I'll see if I can rip a quote from him on it.

2

u/kankyo Nov 03 '19

It's a pity this isn't what the pip command does.

3

u/oligonucleotides Nov 03 '19

I prefer conda and include an environment.yml with every repo so that my env also lives in version control. People can clone the repo, conda env create -f environment.yml and it works every time.

3

u/mrexodia Nov 03 '19

Which is explained in the post...