paul-g
paul-g

Reputation: 3877

How do I set the installation directory for pip?

How can I set the installation path for pip using get-pip.py to /usr/local/bin/? I can't find any mention in the setup guide or in the command line options.

To clarify I don't mean the path where pip packages are installed, but the path where pip itself is installed (it shuold be in /usr/local/bin/pip).

Edit

I do agree with many of the comments/answers that virtualenv would be a better idea in general. However it simply isn't the best one for me at the moment since it would be too disruptive; many of our user's scripts rely on a python2.7 being magically available; this is not convenient either and should change, but we have been using python since before virtualenv was a thing.

Upvotes: 3

Views: 6356

Answers (2)

paul-g
paul-g

Reputation: 3877

It seems the easiest workaround I found to do this is:

  1. install easy_install; this will go in /usr/local/bin/ as expected; the steps for doing this are listed here; I personally ended up running wget https://bootstrap.pypa.io/ez_setup.py -O - | python
  2. install pip with /usr/local/bin/easy_install pip; this will make pip go in /usr/local/bin

Upvotes: 2

Alex Huszagh
Alex Huszagh

Reputation: 14614

Pip itself is a Python package, and the actual pip command just runs a small Python script which then imports and runs the pip package.

You can edit locations.py to change installation directories, however, as stated above, I highly recommend that you do not do this.

Pip Command

Pip accepts a flag, '--install-option="--install-scripts"', which can be used to change the installation directory:

pip install somepackage --install-option="--install-scripts=/usr/local/bin"

Source method

On line 124 in pip/locations.py, we see the following:

site_packages = sysconfig.get_python_lib()
user_site = site.USER_SITE

You can technically edit these to change the default install path, however, using a virtual environment would be highly preferable. This is then used to find the egglink path, which then finds the dist path (code appended below, from pip/__init__.py).

def egg_link_path(dist):
    """
    Return the path for the .egg-link file if it exists, otherwise, None.

    There's 3 scenarios:
    1) not in a virtualenv
       try to find in site.USER_SITE, then site_packages
    2) in a no-global virtualenv
       try to find in site_packages
    3) in a yes-global virtualenv
       try to find in site_packages, then site.USER_SITE
       (don't look in global location)

    For #1 and #3, there could be odd cases, where there's an egg-link in 2
    locations.

    This method will just return the first one found.
    """
    sites = []
    if running_under_virtualenv():
        if virtualenv_no_global():
            sites.append(site_packages)
        else:
            sites.append(site_packages)
            if user_site:
                sites.append(user_site)
    else:
        if user_site:
            sites.append(user_site)
        sites.append(site_packages)

    for site in sites:
        egglink = os.path.join(site, dist.project_name) + '.egg-link'
        if os.path.isfile(egglink):
            return egglink


def dist_location(dist):
    """
    Get the site-packages location of this distribution. Generally
    this is dist.location, except in the case of develop-installed
    packages, where dist.location is the source code location, and we
    want to know where the egg-link file is.

    """
    egg_link = egg_link_path(dist)
    if egg_link:
        return egg_link
    return dist.location

However, once again, using a virtualenv is much more traceable, and any Pip updates will override these changes, unlike your own virtualenv.

Upvotes: 3

Related Questions