Install a Python package into a different directory using pip?
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
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).
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):
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!
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).
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