r/programming • u/5skandas • Nov 03 '19
Why you should use `python -m pip`
https://snarky.ca/why-you-should-use-python-m-pip/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,TestSuiteand the test runner utils always work for me, and 'just work'. Only use implicit namespaces? No problem. Ifpytest... ask yourself if you really need it, becausepytesthas it's own sys.path logic wholly based on__init__.pyexistencelaunch your
TestRunneror equivalent.pywith the '-m' flag, remembering that it's module import syntaxprofit
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
4
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
pytestdocs 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
Nov 04 '19
Ok, will have to revisit Okken's book. Still not sure I understand what organizational paradigm you propose to use.
2
2
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
4
u/[deleted] Nov 03 '19
Who says you should?
You may.
-moption means "module". When you run Python with-m Xoption, it will look for module namedX.__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 withpip.So, when you call
python -m pip, Python will executepip.__main__module, which, most likely, contains a call to the same entry point, which you would have been reached if you calledpipdirectly.However you may have different versions of
pipinstalled, they aren't necessarily tied to the Python interpreter. So, while callingpipwhenpython(orsys.executable) resolves to a Python interpreter you didn't intend, or, if, conversely, you are using the wrong version ofpip, which does resolvepythoncorrectly, you may end up with broken environment (either missing packages, or conflicting behavior of differentpipversions).Thus, running
python -m pipensures that you are using the Python that you want, and thepipversion that was installed together with this Python, and, on top of it, that whenpipwill run, it will resolvepythoncorrectly.None of this is necessary or important if you only have one instance of either Python or
pipinstalled.