333

Is it possible? When installing pip, install the python packages inside my $HOME folder. (for example, I want to install mercurial, using pip, but inside $HOME instead of /usr/local)

I'm with a mac machine and just thought about this possibility, instead of "polluting" my /usr/local, I would use my $HOME instead.

PEP370 is exactly about this. Is just creating a ˜/.local and do a pip install package enough to make these packages to be installed only at my $HOME folder?

alex
  • 6,818
  • 9
  • 52
  • 103
  • Have you checked out virtualenv? You could install packages with pip in an isolated environment. – joet3ch Aug 22 '11 at 04:30
  • 2
    I thought about it, but I would like to install some python applications using the existing python (2.6.1), but instead of installing at /usr/local/, install at my $HOME folder and add it to $PATH. I want to know if it's possible, and any caveats of this approach. – Somebody still uses you MS-DOS Aug 22 '11 at 05:12
  • 11
    +1 for promoting PEP370. This is a simple but useful option that more people should know about. – Ned Deily Aug 22 '11 at 05:57
  • I was fighting with similar problem (possibly caused by misconfigured pip and easy_install for two different pyhtons). As last resort, I tried to use plain ```$ python setup.py --user install```. And it worked. Package is now installed at home subdir and all works as expected. Will have to talk to my server admin. – Jan Vlcinsky May 13 '13 at 22:21
  • 1
    If you need to install PIP, just do something like `wget https://bootstrap.pypa.io/get-pip.py` followed by `python get-pip.py` and you're good to go. Might be useful if you're on a machine where the installed PIP is too old (was the case for me). See http://pip.readthedocs.org/en/latest/installing.html for more info. – Markus Sep 02 '14 at 13:15
  • @JanVlcinsky for me, with python 3.5, that didn't work. It was: `python setup.py install --user` – Akronix Oct 25 '17 at 10:20
  • May I know how to do the same for conda install. As ````conda install --user -c conda-forge gdal```` doesn't work. – mArk May 26 '20 at 04:44

4 Answers4

474

While you can use a virtualenv, you don't need to. The trick is passing the PEP370 --user argument to the setup.py script. With the latest version of pip, one way to do it is:

pip install --user mercurial

This should result in the hg script being installed in $HOME/.local/bin/hg and the rest of the hg package in $HOME/.local/lib/pythonx.y/site-packages/.

Note, that the above is true for Python 2.6. There has been a bit of controversy among the Python core developers about what is the appropriate directory location on Mac OS X for PEP370-style user installations. In Python 2.7 and 3.2, the location on Mac OS X was changed from $HOME/.local to $HOME/Library/Python. This might change in a future release. But, for now, on 2.7 (and 3.2, if hg were supported on Python 3), the above locations will be $HOME/Library/Python/x.y/bin/hg and $HOME/Library/Python/x.y/lib/python/site-packages.

Hugo Lopes Tavares
  • 28,528
  • 5
  • 47
  • 45
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • Good point. Unfortunately, there is a bug in Python 2.7 that causes PYTHONUSERBASE to be ignored for OS X framework builds. It'll be fixed for Python 2.7.3. – Ned Deily Aug 22 '11 at 08:02
  • 1
    This looks very insteresting. `easy_install` comes installed in Mac OS X by default, so I would have only pip installed outside the `$HOME` folder. – Somebody still uses you MS-DOS Aug 22 '11 at 14:08
  • The `easy_install` that comes with my Snow Leopard installs `pip` on `/Library/Python/2.6/site-packages/pip-1.0.2-py2.6.egg`. Doing `which pip` shows `/usr/local/bin/pip`. Installing mercurial with `--user` argument and pip, `which hg` now shows `$HOME/.local/bin/hg`!. (I needed to add `$HOME/.local/bin` to `$PATH` to make it work though.) – Somebody still uses you MS-DOS Aug 23 '11 at 03:26
  • 1
    I have tried to change the PYTHONUSERBASE to another location. No success. How is this done on Windows specifically? – Snerd Jul 07 '13 at 21:31
  • 2
    Beware, that if you do this and then try to use conda environments the files installed in `.local` will always be hit first which can lead to very hard to track down import errors. – tacaswell May 11 '15 at 02:48
  • 13
    Note that on OSX, the user site directory's bin folder is not on PATH. This means scripts installed by packages in the user site directory will not be executable unless you `export PATH=$PATH:~/Library/Python/X.Y/bin/`. – André Caron Dec 31 '15 at 17:45
  • For anyone wondering, on Windows this installs scripts to %APPDATA%\Python\Python36\Scripts – Somo S. May 27 '17 at 22:53
  • Also useful when the package you want to install locally is virtualenv. – Paul Richter Aug 17 '17 at 10:33
  • `$ echo $(python -c "import site; print(site.USER_BASE)")/bin /Users/systemdisc/Library/Python/2.7/bin` – Timothy Zorn Oct 14 '17 at 18:25
  • @NedDeily May I know how to do the same for conda install. As ````conda install --user -c conda-forge gdal```` doesn't work. – mArk May 26 '20 at 04:38
  • You can also get the path of local packages via `python -m site --user-site` – Sorush Jun 04 '21 at 11:26
22

You can specify the -t option (--target) to specify the destination directory. See pip install --help for detailed information. This is the command you need:

pip install -t path_to_your_home package-name

for example, for installing say mxnet, in my $HOME directory, I type:

pip install -t /home/foivos/ mxnet
Foivos
  • 545
  • 4
  • 13
  • Getting an error `distutils.errors.DistutilsOptionError: can't combine user with prefix, exec_prefix/home, or install_(plat)base`. What does it mean? – Anthony Kong Aug 16 '19 at 07:15
  • Check https://github.com/pypa/pip/issues/3826 for a related discussion. I don't know anything more on this, but I never encountered a problem in my environments. – Foivos Aug 23 '19 at 18:29
  • 1
    I would rather give "-t /home/foivos/python_packages" as example, as installing directly into the home directory could lead to a big bunch of files "polluting" the $HOME – jifb Feb 09 '23 at 10:10
21

I would use virtualenv at your HOME directory.

$ sudo easy_install -U virtualenv
$ cd ~
$ virtualenv .
$ bin/pip ...

You could then also alter ~/.(login|profile|bash_profile), whichever is right for your shell to add ~/bin to your PATH and then that pip|python|easy_install would be the one used by default.

Ross Patterson
  • 5,702
  • 20
  • 38
  • 9
    The *"de facto"* way to do this is `virtualenv venv`, `source venv/bin/activate`. You might not necessarily want to **always** use your virtualenv, and by using this method, your prompt is prefixed with `(venv)` when you're using it. – alexia Jul 27 '14 at 11:46
  • 2
    @nyuszika7h We know how to use virtualenv. But this really answers the question and is a very neat solution. – itsafire Jan 18 '15 at 21:47
  • 8
    @itsafire Who is "we"? I recommend you to read https://en.wikipedia.org/wiki/Weasel_word – alexia Jan 18 '15 at 21:50
  • 4
    Anyway, I disagree, this is not a "neat solution". It's a pointless hack. – alexia Jan 18 '15 at 21:54
0

Short answer to your two questions grabbed from the other answers

One

Yes

it is possible installing pip packages to $HOME instead of /usr/local/lib/, but

Two

mkdir ˜/.local # then
pip install package 

is not enough.

You need

pip install package --user

and the packages get installed to

/home/user/.local/lib/python3.x/site-packages

Exception when you do not need --user

  • if you are not root user
  • On Debian together with Wsl - Windows Subsystem Linux no notice
  • On Ubuntu 20.04 notice default to user installation because normal site-packages not writeable - default config is /usr/lib not writeable for other users except root
Timo
  • 2,922
  • 3
  • 29
  • 28