60

I'm trying to install Pandas using pip, but I'm having a bit of trouble. I just ran sudo pip install pandas which successfully downloaded pandas. However, it did not get downloaded to the location that I wanted. Here's what I see when I use pip show pandas:

---
Name: pandas
Version: 0.14.0
Location: /Library/Python/2.7/site-packages/pandas-0.14.0-py2.7-macosx-10.9-intel.egg
Requires: python-dateutil, pytz, numpy

So it is installed. But I was confused when I created a new Python Project and searched under System Libs/lib/python for pandas, because it didn't show up. Some of the other packages that I've downloaded in the past did show up, however, so I tried to take a look at where those were. Running pip show numpy (which I can import with no problem) yielded:

---
Name: numpy
Version: 1.6.2
Location: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requires: 

Which is in a completely different directory. For the sake of confirming my error, I ran pip install pyquery to see where it would be downloaded to, and got:

Name: pyquery
Version: 1.2.8
Location: /Library/Python/2.7/site-packages
Requires: lxml, cssselect

So the same place as pandas...

How do I change the default download location for pip so that these packages are downloaded to the same location that numpy is in?

Note: There were a few similar questions that I saw when searching for a solution, but I didn't see anything that mentioned permanently changing the default location.

weskpga
  • 2,017
  • 7
  • 29
  • 43
  • Do you know why numpy installed to that location? My friend is having similar issues but he's installing tensorflow. – jlarks32 Mar 28 '17 at 02:20
  • are you using venv? I'm asking because I was having the same error when I changed the location of my working folder, turned out I needed to delete the venv folder and create it again. Please let me know if this helps so I can post a full answer. – Daniel Carrera Jul 29 '20 at 07:36
  • venv (python virtual environment) is the answer and it is awesome – domih Mar 16 '21 at 22:14

6 Answers6

51

According to pip documentation at

http://pip.readthedocs.org/en/stable/user_guide/#configuration

You will need to specify the default install location within a pip.ini file, which, also according to the website above is usually located as follows

On Unix and Mac OS X the configuration file is: $HOME/.pip/pip.conf

On Windows, the configuration file is: %HOME%\pip\pip.ini

The %HOME% is located in C:\Users\Bob on windows assuming your name is Bob

On linux the $HOME directory can be located by using cd ~

You may have to create the pip.ini file when you find your pip directory. Within your pip.ini or pip.config you will then need to put (assuming your on windows) something like

[global]
target=C:\Users\Bob\Desktop

Except that you would replace C:\Users\Bob\Desktop with whatever path you desire. If you are on Linux you would replace it with something like /usr/local/your/path

After saving the command would then be

pip install pandas

However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere.

Community
  • 1
  • 1
Austin
  • 1,794
  • 13
  • 8
  • 1
    "However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere." So should I just include the path of this new folder in my Python Path? How exactly do I do that? – weskpga Jun 12 '14 at 01:50
  • It would depend on your operating system how you would include a certain path. "export PATH=$PATH:/path/to/dir1" will work in Linux which is what I believe you are using. If I am mistaken about your OS here is a link [link](http://www.computerhope.com/issues/ch000549.htm) for Windows. I hope this helps. – Austin Jun 12 '14 at 02:38
  • I would like to specify `.` as the target directory (the root directory) in `pip.conf` - but unfortunately it creates a new directory with name `.` :( Is there way to make this happen without hardcoding the path of the directory on my local filesystem? – Aditya M P Sep 17 '17 at 13:25
  • 9
    The location is %APPDATA%\pip not %HOME% (Win10) (Local, not Roaming) – Xodarap777 Nov 11 '18 at 23:32
  • Setting my global installation directory with `pip config set global.target /Users/massimopinto/Library/Python/3.8/site-packages`, and then installing a package via `pip`, let's call is _spekpy_, this ends in a sub-directory structure which replicates the global target, to: /Users/massimopinto/Library/Python/3.8/site-packages/Users/massimopinto/Library/Python/3.8/site-packages/spekpy and the result is that the package is not found when trying to import it. Dirty fix: after installation, move the package to Users/massimopinto/Library/Python/3.8/site-packages/. I am doing something wrong. – massimopinto Nov 26 '20 at 10:55
  • 3
    I see this post says the default location is `$HOME/.pip/pip.conf`, but when I ran the `pip config set global.target `, I noticed that's outdated information (which is also described in the documentation). It now respects the XDG Base Directory, and its variable, defaulting to `$HOME/.config/pip/`. – mazunki Aug 19 '21 at 16:07
  • Unfortunately, `global.target` also chanes `bindir` from `/usr/bin/` to `$PIP_TARGET/bin/` when installing as root – basin Oct 10 '21 at 20:02
32

You can set the following environment variable:

PIP_TARGET=/path/to/pip/dir

https://pip.pypa.io/en/stable/user_guide/#environment-variables

barbolo
  • 3,807
  • 1
  • 31
  • 31
  • 4
    In this case you need to specify `PYTHONPATH` (or some such) to be able to import the modules. E.g. `PIP_TARGET=site-packages`, `PYTHONPATH=site-packages`. – x-yuri Jun 24 '20 at 23:46
19

Open Terminal and type:

pip config set global.target /Users/Bob/Library/Python/3.8/lib/python/site-packages

except instead of

/Users/Bob/Library/Python/3.8/lib/python/site-packages

you would use whatever directory you want.

Jarrell Khoo
  • 191
  • 1
  • 2
  • 1
    This is a very effective solution. However, it can cause a lot of problems with pip installations and package updates either going to the wrong version of Python or throw outright errors when you want to pip with another version. To undo this command, use `pip config unset global.target`. It is also available at the user level as `pip config set/unset user.target`. – tommylicious Oct 06 '20 at 04:27
8

Follow these steps

  1. pip config set global.target D:\site-packages to change install path

or py -m pip config --user --editor notepad edit

[global]
target = D:\site-packages
  1. set environment variable to use download import xxx

    PIP_TARGET=site-packages PYTHONPATH=site-packages

3.pip config unset global.target, to upgrade pip py -m pip install --upgrade pip

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
user15408191
  • 81
  • 1
  • 1
1

@Austin's answer is outdated, here for more up-to-date solution:

According to pip documentation at

https://pip.pypa.io/en/stable/topics/configuration/

You will need to specify the default install location within a configuration file, which, also according to the website above is usually located as follows

Mac OS

$HOME/Library/Application Support/pip/pip.conf if directory $HOME/Library/Application Support/pip exists else $HOME/.config/pip/pip.conf.

The legacy “per-user” configuration file is also loaded, if it exists: $HOME/.pip/pip.conf.

The $HOME folder can be located by navigating to ~/ (cmd+shift+G in Finder; cmd+shift+. to show hidden files).

Windows

%APPDATA%\pip\pip.ini

The legacy “per-user” configuration file is also loaded, if it exists: %HOME%\pip\pip.ini

The %HOME% is located in C:\Users\Bob on windows assuming your username is Bob

Unix

$HOME/.config/pip/pip.conf, which respects the XDG_CONFIG_HOME environment variable.

The legacy “per-user” configuration file is also loaded, if it exists: $HOME/.pip/pip.conf.

On linux the $HOME directory can be located by using cd ~

You may have to create the configuration file when you find your pip directory. Put something like

[global]
target = /Library/Frameworks/Python.framework/Versions/Current/lib/python3.10/site-packages/

if you are on a Mac. Except that you would replace /Library/Frameworks/Python.framework/Versions/Current/lib/python3.10/site-packages/ with whatever path you desire. If you are on Linux you would replace it with something like /usr/local/your/path

After saving the command would then be

pip install pandas

However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere.

Please note that

pip3 install pandas

might be the solution if your packages gets installed in the Python2 folder vs Python3.

0

Best answer for windows:

go to the folder: C:\Users\ {your username} \AppData\Roaming\pip

there, you will find a file "pip.ini". Open it on notepad or similar. Just change the target destination to the folder you want.