Skip to main content
 首页 » 编程设计

python之pip 安装在共享目录 (windows)

2024年10月01日9wuhuacong

我已经尝试创建自己的 Pypi 存储库,尊重 https://www.python.org/dev/peps/pep-0503/ .我的想法是将它放在一个共享目录中(我使用的是 Windows),比如说 host1/my-pypi .我已经生成了所需的 index.html:

(dir) host1\my-pypi 
     -> (dir) toto 
         (file) index.html 
         (file) toto-1.0.0.whl 
     -> (file) index.html 

index.html文件看起来有些正常(与 pep-503 相同)。我尝试从另一台计算机说 host2有权访问此共享目录以使用以下方式安装 toto 包:

pip install --index-url file://host1/my-pipy toto 

但它失败了(当它试图用 OSError: [Errno 22] Invalid argument: '\\\\host1\\my-pypi\\' 读取文件时)。

首先,有没有人尝试过(并解决过)这个问题?(这是简单的解决方案 :-))。

其次,我对 pip 的代码进行了一些研究,有几件事我不清楚(如果 pip 中的专家可以回答 ;-))。

  1. index.py , 方法 find_all_candidates : 它会自动更改我的 url 列表 [file://host1/my-pypi/toto] , 所以它似乎从不尝试读取 host1\my-pypi\index.html ……奇怪?

  2. index.py , 方法 get_page : 第 1 pip 对我来说并没有阻塞,因为它神奇地匹配了我的架构,但是有一个奇怪的情况:

    # Tack index.html onto file:// URLs that point to directories 
    (scheme, netloc, path, params, query, fragment) = \ 
        urllib_parse.urlparse(url) 
    if (scheme == 'file' and 
            os.path.isdir(urllib_request.url2pathname(path))): 
        # add trailing slash if not present so urljoin doesn't trim 
        # final segment 
        if not url.endswith('/'): 
            url += '/' 
        url = urllib_parse.urljoin(url, 'index.html') 
        logger.debug(' file: URL is directory, getting %s', url) 
    resp = session.get( 
        url, 
        headers={ 
            "Accept": "text/html", 
            "Cache-Control": "max-age=600", 
        }, 
    ) 
    

    好吧,正如预期的那样,我们有:

    scheme = 'file' 
    netloc = 'host1' 
    path = '/my-pypi/' 
    

    但是检查条件os.path.isdir(urllib_request.url2pathname(path)))显然是错误的,因为我们已经丢弃了网络位置。因此 index.html没有附加到路径(否则会是正确的),因此在尝试读取不存在的文件时出错。

请您参考如下方法:

对于远程 UNC 文件共享,请使用四个斜杠指定文件 URL:

pip install --index-url file:////host1/my-pypi toto 

在这种情况下,解析的 netloc 为空并且 path 仍然包含 UNC 格式的服务器主机名,因此 isdir 可以正常工作并且index.html 将按预期附加。