如何通过pip3离线安装具有所有依赖项的python包?

yab*_*rth 5 dependencies pip installation python3

首先,我设法在无法访问互联网的服务器上安装 Ansible。但我想知道我的做法是否正确。

首先我通过下载了必要的依赖项pip3

pip3 download ansible -d .
Run Code Online (Sandbox Code Playgroud)

这导致下载以下文件:

ansible-2.9.4.tar.gz                          
cryptography-2.8-cp34-abi3-manylinux1_x86_64.whl  
MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl  
PyYAML-5.3.tar.gz
cffi-1.13.2-cp36-cp36m-manylinux1_x86_64.whl  
Jinja2-2.11.1-py2.py3-none-any.whl                
pycparser-2.19.tar.gz                              
six-1.14.0-py2.py3-none-any.whl
Run Code Online (Sandbox Code Playgroud)

现在我已经在远程计算机上提供了这些文件,我尝试使用

pip3 install ansible-2.9.4.tar.gz
Run Code Online (Sandbox Code Playgroud)

安装ansible。

这导致了以下错误:

Processing ./ansible-2.9.4.tar.gz
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 
'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 
0x7f00726f9ef0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/jinja2/
Run Code Online (Sandbox Code Playgroud)

所以我尝试手动安装 Jinja2:

pip3 install jinja2-2.11.1-py2.py3-none-any.whl
Run Code Online (Sandbox Code Playgroud)

但这也不起作用:

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection
 broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 
0x7fd303a23940>: Failed to establish a new connection: [Errno -2] Name or service not known',)': 
/simple/markupsafe/
Run Code Online (Sandbox Code Playgroud)

安装Markupsafe后,Jinja2也可以安装。最后pip3 install ansible-2.9.4.tar.gz我成功安装了ansible。

现在我的问题是。有没有办法告诉pip使用下载的文件来安装依赖项,或者有没有更简单的方法来离线安装​​特定的 python 包及其所有依赖项?

感谢您的帮助和最诚挚的问候。亚伯斯

Ana*_*man 3

\n

有没有办法告诉pip使用下载的文件来安装依赖项,或者有没有更简单的方法来离线安装​​特定的 python 包及其所有依赖项?

\n
\n

我相信您正在寻找的是--no-index--find-links选项pip install。根据官方pip install选项文档:

\n
\n

--无索引

\n

忽略包索引(只查看 --find-links URL)。

\n

-f, --查找链接

\n

如果是 html 文件的 url 或路径,则解析指向档案的链接。如果本地路径或 file:// url 是\xe2\x80\x99 目录,则在目录列表中查找档案。

\n
\n

通过这些选项,您可以执行以下操作。本地安装:

\n
pip3 install --no-index --find-links /some/path <package name>\n
Run Code Online (Sandbox Code Playgroud)\n

或远程安装(例如通过 HTTP):

\n
pip3 install --no-index --find-links http:\\\\remotes\\server <package name>\n
Run Code Online (Sandbox Code Playgroud)\n

在您的情况下,您应该能够简单地使用ansible包名称例如:

\n
pip3 install --no-index --find-links /some/path ansible\n\npip3 install --no-index --find-links http:\\\\remotes\\server ansible\n
Run Code Online (Sandbox Code Playgroud)\n

如果您愿意,可以使用完整的文件名:

\n
pip3 install --no-index --find-links /some/path ansible-2.9.4.tar.gz\n\npip3 install --no-index --find-links http:\\\\remotes\\server ansible-2.9.4.tar.gz\n
Run Code Online (Sandbox Code Playgroud)\n

假设所有必需的依赖项都位于同一位置(如原始问题中所列),则应正常安装它们(即无需手动按顺序安装每个依赖项)。

\n
\n

要求

\n

另一种选择可能是使用正确的依赖项安装顺序创建需求文件,例如:

\n

前任。要求.txt

\n
/path/to/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl \n/path/to/Jinja2-2.11.1-py2.py3-none-any.whl \n/path/to/ansible-2.9.4.tar.gz\n# ...                          \n
Run Code Online (Sandbox Code Playgroud)\n

然后使用egpip3 install -r requirements.txt来安装列出的软件包。您也可以再次使用 HTTP 链接:

\n

前任。要求.txt

\n
http:\\\\remotes\\server\\MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl \nhttp:\\\\remotes\\server\\Jinja2-2.11.1-py2.py3-none-any.whl \nhttp:\\\\remotes\\server\\ansible-2.9.4.tar.gz\n# ... \n
Run Code Online (Sandbox Code Playgroud)\n

这里明显的缺点是,假设一个软件包依赖于另一个软件包,您已经需要知道某些软件包需要安装的顺序。

\n
\n

其他 URL 选项

\n

您可能还想查看文档的版本控制系统 (VCS) 部分pip install,其中提供了与 VCS 链接(即 Git、Mercurial、Subversion 和 Bazaar)结合的示例pip install

\n
\n

参考

\n

点安装

\n

pip 安装(选项)

\n