588

I know the obvious answer is to use virtualenv and virtualenvwrapper, but for various reasons I can't/don't want to do that.

So how do I modify the command

pip install package_name

to make pip install the package somewhere other than the default site-packages?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Monika Sulik
  • 16,498
  • 15
  • 50
  • 52
  • 3
    Related: [How to make editable install of Python package from vcs into specific directory using pip?](http://stackoverflow.com/q/10866199/95735) – Piotr Dobrogost Jun 02 '12 at 22:06
  • 2
    Now question number two: when you're already installing into a custom directory, how to make `pip` NOT try to remove and older version from a non-custom directory. For example - a system-wide one, where you have no write permissions. So far I only pulled this off with `easy_install`... – Tomasz Gandor Dec 11 '14 at 15:41
  • 2
    @TomaszGandor I think using `--ignore-installed` option should prevent pip from trying to uninstall already installed packages. – Piotr Dobrogost Aug 18 '16 at 08:30
  • 1
    sorry I am new with pip, but is your question the same as asking "as how to have pi install to a different version of python"? I have python 3.4 and 3.5 but i want my pip installations to go to python 3.5. – Charlie Parker Feb 08 '17 at 17:30
  • @Charlie Nope, different question. I don't know enough about your installation/intentions, but generally I would probably use virtualenvwrapper and create a virtual environment with something like `mkvirtualenv --python=/usr/bin/python3.5 env_name` – Monika Sulik Feb 08 '17 at 22:06
  • @MonikaSulik woud you do that even if you were using a docker image? Virtual envs inside docker seem silly to me, but in most other case they are amazing! – Charlie Parker Feb 08 '17 at 22:21
  • probably not, but I'd avoid having both python3.4 and 3.5 on the same docker image tbh I suggest you create your own question :) Others might have better insights. – Monika Sulik Feb 09 '17 at 15:43
  • @CharlieParker I think these questions refer to question in your comment https://stackoverflow.com/questions/2812520/dealing-with-multiple-python-versions-and-pip https://stackoverflow.com/questions/10919569/install-a-module-using-pip-for-specific-python-version – braulio Jul 01 '20 at 09:28

19 Answers19

794

The --target switch is the thing you're looking for:

pip install --target=d:\somewhere\other\than\the\default package_name

But you still need to add d:\somewhere\other\than\the\default to PYTHONPATH to actually use them from that location.

-t, --target <dir>
Install packages into <dir>. By default this will not replace existing files/folders in <dir>.
Use --upgrade to replace existing packages in <dir> with new versions.


Upgrade pip if target switch is not available:

On Linux or OS X:

pip install -U pip

On Windows (this works around an issue):

python -m pip install -U pip
Janusz Skonieczny
  • 17,642
  • 11
  • 55
  • 63
  • Hmmm: my version of pip (1.0.2 on Windows, installed for Python 2.6) replies "no such option" when I try "--target". – Dan H Nov 01 '13 at 00:34
  • 8
    @DanH run `pip install --upgrade pip`! – r3m0t Mar 19 '14 at 16:51
  • 21
    This is the true answer, it's just the option was added quite a bit after the accepted answer. – bukzor Apr 06 '14 at 17:57
  • 8
    What's the difference between `--install-option="--prefix=$PREFIX_PATH"` mentioned by @Ian Bicking and the `--target=$PATH` option? – Hibou57 Aug 15 '14 at 15:49
  • 8
    `target` is a pip option, and everything you put in `install-option` will be passed on to the `setup.py install command`. Basically `target` is custom site-packages location. – Janusz Skonieczny Aug 18 '14 at 09:19
  • 14
    Using `--target` may result in a partial installation, since it will not install any including scripts/data files in the specified prefix. It seems like passing `--prefix` with `--install-option` is the only proper way to have full control over the used installations prefix. – Kenneth Hoste Jan 26 '15 at 11:11
  • 1
    I get `DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both` when I try to use `--target` Pip 6.0.8 Py2.7 – Anentropic Mar 31 '15 at 10:51
  • 3
    Please note that `--target` is not supported when using `--editable` at the same time – https://github.com/pypa/pip/issues/562 – Piotr Dobrogost Apr 04 '15 at 15:27
  • Thank you very much for sharing this information, which was amazingly hard to find! But you are wrong about the pythonpath. The first place a Python script is searching for modules is in its own folder - wich could very well be d:\somewhere\other\than\the\default – turingtested May 12 '15 at 22:40
  • 2
    pip 1.5.4 says --target 'no such option' – szeitlin May 13 '15 at 02:40
  • 1
    @szeitlin You're using a very old version of pip. Run `pip install --upgrade pip` to upgrade. – Pieter Dec 30 '15 at 10:34
  • @Pieter thanks, just using the version the rest of my company is using. I'll upgrade it locally for my own convenience. – szeitlin Dec 30 '15 at 19:02
  • Unfortunately, this doesn't work if the package being installed is already present elsewhere on the system, but that installation is outdated and cannot be changed. – Arthur Jul 18 '16 at 17:46
  • 2
    @Arthur In this case use `--ignore-installed` option. – Piotr Dobrogost Aug 18 '16 at 08:29
  • @KennethHoste You can use `--install-option` with `--target` too, see here http://stackoverflow.com/questions/26476607 – chhantyal Aug 25 '16 at 21:55
  • 1
    is there a way to have this target option "permanent" so that I don't have to tell pip were to install ever single time? I just want it to install to my python 3.5 instead of my python 3.4. I am currently doing `pip install --target=/usr/local/lib/python3.5/dist-packages pkg_name` – Charlie Parker Feb 08 '17 at 18:12
  • 1
    Then use pip from python 3.5 location. – Janusz Skonieczny Feb 09 '17 at 11:54
  • @turingtested, yes but only if you star from this location if you install a package in A and run python from B then B doesn't have A in python path, unless you add it there. Am I correct? – Janusz Skonieczny Sep 25 '17 at 10:00
  • @Arthur or rearrange python path so you'r location is before the default location. – Janusz Skonieczny Sep 25 '17 at 10:02
  • My next experiment with the crap flooding the python world was this. Of course, it didn't work, this time the error was the nothing-saying error message: `distutils.errors.DistutilsOptionError: can't combine user with prefix, exec_prefix/home, or install_(plat)base` – peterh Feb 04 '18 at 02:50
  • 1
    When using `--target` be wary of [this bug](https://github.com/pypa/pip/issues/3826). – Björn Lindqvist Feb 05 '20 at 23:15
  • You need to add `/home/path/of/directory/lib/python3.6/site-packages/` to `PYTHONPATH`, for me – Kamil Jul 07 '20 at 13:16
  • It might not work if specified via [`PIP_TARGET`](https://github.com/pypa/pip/issues/8438#issuecomment-674420308). – x-yuri Aug 15 '20 at 20:25
  • How do I uninstall the package from the target directory? `pip uninstall --target ` doesn't work. – Sadman Sakib Jun 11 '22 at 15:49
  • @SadmanSakib to uninstall just delete the target dir, and remove the corresponding PYTHONPATH line from bashrc – Ariel Lubonja Dec 15 '22 at 20:46
367

Use:

pip install --install-option="--prefix=$PREFIX_PATH" package_name

You might also want to use --ignore-installed to force all dependencies to be reinstalled using this new prefix. You can use --install-option to multiple times to add any of the options you can use with python setup.py install (--prefix is probably what you want, but there are a bunch more options you could use).

Ian Bicking
  • 9,762
  • 6
  • 33
  • 32
  • 6
    if you do this, is there a way to get `pip freeze` to see the alternate directory? – Russ Jul 22 '11 at 06:54
  • 7
    pip freeze looks on the path, so if you something like `PYTHONPATH=$PREFIX_PATH/lib/python2.6/site-packages pip freeze` it should see them. – Ian Bicking Aug 03 '11 at 20:53
  • 5
    Using `--prefix=$PREFIX_PATH` doesn't seem to allow to have full control of installation directory as there's system specific suffix being appended to it (`\Lib\site-packages` on Windows for example). Is there a way to specify specific directory? – Piotr Dobrogost Jun 02 '12 at 22:04
  • 2
    @Piotr: yes there is see my answer. Using '--prefix' is a bit coarse, but works nice if you want your pure python to go under /usr/lib/pythonX.Y/site-packages instead of /usr/local/lib/pythonX.Y/site-packages. – Anthon Jun 13 '12 at 14:39
  • Throoze: it should if you use `PYTHONPATH` as with `pip freeze` – Ian Bicking Dec 28 '13 at 03:44
  • It doesn't work for dependancies, the --target option worked perfectly as per an answer below – hammady Apr 12 '14 at 19:46
  • 123
    Not a bad answer 4 years ago, but the --target option exists now. – Tritium21 Sep 28 '14 at 18:20
  • This relies on the setup.py using it, and some packages do not. The --target option mentioned below is a more robust solution these days – Davide Mar 19 '15 at 18:28
  • This doesn't perfectly install everything into prefix for me. --user gave me much better results. – Nopik Apr 29 '15 at 20:07
  • 2
    The `PYTHONUSERBASE` environment variable combined with the pip `--user` option ensures that everything installs into the specified prefix correctly, but my answer hasn't had as much love as this and the `--target` suggestion yet. – David Gardner Jun 05 '15 at 13:44
  • Dear Ian, thanks a lot for this. Would you be so kind to look into this post: http://stackoverflow.com/questions/42699279/installing-a-python-package-in-a-desired-folder it is the same question but for cases without pip – user929304 Mar 09 '17 at 16:23
  • 1
    Not a bad answer 7 years ago, but the --prefix option exists directly for pip now. – Mike T Aug 15 '17 at 21:58
  • This doesnt work for UNINSTALL you get: Cannot uninstall requirement django, not installed, so something is missing – kensai Dec 05 '17 at 11:09
  • 1
    Can someone give me an example of the string argument to the `--install-option=` flag? I don't know what `prefix` or `PREFIX_PATH` refers to or whether `$PREFIX_PATH` in the string `--prefix=$PREFIX_PATH` should be substituted with the path to the location where I wish to install the python package. Would something like `pip install --install-option="--prefix=c:\Users\Bob\Desktop" numpy` install `numpy` to the `Desktop` directory? – Minh Tran Jan 21 '18 at 05:51
  • 1
    As it is in quite usual in the Python world, it doesn't work - it still installs the packages into `~/.local/lib`. – peterh Feb 04 '18 at 02:48
  • @MinhTran Use the `--target` flag instead, then you can just use the string as the argument – Amon Jun 12 '21 at 02:14
  • This no longer works as of 15-Dec-2022. I get `ERROR: Location-changing options found in --install-option: ['--prefix'] from command line. This is unsupported, use pip-level options like --user, --prefix, --root, and --target instead.` – Ariel Lubonja Dec 15 '22 at 20:52
107

Instead of the --target or --install-options options, I have found that setting the PYTHONUSERBASE environment variable works well (from discussion on a bug regarding this very thing):

PYTHONUSERBASE=/path/to/install/to pip install --user

(Or set the PYTHONUSERBASE directory in your environment before running the command, using export PYTHONUSERBASE=/path/to/install/to)

This uses the very useful --user option but tells it to make the bin, lib, share and other directories you'd expect under a custom prefix rather than $HOME/.local.

Then you can add this to your PATH, PYTHONPATH and other variables as you would a normal installation directory.

Note that you may also need to specify the --upgrade and --ignore-installed options if any packages upon which this depends require newer versions to be installed in the PYTHONUSERBASE directory, to override the system-provided versions.

A full example

PYTHONUSERBASE=/opt/mysterypackage-1.0/python-deps pip install --user --upgrade numpy scipy

..to install the scipy and numpy package most recent versions into a directory which you can then include in your PYTHONPATH like so (using bash and for python 2.6 on CentOS 6 for this example):

export PYTHONPATH=/opt/mysterypackage-1.0/python-deps/lib64/python2.6/site-packages:$PYTHONPATH
export PATH=/opt/mysterypackage-1.0/python-deps/bin:$PATH

Using virtualenv is still a better and neater solution!

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
David Gardner
  • 6,952
  • 4
  • 35
  • 37
  • 4
    This worked with Travis CI running on Docker containers whereas the --install-option="--prefix=$PREFIX_PATH" solution did not. – 32bits Aug 09 '15 at 21:34
  • 1
    Noobie question, how important is the `/bin` folder pip creates, `--user` creates it as does `--PREFIX` whereas `--target` does not. – Jonathan Sep 27 '16 at 14:13
  • @Jonathan Very important! You need this for many pip-installed packages to have their actual commands be available. For example, I have used "afew" alongside notmuch, and the "fpm" package creation tool, and both of these add a stub script to the `bin` directory. I'm not at all sure how you can run these as standalone executables without this! – David Gardner Sep 28 '16 at 11:08
  • @Jonathan In fact, this is entirely the reason I went looking for an alternative to the `--target` and `--prefix` options -- to cover all the types of installed files at once :) – David Gardner Sep 28 '16 at 11:11
  • 1
    If package is installed in global Python, `--ignore-installed` is needed. – George Sovetov Jan 17 '17 at 11:35
  • 1
    This option is also compatible with `--editable` and local installs. – mdh Feb 27 '17 at 13:57
  • 2
    This option comes handy for installing packages as superuser for another non-root user without having to `su` around (which may be problematic in containers, for example). – mdh Feb 27 '17 at 14:05
  • 1
    I have a shared home for both RH6 and RH7 clusters. So I set `PYTHONUSERBASE=$HOME/.rh6local` for RH6 and use the default for RH7. – fchen Nov 18 '19 at 22:00
  • 1
    this does not work. it only installs `lib`. the `bin` directory is not created. Mac catalina 10.15.2 (19C57), pip 20.0.2, python 3.8 – Sang Jan 30 '20 at 00:55
  • @transang Works for me with python 3.7.4 and pip3 19.1.1 on macOS 10.15.2, so might be a problem with particular python and pip versions? Or something to do with the new macOS security/protection mechanisms blocking writing to a bin dir for you? – David Gardner Feb 02 '20 at 16:25
  • 1
    I found this solution is the only way to automatically parse .pth file under a directory. For example, when I add a directory into PYTHONPATH, which is copied from a site-packages and contains protobuf-3.1.0-py2.7-nspkg.pth and a `google` dir, `import google.protobuf` will report a error. However PYTHONUSERBASE works! – Tomas Jan 25 '21 at 08:53
  • --target must overwrite the bin file, use --user will not – daohu527 Jan 16 '23 at 02:41
  • @daohu527 If you install into a new dir per (set of) package(s) then this doesn't matter, as you'd not already have a certain installed in the target directory, which is generally the reason I use this. I tend to create environment modules for sets of packages if I'm using this rather than virtualenv. But I still use virtualenv when possible, I just put this as a possible answer to the original question! :) – David Gardner Feb 09 '23 at 22:30
52

To pip install a library exactly where I wanted it, I navigated to the location I wanted the directory with the terminal then used

pip install mylibraryName -t . 

the logic of which I took from this page: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/download

46

Installing a Python package often only includes some pure Python files. If the package includes data, scripts and or executables, these are installed in different directories from the pure Python files.

Assuming your package has no data/scripts/executables, and that you want your Python files to go into /python/packages/package_name (and not some subdirectory a few levels below /python/packages as when using --prefix), you can use the one time command:

pip install --install-option="--install-purelib=/python/packages" package_name

If you want all (or most) of your packages to go there, you can edit your ~/.pip/pip.conf to include:

[install]
install-option=--install-purelib=/python/packages

That way you can't forget about having to specify it again and again.

Any excecutables/data/scripts included in the package will still go to their default places unless you specify addition install options (--prefix/--install-data/--install-scripts, etc., for details look at the custom installation options).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • 2
    A big plus for the config file. I personally specify the `--prefix` there, because I have a "local" directory on a shared server in the home directory, and it was used as the prefix by `easy_install` before I moved to `pip`. System `$PATH` and `$PYTHONPATH` were configured before. Instead of `install-option=--install-purelib=/blah` there is this newer `target=/blah` option/switch. It is also nice, but sometimes you need just a replacement for `--prefix`, that you'd use with `setup.py` or `easy_install`. – Tomasz Gandor Dec 13 '13 at 10:10
  • `ERROR: Location-changing options found in --install-option: ['--install-purelib'] from command line. This is unsupported, use pip-level options like --user, --prefix, --root, and --target instead.`, with pip 22.0.4 – user7610 Apr 29 '22 at 11:25
31

Tested these options with python3.5 and pip 9.0.3:

pip install --target /myfolder [packages]

Installs ALL packages including dependencies under /myfolder. Does not take into account that dependent packages are already installed elsewhere in Python. You will find packages from /myfolder/[package_name]. In case you have multiple Python versions, this doesn't take that into account (no Python version in package folder name).

pip install --prefix /myfolder [packages]

Checks if dependencies are already installed. Will install packages into /myfolder/lib/python3.5/site-packages/[packages]

pip install --root /myfolder [packages]

Checks dependencies like --prefix but install location will be /myfolder/usr/local/lib/python3.5/site-packages/[package_name].

pip install --user [packages]

Will install packages into $HOME: /home/[USER]/.local/lib/python3.5/site-packages Python searches automatically from this .local path so you don't need to put it to your PYTHONPATH.

=> In most of the cases --user is the best option to use. In case home folder can't be used because of some reason then --prefix.

Seppo Enarvi
  • 3,219
  • 3
  • 32
  • 25
Mikko
  • 929
  • 8
  • 5
  • 1
    Running under python 2.7.16, --target (or --prefix) installs Jinja2-2.10.1.dist-info/ for example, whereas install --install-option="--prefix installs Jinja2-2.10.1-py2.7.egg-info/, which is what I actually wanted – Greg Dougherty Aug 07 '19 at 17:52
  • Doesn't always work via [environment variables](https://github.com/pypa/pip/issues/8438#issuecomment-674420308). – x-yuri Aug 15 '20 at 17:38
  • You can also set the directory where `--user` installs the packages in `$PYTHONUSERBASE`. Is there any difference betseen using `--user` and setting `$PYTHONUSERBASE` vs using `--prefix` and setting `$PYTHONPATH`? – Seppo Enarvi Dec 22 '22 at 13:23
26
pip install "package_name" -t "target_dir"

source - https://pip.pypa.io/en/stable/reference/pip_install/

-t switch = target

Samir Kape
  • 1,733
  • 1
  • 16
  • 19
21

Nobody seems to have mentioned the -t option but that the easiest:

pip install -t <direct directory> <package>
Erwan
  • 3,733
  • 30
  • 25
  • 11
    The `-t` option is the short version of the `--target` option which has been described in another answer (https://stackoverflow.com/a/19404371/594053) :) – MattiSG Apr 03 '18 at 05:40
16
pip install packageName -t pathOfDirectory

or

pip install packageName --target pathOfDirectorty
Zoe
  • 27,060
  • 21
  • 118
  • 148
kundan kaushik
  • 217
  • 2
  • 10
15

Just add one point to @Ian Bicking's answer:

Using the --user option to specify the installed directory also work if one wants to install some Python package into one's home directory (without sudo user right) on remote server.

E.g.,

pip install --user python-memcached

The command will install the package into one of the directories that listed in your PYTHONPATH.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Good Will
  • 1,220
  • 16
  • 10
13

Newer versions of pip (8 or later) can directly use the --prefix option:

pip install --prefix=$PREFIX_PATH package_name

where $PREFIX_PATH is the installation prefix where lib, bin and other top-level folders are placed.

Mike T
  • 41,085
  • 18
  • 152
  • 203
4

To add to the already good advice, as I had an issue installing IPython when I didn't have write permissions to /usr/local.

pip uses distutils to do its install and this thread discusses how that can cause a problem as it relies on the sys.prefix setting.

My issue happened when the IPython install tried to write to '/usr/local/share/man/man1' with Permission denied. As the install failed it didn't seem to write the IPython files in the bin directory.

Using "--user" worked and the files were written to ~/.local. Adding ~/.local/bin to the $PATH meant I could use "ipython" from there.

However I'm trying to install this for a number of users and had been given write permission to the /usr/local/lib/python2.7 directory. I created a "bin" directory under there and set directives for distutils:

vim ~/.pydistutils.cfg

[install]
install-data=/usr/local/lib/python2.7
install-scripts=/usr/local/lib/python2.7/bin

then (-I is used to force the install despite previous failures/.local install):

pip install -I ipython

Then I added /usr/local/lib/python2.7/bin to $PATH.

I thought I'd include this in case anyone else has similar issues on a machine they don't have sudo access to.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stuart Brock
  • 3,596
  • 1
  • 22
  • 21
2

If you are using brew with python, unfortunately, pip/pip3 ships with very limited options. You do not have --install-option, --target, --user options as mentioned above.

Note on pip install --user
The normal pip install --user is disabled for brewed Python. This is because of a bug in distutils, because Homebrew writes a distutils.cfg which sets the package prefix. A possible workaround (which puts executable scripts in ~/Library/Python/./bin) is: python -m pip install --user --install-option="--prefix=" <package-name>

You might find this line very cumbersome. I suggest use pyenv for management. If you are using

brew upgrade python python3

Ironically you are actually downgrade pip functionality.

(I post this answer, simply because pip in my mac osx does not have --target option, and I have spent hours fixing it)

anonymous
  • 1,372
  • 1
  • 17
  • 22
  • This answer with an explicit prefix, and `ignore-installed` due to previous efforts, worked for me: `python -m pip install --user --install-option="--prefix='/myFunkyApp/lib'" --ignore-installed ` – Jeremy Jones Aug 28 '19 at 16:00
0

With pip v1.5.6 on Python v2.7.3 (GNU/Linux), option --root allows to specify a global installation prefix, (apparently) irrespective of specific package's options. Try f.i.,

$ pip install --root=/alternative/prefix/path package_name
sphakka
  • 457
  • 4
  • 11
0

I suggest to follow the documentation and create ~/.pip/pip.conf file. Note in the documentation there are missing specified header directory, which leads to following error:

error: install-base or install-platbase supplied, but installation scheme is incomplete

The full working content of conf file is:

[install]
install-base=$HOME
install-purelib=python/lib
install-platlib=python/lib.$PLAT
install-scripts=python/scripts
install-headers=python/include
install-data=python/data

Unfortunatelly I can install, but when try to uninstall pip tells me there is no such package for uninstallation process.... so something is still wrong but the package goes to its predefined location.

kensai
  • 943
  • 9
  • 16
0

pip install /path/to/package/

is now possible.

The difference with this and using the -e or --editable flag is that -e links to where the package is saved (i.e. your downloads folder), rather than installing it into your python path.

This means if you delete/move the package to another folder, you won't be able to use it.

A H
  • 2,164
  • 1
  • 21
  • 36
0

system` option, that will install pip package-bins to /usr/local/bin thats accessible to all users. Installing without this option may not work for all users as things go to user specific dir like $HOME/.local/bin and then it is user specific install which has to be repeated for all users, also there can be path issues if not set for users, then bins won't work. So if you are looking for all users - yu need to have sudo access:

sudo su - 
python3 -m pip install --system <module>
logout
log back in 
which <module-bin> --> it should be installed on /usr/local/bin/
ashish
  • 409
  • 4
  • 7
0

Sometimes it works only works with Cache argument

-m pip install -U pip --target=C:\xxx\python\lib\site-packages Pillow --cache-dir C:\tmp
dazzafact
  • 2,570
  • 3
  • 30
  • 49
-1

use default venv, third party vitrualenv or virtualenvwrapper will be pain in future

shyed2001
  • 35
  • 6
  • 1
    The OP says: "*I know the obvious answer is to use virtualenv and virtualenvwrapper, but for various reasons I can't/don't want to do that.*" – Gino Mempin May 08 '23 at 23:37