如何设置pip的安装目录?

时间:2015-12-10 19:24:19

标签: python python-2.7 pip

如何使用pipget-pip.py设置/usr/local/bin/的安装路径?我无法在setup guide或命令行选项中找到任何提及。

澄清我并不是指安装pip包的路径,而是安装pip本身的路径(它应该在/usr/local/bin/pip中)。

修改

我同意很多评论/答案,一般来说,virtualenv会更好。然而,它对我来说根本不是最好的,因为它太具有破坏性;我们的许多用户脚本依赖于python2.7神奇可用;这也不方便也应该改变,但是我们之前一直在使用python,因为在virtualenv之前就是这样。

2 个答案:

答案 0 :(得分:2)

Pip本身是一个Python包,实际的pip命令只运行一个小的Python脚本,然后导入并运行pip包。

您可以编辑locations.py来更改安装目录,但是,如上所述,我高度建议您不要这样做。

Pip Command

Pip接受一个标志,' - install-option =" - install-scripts"',可用于更改安装目录:

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

来源方法

在pip / locations.py的第124行,我们看到以下内容:

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

您可以在技术上编辑这些以更改默认安装路径,但是,使用虚拟环境将是非常可取的。然后用它来查找egglink路径,然后找到dist路径(下面的代码来自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

然而,再次使用virtualenv更易于追踪,任何Pip更新都会覆盖这些更改,与您自己的virtualenv不同。

答案 1 :(得分:2)

我发现这样做最简单的解决方法是:

  1. 安装easy_install;这将按预期进入/usr/local/bin/;执行此操作的步骤列于here;我个人最终运行wget https://bootstrap.pypa.io/ez_setup.py -O - | python
  2. 使用/usr/local/bin/easy_install pip安装点子;这将使pip进入/usr/local/bin