Pip如何能够通过名称或URL安装包



我知道您可以使用pip按名称安装Python包,但我最近发现您可以按URL安装Python包。例如:

pip install http://www.crummy.com/software/BeautifulSoup/unreleased/4.x/BeautifulSoup-4.0b.tar.gz

Pip是怎么做到的?如果一个包的名字看起来像一个URL呢?

当有疑问时,只需检查源代码。Pip在其下载模块中有一个函数来检查名称输入是否像URL。

def is_url(name):
    """Returns true if the name looks like a URL"""
    if ':' not in name:
        return False
    scheme = name.split(':', 1)[0].lower()
    return scheme in ['http', 'https', 'file', 'ftp'] + vcs.all_schemes

Edit:正如@MartijnPieters指出的那样,模块的命名是有标准的,在名称中添加冒号是不允许的。

如果你想把http:放在模块名称的开头,你可能不是在写我想下载的软件

最新更新