7

I have a question about installing/upgrading Python packages using the tool pip.

I can either run

sudo pip install -U PACKAGENAME

or

pip install -U PACKAGENAME --user

What is the difference between those two commands and which one should I prefer...

  1. on my own machine?
  2. in the install script of a python program I publish?
5
  • Have a look at stackoverflow.com/questions/21055859/…
    – Ron
    Jun 26, 2015 at 7:44
  • There are a couple ways to call pip via sudo: either sudo -H pip install... or sudo -H python -m pip install... (note that pip likes to be called using sudo -H). The pip bundled with python is available as of Python 2.7.9 and later (on the python2 series), and Python 3.4 and later (for Python 3).
    – michael
    Sep 6, 2016 at 9:02
  • It'd be great (and related to this question) if someone could clarify if sudo pip does anything w/ system dependencies as well; e.g., does a sudo pip on Ubuntu actually call apt-get to install (or remove!) anything? For instance, if I'm installing ipython via pip (putting it into /usr/local by default), but an older one was already installed via apt-get (/usr/bin), does my system only work correctly if /usr/local/bin is in my path ahead of /usr/bin? These issues seem to be resolved at run-time. Or, does sudo pip do anything special at install time?
    – michael
    Sep 6, 2016 at 9:07
  • @michael_n Not sure, but I strongly believe that pip is completely unrelated to apt. One does not use the other and vice versa.
    – Byte Commander
    Sep 6, 2016 at 11:05
  • 1
    @ByteCommander but pip does seem to uninstall system packages (or so it reports, when installing things); others seem to agree, but this is not clearly documented (as far as I can tell): askubuntu.com/questions/333475/…
    – michael
    Sep 6, 2016 at 11:23

1 Answer 1

8

To me the only difference is the scheme path.

Using sudo your package will be installed in:

/usr/local/lib/python2.7/dist-packages/

With --user they are installed in your $HOME directory in:

$HOME/.local/lib/python2.7/site-packages

For your own system, I'd say it does not really matter. Use --user if you're not a sudoer member.

For code you publish, avoid --user.

4
  • 1
    Could you explain why to avoid --user, please? I only read about avoiding sudo yet...
    – Byte Commander
    Jun 26, 2015 at 8:11
  • @ByteCommander: For a program you publish I'd not make any assumptions of the target directory of the end user. It can be run with root or not. So I'd not mess my install script with commands that would install modules in several locations. Jun 26, 2015 at 8:18
  • 1
    @ByteCommander I should add (and confirm) that sudo is for modules you trust. If you want to test a new fancy package, try it first with --user. Jun 26, 2015 at 8:21
  • my inclination is that this answer is wrong. With ruby, you want that restricted to a user and not to effect the system as a whole. The same reasoning applies here. However, I'm not sure, so just making a comment. Please correct.
    – Thufir
    Jan 2, 2017 at 3:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .