Search code examples
pythonpippygamecarla

where should pygame be installed by pip? /usr/lib or ~/.local/lib?


I was testing CARLA, a self-driving car simulator on ubuntu 16.04.5 LTS last year and at that time, I had installed pygame. At that time pygame had been installed under /usr/lib/python2.7/dist-packages/pygame and I had fixed a file there to make it work right. Now, recently I re-installed ubuntu 16.04.5 LTS for the machine (only the OS part) and tried testing CARLA and found I have to install pygame(which is of course). So I did pip install pygame(without sudo) and CARLA now works again.
But soon I found the location of pygame installation is now not /usr/lib/python2.7/dist-packages/pygame but ~/.local/lib/python2.7/site-packages/pygame.
Why is it installed in my local home directory, not in the system directory? (I tried installing it with sudo, but it says Requirement alread satisfied.)
I tried python -m pip uninstall pip but received message below.

 Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
You are using pip version 8.1.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

should I do pip upgrade?
ADD : This question is not about 'sudo' or 'apt install'. Normally in apt install, if we omit sudo, it asks for root priviledge and doesn't install it. But pip installs it under ~/.local. Therefore this question is different from suggested duplicate question.


Solution

  • First, python -m pip uninstall pip is meant to uninstall pip, not pygame, so you wouldn't want to do that.

    When invoked with sudo, pip will install the libraries system-wide on /usr/lib, so they're accessible by all users.

    When called without sudo and/or with a --user flag, they're installed for your user only on your home directory. In either case, pip reports the library as already installed, and doesn't need to re-install it. Take a look at the link provided by triplee, as well as here

    You can use the following, to verify that Python looks at both of these paths to find available libraries.

    import sys
    print(sys.path)
    

    To address your point, both of these locations are fine, it just a matter of preference.

    If you want to uninstall pygame from ~/.local you should try pip uninstall pygame, and then use sudo pip install pygame again, to have a system-wide, static version installation.

    In my opinion, it's better to have user dependencies gathered locally, so that each user is free to install/remove/update libraries and modules to his requirements, and preferably inside his Virtual Environment.