
Why to use ‘python -m pip’ - pyprism
https://snarky.ca/why-you-should-use-python-m-pip/
======
jedberg
To prevent accidentally installing in the global interpreter I have this in my
.bash_profile:

    
    
        # pip should only run if there is a 
        # virtualenv currently activated
        export PIP_REQUIRE_VIRTUALENV=true

~~~
tus88
Do you type "sudo" by accident a lot?

~~~
coldtea
If you do frequent admin work on a computer it's trivial to "type sudo by
accident a lot", so I don't understand the spirit of the question...

------
tofof
>Now you may be saying, "I always install the latest versions, so that would
mean Python 3.8.0 was installed last since it's newer than 3.7.5". OK, but
what happens when Python 3.7.6 comes out? Your pip command would have gone
from using Python 3.8 to Python 3.7.

This seems less like a reason to use `python -m pip` and more a reason to
endorse languages with saner versioning and less need to sandbox twenty
different versions from one another.

~~~
heavenlyblue
Nah - what you simply need to do is to access the pip binary in the directory
of the virtualenv (using PATH) you’re working with and simply stop using the
system interpreter.

That being said I would like to inquire which languages _you_ could give as an
example that work perfectly well with the system-provided compilers when you
are using the latest features.

~~~
enriquto
virtualenv is a nasty kludge and needs to die

~~~
bbernoulli
Do the problems it purports to solve just not interest you or did you have an
alternate suggestion?

~~~
enriquto
It creates more and worse problems than it solves. People find it alright to
write code that only works in a specific combination of language and package
versions, and that is a shameful state of affairs.

~~~
orf
That has nothing to do with virtualenvs though.

------
beagle3
Just use [mini]conda. Everytime someone posts about a python package issue or
virtual env issue, it's something that conda already had solved before 2014.
And yes, pip is fully supported within a conda environment.

I really can't figure out why conda is not ore popular.

~~~
robertk
Have you tried using it with larger projects? You can easily lose a day
waiting for dependency resolution to finish — it takes _forever_.

~~~
lsorber
It's not fast on large environment.ymls, but recreating your environment isn't
something you do daily so it's not that big of a deal for us.

~~~
orf
Any project with CI should recreate the environment a lot.

The lack of a Conda lockfile makes it impractical to use outside of toy
research proof of concepts.

~~~
lsorber
> Any project with CI should recreate the environment a lot.

That's a very inefficient way to run your CI, with conda and pip alike.

Instead, you could build your environment once in a Docker image and use that
as your build image.

Saves a lot of time on your builds, guarantees reproducibility, and will work
even when package servers are unavailable.

~~~
orf
We do, but docker caching is not terribly easy to implement in a project with
lots of concurrent builds. At least not for me.

We build images based on their commit hash, caching off the last commit hash.
That works but has issues with merges and the first commit to a branch.

We also do it based on the branch name, but Docker has issues around
specifying multiple cache from arguments in the CLI. That causes unnecessary
invalidations on branches.

That all leads to more rebuilds than I would like.

------
j88439h84
Pip is a powerful but low-level tool, which probably shouldn't be user-facing.
It doesn't have a full resolver, locking mechanism, or environment management.
Likewise setuptools.

For installing global command-line scripts, I like pipx instead of pip.
[https://github.com/pipxproject/pipx](https://github.com/pipxproject/pipx)

For development, I like Poetry instead of pip.
[https://poetry.eustace.io](https://poetry.eustace.io)

~~~
keb_
Haven't used Python in a long time, but last I remember, pipenv was the tool
of choice. Frankly, I'm overwhelmed by all these choices. Does Poetry, pipsi,
pipx, pipenv, and pip + venv all accomplish similar goals? Is it just a matter
of preference?

~~~
j88439h84
Pipenv is a replacement for venv + pip + pip-compile.

Poetry is a replacement for pipenv + setuptools.

Pipsi is unmaintained and replaced by pipx.

~~~
orf
Poetry doesn’t manage virtual environments for you, does it?

~~~
j88439h84
It does.

------
mlthoughts2018
> “Let's say I have two versions of Python installed, like Python 3.7 and 3.8
> (and this is very common for people thanks to Python coming installed on
> macOS and Linux, let alone you may have installed Python 3.8 to play with it
> while having previously installed Python 3.7). Now, if you were to type pip
> in your terminal, which Python interpreter would it install for?”

Yeah but you just kick the can to which python happens to be the system python
or activated python referenced by “python”. It’s exactly the same problem.

If I as a user have to be careful of which underlying Python installation is
being referenced, then I want to be responsible for activating the conda
environment I need and using “regular” commands like plain “pip” after that.
The “python -m” idiom is not important for this use case, as it doesn’t
actually solve the problem (ensuring the right referenced Python).

~~~
bscphil
> Yeah but you just kick the can to which python happens to be the system
> python or activated python referenced by “python”. It’s exactly the same
> problem.

Exactly right. And on any sane Linux system the pip command should get you a
version compatible with the system Python, which means this is a pointless
extra step. Unless these instructions are supposed to be for Windows, or
MacOS, in which case it should probably say so. (And doesn't everyone on
Windows use Conda anyway?)

------
zem
if you use a per project venv I see no reason not to use `pip install`. if you
don't use a per project venv, well, do that!

~~~
hannibalhorn
Yeah, venv's are a must. And to the article's "forgot to activate the venv"
complaint, I highly recommend direnv.

On top of activating the venv when (and only when) you're inside your project
directory tree, you can use dotenv for loading all relevant env vars (AWS
creds, API keys, etc.,) and add your project's root dir to PYTHONPATH with
path_add.

~~~
rmwaite
The PIP_REQUIRE_VIRTUALENV environment variable also helps with this.

~~~
yrro
Thank you! Now if only this was documented!

Come to think of it, only there was a PIP_DEFAULT_VIRTUALENV_PROJECTDIR
variable which would causes pip to look upwards until it found a setup.py
file, and then use a .venv directory in that directory by default...

------
diminoten
Okay, or just use pipenv.

[https://github.com/pypa/pipenv](https://github.com/pypa/pipenv)

~~~
brobinson
Not sure why this is downvoted. Only the uninformed are using (usually
multiple, lol) requirements.txt files + raw `pip` and managing venvs
themselves nowadays.

~~~
diminoten
There's a huge rift between hobbyist Python users and professional developers.
That's okay, but the former group doesn't always like it when the latter group
chimes in...

------
kalenx
Maybe it's just on my installation, but pip comes with versioned executables.
That is, I can call "pip3.7" or "pip3.4". What's the benefits of using the -m
approach in this case?

~~~
bluedino
You should be using a virtualenv

------
Jsharm
Pip is so bad vs rivals, conda is so much better I'm amazed it's not the
recommended package manager.

~~~
thecleaner
What about installing from source ? conda does not support it at all. For some
esoteric python packages which use C++ or Cython backends, it is sometimes
impossible to install those packages and installing from source becomes the
only viable option. pip is quite awesome for this.

~~~
tempay
This is where conda excels because it uses its own toolchain and therefore
doesn’t need to make any assumptions about the system. Someone needs to have
packaged it, ideally using conda-forge, but it should then just work.

Pip installing all from source is tricky to get working correctly as soon as
you require a newer compiler or dependencies. Providing wheels works well but
is a pain to set up correctly with good platform coverage.

That said, it doesn’t need to be one or the other. Conda integrates well with
pip so you can install a base layer with conda then pip install your more
esoteric dependencies.

~~~
Mathnerd314
It doesn't integrate well. Conda HQ has a bunch of warnings:
[https://www.anaconda.com/using-pip-in-a-conda-
environment/](https://www.anaconda.com/using-pip-in-a-conda-environment/)

------
0x8BADF00D
I usually use pip3 install --user. Then just add ~/.local/bin to PATH. It
hasn't caused any issues for me thus far. I also use venv, but only in CI to
make sure that other people's environments don't get messed up. I personally
prefer using pip3 install --user.

~~~
mehrdadn
I'm guessing you just never use Python 2? Otherwise it's hard to see how you
don't run into issues.

~~~
mixmastamyk
It’s EOL in under two months.

~~~
mehrdadn
It's also

    
    
      Required By: apitrace apm asciidoc bluefish bzr cloudprint-cups dblatex epydoc fio gconf gemrb getmail gmock gnome-doc-utils gtk-recordmydesktop inkscape ipcheck ipython2 java-openjfx java11-openjfx java8-openjfx jcl jmc kig kodi-bin kodi-gbm kodi-wayland lhapdf lib32-apitrace libgda libkate libvirt-python2 libvolk lilypond mailman mcomix mediaproxy mercurial mftrace mod_wsgi2 moosefs mopidy mysql-python mysql-workbench nss-pam-ldapd ntop pluma pychecker pydb pylibacl pynac pypanel pyrex pyrit pysol-sound-server rdiff-backup repo rtaudio scribus sgmltools-lite shedskin singularity spambayes tellico texmacs tuxpaint txt2tags vim-ultisnips wesnoth wicd wifite xpra
      Optional For: android-tools arduino armagetronad boost boost1.69 brial bugzilla cairo-dock-plug-ins cantor cryptominisat5 csound dia ecasound ecryptfs-utils efl efl-docs faust freedroidrpg freeradius gammu geda-gaf gif2png git gogglesmm graphviz gvim hivex inn john kcachegrind-common kodi-eventclients kross-interpreters libdnet libevent libglade libieee1284 libnewt libproxy libuhd magma marsyas mate-menus mathomatic mediawiki munin-node ncmpc net-snmp non-timeline notmuch openconnect opensips plan9port postgresql pycharm-community-edition python-pywal recoll roundcubemail rrdtool singular skktools spectmorph spring subversion texlive-core texlive-music vim vim-latexsuite xf86-video-qxl
    

Regardless, let's not turn this into yet another argument about why anyone
should/shouldn't upgrade to Python 2. I was just trying to confirm that the
parent is not doing anything that requires Python 2.

~~~
takeda
All packages that matter were already ported, some new packages work only with
version 3, many popular packages already dropped support for python 2, a lot
will drop in December.

Most (all? actually I don't know any from your list that do) packages that you
listed are no longer maintained

~~~
mehrdadn
It seems you missed the second half of my comment.

------
femto113

        alias pip python -m pip
    

then stop worrying about it

~~~
luxuryballs
just don’t include that in a machine where the user doesn’t know it’s there
until it’s the last thing they check to explain wtf is going on ;)

------
at_a_remove
As someone who is stuck on a very old version of python (2.7) on win32 due to
Reasons (vendors, basically), how I wish I could even try to install pip;
unfortunately, it will only recommend upgrading to a newer version of python
when I attempt. All roads seem to lead to this place, a one-size-fits-all
dismissive "lol upgrade."

Packaging is one of my least favorite things about the language I love the
most.

~~~
neurostimulant
Can you at least upgrade to latest version of python 2.7? If you're on at
least python 2.7.9, pip should be already installed because it's included by
default.

------
pbreit
This kind of stuff is my least favorite attribute of Python. Surely there must
be a better way?

~~~
WilliamEdward
Once you figure out how venv works it's really simple to set up a python
project. It's just another quirk with just another programming language, they
all have them. People don't complain about how hard it is to learn to program,
but learning how to use some pip commands is too difficult for them?

~~~
m45t3r
Sure it is a quirk, however it could really be better.

Anyway, I think the fact that people has more patience with learning a
language than tools is that learning a language is fun, while tooling is just
boring.

------
takeda
"that's why you should use virtualenv"

------
IshKebab
TL;DR because Python is a mess, and installations are often messed up.

~~~
m45t3r
Python itself is not a mess, the packaging part of Python is.

However, Python is also one of the oldest programming languages still in use
and one of the first to have the concept of installable dependencies
(easy_install).

Now it is kinda difficult to fix without breaking something, however I think
we eventually will have a solution, like __pypackage__
[https://www.python.org/dev/peps/pep-0582/](https://www.python.org/dev/peps/pep-0582/).

