177

I am trying to install flake8 package using pip3 and it seems that it refuses to install because is already installed in one local location.

How can I force it to install globally (system level)?

pip3 install flake8
Requirement already satisfied (use --upgrade to upgrade): flake8 in ./.local/lib/python3.4/site-packages

Please note that I would prefer a generic solution (that should work on Debian, OS X maybe even Windows), one that should be used on any platform so I don't want to specify the destination myself.

For some weird reason it behaves like I already specified --user which in my case I didn't.

The only way I was able to install a package globally was to first remove it and install it again after this. Somehow it seems that pip (8.1.1) refuses to install a package globally if it exists locally.

Disclaimer: No virtual environments were used or harmed during the experiments.

sorin
  • 161,544
  • 178
  • 535
  • 806
  • 8
    Have you tried from root user or sudo? – matino Apr 29 '16 at 10:57
  • Have you tried `pip install flake8 --upgrade`? – Mauro Baraldi Apr 29 '16 at 11:05
  • In case of similar issues related w/ the usage of virtual environments (venv) use `--system-site-packages` option to inherit globally installed packages into your venv. See https://stackoverflow.com/a/61326709/4445175 – Wlad Apr 20 '20 at 15:58

7 Answers7

221

Why don't you try sudo with the H flag? This should do the trick.

sudo -H pip install flake8

A regular sudo pip install flake8 will try to use your own home directory. The -H instructs it to use the system's home directory. More info at https://stackoverflow.com/a/43623102/

nikhilweee
  • 3,953
  • 1
  • 18
  • 13
  • 283
    `H` as in Hlobal? – Matt Dec 27 '18 at 23:03
  • 147
    `-H` as in *hey sudo, stop using my home directory. use your own `H`ome directory instead* https://stackoverflow.com/a/43623102/ – nikhilweee Jan 09 '19 at 13:29
  • 14
    This doesn't really install it globally, but instead seems to just store it in the user 'root''s home directory – parsecpython Feb 19 '20 at 02:04
  • 2
    @nikhilweee I don't understand how this is different than `pip3 install ipython` when virtualenv is deactivated, they seem to install to same `site-packages` directory. – iamfrancisyo May 03 '20 at 00:17
  • 1
    @parsecpython for me it does the trick and install the packages into /usr/local/lib/python3.8/dist-packages where I meant them to – yota Sep 22 '20 at 20:23
  • @yota I don't know if that is desired. Using `python -m venv env_path` actually pulls pip from `/usr/lib/python3/dist-packages` and not local. – rtaft Feb 10 '21 at 14:51
  • I had to install a package for the plpython3u extension of PostgreSQL. I needed to use `sudo`, but I did not use `-H` (I have not tested it), and I obviously also did not need `-H` to get the extension to find the package. – questionto42 Sep 02 '21 at 15:39
  • 1
    `sudo pip install` is probably a [bad practice](https://askubuntu.com/questions/802544/is-sudo-pip-install-still-a-broken-practice) – nocibambi Jan 25 '22 at 14:20
17

Maybe --force-reinstall would work, otherwise --ignore-installed should do the trick.

Emma
  • 430
  • 8
  • 14
8

Where does pip installations happen in python?

I will give a windows solution which I was facing and took a while to solve.

First of all, in windows (I will be taking Windows as the OS here), if you do pip install <package_name>, it will be by default installed globally (if you have not activated a virtual enviroment). Once you activate a virtual enviroment and you are inside it, all pip installations will be inside that virtual enviroment.


pip is installing the said packages but not I cannot use them?

For this pip might be giving you a warning that the pip executables like pip3.exe, pip.exe are not on your path variable. For this you might add this path ( usually - C:\Users\<your_username>\AppData\Roaming\Programs\Python\ ) to your enviromental variables. After this restart your cmd, and now try to use your installed python package. It should work now.

uinstinct
  • 630
  • 1
  • 7
  • 14
8

For windows 10:

Installing Python for all users is straight forward since when you install you have to click a checkbox for all users.

In order to install modules globally under C:\Program Files\Python310\Lib\site-packages start CMD prompt as administrator and then install modules

python -m pip install selenium
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8
5

For the Windows case:

Are you using virtualenv? If yes, deactivate the virtualenv. If you are not using a venv, the package should have already be installed on system level (system-wide). In that case, try to upgrade the package.

pip install flake8 --upgrade
questionto42
  • 7,175
  • 4
  • 57
  • 90
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • 6
    Not quite true. – GMaster Jun 19 '17 at 01:53
  • It is a problem of the question not being put clearly on purpose (it asks for a "generic" solution that holds at best on all operating systems). On Linux, this answer is just wrong: if you do not use `sudo` in front of `pip install`, it will be installed in the `.local` directory, then this answer would be wrong. On Windows, it should be right. – questionto42 Sep 02 '21 at 07:45
1

I actually don‘t see your issue. Globally is any package which is in your python3 path‘s site package folder.

If you want to use it just locally then you must configure a virtualenv and reinstall the packages with an activated virtual environment.

LIT
  • 73
  • 10
  • 3
    When creating a venv you can use `--system-site-packages .venv/dev` to inherit global packages so you don't have to reinstall them in each venv. This make sense for packages that one may want to use in any project, i.e. black, flake8, pytest. Example: `python -m venv --system-site-packages .venv/dev` – Wlad Apr 20 '20 at 15:38
  • 2
    I think they're referring to the fact that it is installed in a `.local` directory. I encounter the same issue on Windows, where it incorrectly installs to `c:\users\username\appdata\roaming\python\python39\site-packages` instead of `c:\program files\python39\lib\site-packages`. I think this happens because of insufficient permission, but once installed, it refuses to install to the global directory, even after it has sufficient permission. – cowlinator Apr 30 '21 at 21:56
  • @cowlinator The first part about the `.local` is understood. The second part might be right on Windows, I have not tested it. On Linux instead, `but once installed, it refuses to install to the global directory, even after it has sufficient permission` is wrong, I have tested it: first I installed without `sudo` to `.local`, that is, locally. Then with `sudo`, and it installed globally. The local installation did not block the global installation. – questionto42 Sep 02 '21 at 15:46
  • "Globally" means it should run for any user, not just the current one. – Nicola Musatti Jul 05 '23 at 12:34
0

Sorry for digging up the topic but there is a multiple issues which we could face on Windows OS.

  1. Lack of privileges in Python location for current user
  2. User AppData/Python location has not been added to PATH
  3. Package was installed with --user flag

How to really resolve these issues:

  1. Open your Python location and click Properties -> Security -> Edit
  2. Click Add... and add the "Users" group or alternatively add your current user which uses Python. Set checkbox "Full control"
  3. Change windows variables and in PATH add C:\Users\your_username>\AppData\Roaming\Programs<Python Version>\ This allow you to install packages for current user instead of globally
SerSergious
  • 449
  • 2
  • 5
  • 21