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
?
Instead of the
--target
option or the--install-options
option, I have found that the following works well (from discussion on a bug regarding this very thing at https://github.com/pypa/pip/issues/446):(Or set the
PYTHONUSERBASE
directory in your environment before running the command, usingexport PYTHONUSERBASE=/path/to/install/to
)This uses the very useful
--user
option but tells it to make thebin
,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 thePYTHONUSERBASE
directory, to override the system-provided versions.A full example:
..to install the
scipy
andnumpy
package most recent versions into a directory which you can then include in yourPYTHONPATH
like so (using bash and for python 2.6 on CentOS 6 for this example):Using virtualenv is still a better and neater solution!
The --target switch is the thing you're looking for:
But you still need to add
d:\somewhere\other\than\the\default
toPYTHONPATH
to actually use them from that location.Upgrade pip if target switch is not available:
On Linux or OS X:
On Windows (this works around an issue):
To pip install a library exactly where I wanted it, I navigated to the location I wanted the directory with the terminal then used
the logic of which I took from this page: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/download
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.
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)
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.,
The command will install the package into one of the directories that listed in your PYTHONPATH.
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 are dependencies 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.