Install a Python package into a different directory using pip?
Asked Answered
E

20

606

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?

Episcopalism answered 26/5, 2010 at 17:55 Comment(8)
Related: How to make editable install of Python package from vcs into specific directory using pip?Hefter
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...Murtha
@TomaszGandor I think using --ignore-installed option should prevent pip from trying to uninstall already installed packages.Hefter
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.Pannikin
@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_nameEpiscopalism
@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!Pannikin
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.Episcopalism
@CharlieParker I think these questions refer to question in your comment #2813020 #10920069Woebegone
C
369

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).

Canella answered 26/5, 2010 at 19:59 Comment(17)
if you do this, is there a way to get pip freeze to see the alternate directory?Argyrol
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.Canella
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?Hefter
@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.Gerrygerrymander
Throoze: it should if you use PYTHONPATH as with pip freezeCanella
It doesn't work for dependancies, the --target option worked perfectly as per an answer belowFifty
Not a bad answer 4 years ago, but the --target option exists now.Ursula
This relies on the setup.py using it, and some packages do not. The --target option mentioned below is a more robust solution these daysStickinthemud
This doesn't perfectly install everything into prefix for me. --user gave me much better results.Boffin
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.Brittan
Dear Ian, thanks a lot for this. Would you be so kind to look into this post: #42699779 it is the same question but for cases without pipLacagnia
Not a bad answer 7 years ago, but the --prefix option exists directly for pip now.Cusk
This doesnt work for UNINSTALL you get: Cannot uninstall requirement django, not installed, so something is missingMerchant
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?Exigible
As it is in quite usual in the Python world, it doesn't work - it still installs the packages into ~/.local/lib.Pozzy
@MinhTran Use the --target flag instead, then you can just use the string as the argumentSchoolmarm
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.Nygaard
B
829

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
Begley answered 16/10, 2013 at 13:10 Comment(27)
Hmmm: my version of pip (1.0.2 on Windows, installed for Python 2.6) replies "no such option" when I try "--target".Truism
@DanH run pip install --upgrade pip!Carlton
This is the true answer, it's just the option was added quite a bit after the accepted answer.Maurizio
What's the difference between --install-option="--prefix=$PREFIX_PATH" mentioned by @Ian Bicking and the --target=$PATH option?Jotting
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.Begley
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.Caveat
I get DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both when I try to use --target Pip 6.0.8 Py2.7Relations
Please note that --target is not supported when using --editable at the same time – github.com/pypa/pip/issues/562Hefter
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\defaultCopyedit
pip 1.5.4 says --target 'no such option'Citation
@Citation You're using a very old version of pip. Run pip install --upgrade pip to upgrade.Sharie
@Sharie thanks, just using the version the rest of my company is using. I'll upgrade it locally for my own convenience.Citation
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.Waterfowl
@Waterfowl In this case use --ignore-installed option.Hefter
@KennethHoste You can use --install-option with --target too, see here stackoverflow.com/questions/26476607Precaution
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_namePannikin
Then use pip from python 3.5 location.Begley
@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?Begley
@Waterfowl or rearrange python path so you'r location is before the default location.Begley
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)basePozzy
When using --target be wary of this bug.Completion
You need to add /home/path/of/directory/lib/python3.6/site-packages/ to PYTHONPATH, for meWaldgrave
It might not work if specified via PIP_TARGET.Shannan
How do I uninstall the package from the target directory? pip uninstall --target <dir> doesn't work.Uterus
@SadmanSakib to uninstall just delete the target dir, and remove the corresponding PYTHONPATH line from bashrcNygaard
Seems that --target=<path> does not work as expected if the path start with ~ (tilde is not expanded to home), but it works if you do not use equals sign, but --target <path>. (pip 23.2.1). Edited the answer to remove the =.Minuet
@Waldgrave Thank you very much for your comment! This helped. Summary: The packages directory (e.g. /home/path/of/directory/lib/python3.6/site-packages/) must be in PYTHONPATH, otherwise the packages are installed correctly but not found by Python.Publicity
C
369

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).

Canella answered 26/5, 2010 at 19:59 Comment(17)
if you do this, is there a way to get pip freeze to see the alternate directory?Argyrol
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.Canella
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?Hefter
@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.Gerrygerrymander
Throoze: it should if you use PYTHONPATH as with pip freezeCanella
It doesn't work for dependancies, the --target option worked perfectly as per an answer belowFifty
Not a bad answer 4 years ago, but the --target option exists now.Ursula
This relies on the setup.py using it, and some packages do not. The --target option mentioned below is a more robust solution these daysStickinthemud
This doesn't perfectly install everything into prefix for me. --user gave me much better results.Boffin
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.Brittan
Dear Ian, thanks a lot for this. Would you be so kind to look into this post: #42699779 it is the same question but for cases without pipLacagnia
Not a bad answer 7 years ago, but the --prefix option exists directly for pip now.Cusk
This doesnt work for UNINSTALL you get: Cannot uninstall requirement django, not installed, so something is missingMerchant
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?Exigible
As it is in quite usual in the Python world, it doesn't work - it still installs the packages into ~/.local/lib.Pozzy
@MinhTran Use the --target flag instead, then you can just use the string as the argumentSchoolmarm
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.Nygaard
B
110

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!

Brittan answered 17/3, 2015 at 15:24 Comment(13)
This worked with Travis CI running on Docker containers whereas the --install-option="--prefix=$PREFIX_PATH" solution did not.Hopscotch
Noobie question, how important is the /bin folder pip creates, --user creates it as does --PREFIX whereas --target does not.Gretel
@Gretel 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!Brittan
@Gretel 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 :)Brittan
If package is installed in global Python, --ignore-installed is needed.Bilski
This option is also compatible with --editable and local installs.Amoretto
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).Amoretto
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.Giovanna
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.8Pistoia
@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?Brittan
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!Rosetterosewall
--target must overwrite the bin file, use --user will notHerman
@Herman 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! :)Brittan
S
53

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

Stamps answered 31/8, 2015 at 17:46 Comment(2)
-t is shorthand for --targetGretel
This was the only solution which worked for me. (Windows 7)Thomasina
G
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).

Gerrygerrymander answered 13/6, 2012 at 14:39 Comment(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.Murtha
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.4Collimate
A
36

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.

Agostino answered 20/12, 2018 at 14:6 Comment(4)
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 wantedGrisgris
Doesn't always work via environment variables.Shannan
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?Beaut
What I found my python3 pipe install --prefix /mydir, then the package will be installed unser /mydir/local/lib/python3.10/dist-packages. I am not sure where you can control the default behavior which is not desirable. I want the local to be missing and the dist-package to be site-packagePaynim
T
25
pip install "package_name" -t "target_dir"

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

-t switch = target

Talkfest answered 11/5, 2020 at 14:9 Comment(1)
@merv it's different because it's not the same. isn't it?Suspense
A
21

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

pip install -t <direct directory> <package>
Atchley answered 20/2, 2018 at 0:2 Comment(1)
The -t option is the short version of the --target option which has been described in another answer (https://mcmap.net/q/64182/-install-a-python-package-into-a-different-directory-using-pip) :)Soniasonic
A
16
pip install packageName -t pathOfDirectory

or

pip install packageName --target pathOfDirectorty
Avogadro answered 22/2, 2019 at 8:35 Comment(1)
This is a duplicate of another answer.Juniper
U
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.

Unfrequented answered 23/12, 2014 at 19:46 Comment(0)
C
14

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.

Cusk answered 24/5, 2017 at 6:37 Comment(0)
B
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.

Buchenwald answered 29/5, 2015 at 14:24 Comment(0)
G
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)

Gallicanism answered 17/9, 2018 at 13:22 Comment(1)
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 <package-name>Ovenware
P
1

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.

Piscatory answered 18/1, 2019 at 8:37 Comment(2)
this doesn't answer the questionEngorge
This lets you install a "local" package at the path you put it in.Piscatory
A
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
Atelectasis answered 18/8, 2014 at 14:53 Comment(0)
M
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.

Merchant answered 5/12, 2017 at 10:57 Comment(0)
P
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/
Provision answered 17/3, 2021 at 13:50 Comment(0)
D
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
Delaware answered 29/12, 2022 at 10:47 Comment(0)
L
0

Use:

pip install package_name -t directory_path

If you get the following error:

ERROR: Can not combine '--user' and '--target'

Use:

pip install package_name -t directory_path --no-user

e.g. pip install pandas -t C:\Users\user\Desktop\Family\test --no-user

Ljubljana answered 19/11, 2023 at 6:32 Comment(0)
F
-3

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

Fortuna answered 6/5, 2023 at 8:18 Comment(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."Vivianne

© 2022 - 2024 — McMap. All rights reserved.