如何从本地 python 包索引安装包?

Mik*_*rge 7 python pip pypi

https://www.python.org/dev/peps/pep-0503https://pip.pypa.io/en/stable/reference/pip_wheel/#cmdoption-i暗示能够从本地目录,但它在实践中是什么样子还不是很清楚。

我是否在本地目录中使用相同的 index.html 文件?对于本地目录,--extra-index-url 的参数是什么样的?

Mik*_*rge 6

假设您想从本地安装 2 个软件包:abc-xyzfoo,并且您有相应的软件包文件abc-xzy-1.2.3.tar.gzfoo-1.0.0.tar.gz

我们会将您的本地 pypi 目录放在/my_local_pypi/simple

您的目录结构将如下所示:

/my_local_pypi/simple
  index.html
  - abc-xyz/
     index.html     
     abc-xyz-1.2.3.tar.gz  
  - foo/
     index.html
     foo-1.0.0.tar.gz
Run Code Online (Sandbox Code Playgroud)

index.html需要<a href></a>每个包的锚条目,因此应如下所示:

$ cat /my_local_pypi/simple/index.html
<!DOCTYPE html><html><body>
<a href="abc-xyz">abc-xyz></a></br>
<a href="foo">foo</a></br>
</body></html>
Run Code Online (Sandbox Code Playgroud)

然后每个都$package/index.html需要一个<a href></a>指向实际包文件的锚点,因此它们应该如下所示:

$ cat /my_local_pypi/simple/abc-xyz/index.html
<!DOCTYPE html><html><body>
<a href="abc-xyz-1.2.3.tar.gz">abc-xyz-1.2.3.tar.gz</a></br>
</body></html>

$ cat /my_local_pypi/simple/foo/index.html
<!DOCTYPE html><html><body>
<a href="foo-1.0.0.tar.gz">foo-1.0.0.tar.gz</a></br>
</body></html>
Run Code Online (Sandbox Code Playgroud)

然后在你的 中requirements.txt,你可以这样做:

$ cat requirements.txt
--extra-index-url file:///my_local_pypi/simple/
abc-xyz==1.2.3
foo==1.0.0
Run Code Online (Sandbox Code Playgroud)

然后你就可以开始了:pip install -r requirements.txt

另请参阅piprepo项目,它在生成所需的本地目录结构方面做得非常好。


wim*_*wim 5

如果您有要通过 pip 搜索的发行版目录,您可以简单地包含该目录的路径:

pip install --extra-index-url=/path/to/wheelhouse somepackage
Run Code Online (Sandbox Code Playgroud)

如果您根本不想搜索远程 PyPI --index-url--extra-index-url则可以使用代替。请注意,也可以添加--extra-index-url和/或--index-urlrequirements.txt文件顶部。

使用pip,您还可以直接从本地文件安装发行版。例如,要安装copyingmock发行版:

$ curl https://pypi.python.org/packages/d9/26/5ae8945356634c87cdf099bd7cee57799df46798af90ae5ccb03961c6359/copyingmock-0.1-py2.py3-none-any.whl > copyingmock-0.1-py2.py3-none-any.whl
$ pip install ./copyingmock-0.1-py2.py3-none-any.whl
Run Code Online (Sandbox Code Playgroud)

我已经展示了一个带有二进制发行版的示例,但同样适用于源发行版 (.tar.gz)。


mme*_*hig 2

您不必使用任何index.html文件。

运行以下命令就足够了:

pip install "path/to/file.whl"

这将从本地车轮文件安装。

  • 您也可以使用requirements.txt 文件中的路径吗? (3认同)