330
votes

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?

3
Have you checked out virtualenv? You could install packages with pip in an isolated environment.joet3ch
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
+1 for promoting PEP370. This is a simple but useful option that more people should know about.Ned Deily
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
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 pip.readthedocs.org/en/latest/installing.html for more info.Markus

3 Answers

467
votes

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.

21
votes

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.

20
votes

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