# Install a Python package into a different directory using pip?

## The Question :

468 people think this question is useful

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?

• 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
• @TomaszGandor I think using --ignore-installed option should prevent pip from trying to uninstall already installed packages.
• 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 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

346 people think this answer is useful

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). ## The Answer 2 609 people think this answer is useful 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  ## The Answer 3 78 people think this answer is useful 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!

44 people think this answer is useful

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

34 people think this answer is useful

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 .



21 people think this answer is useful

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]



where $PREFIX_PATH is the installation prefix where lib, bin and other top-level folders are placed. ## The Answer 10 11 people think this answer is useful pip install packageName -t pathOfDirectory  or pip install packageName --target pathOfDirectorty  ## The Answer 11 10 people think this answer is useful I found a simple way pip3 install "package_name" -t "target_dir"  -t switch = target ## The Answer 12 4 people think this answer is useful 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. ## The Answer 13 2 people think this answer is useful 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) ## The Answer 14 0 people think this answer is useful 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



0 people think this answer is useful

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

pip install /path/to/package/
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.