0

正如Python Packaging User Guide中所建议的那样,我创建了一个包并尝试将其上传到 testpypi 进行测试。我创建了一个发行版,注册它,然后上传到 testpypi:

me@machine$ cd mypackage
me@machine:~/mypackage$ python setup.py sdist bdist_wheel
me@machine:~/mypackage$ twine register dist/mypackage-1.0.0-py3-none-any.whl -r https://testpypi.python.org/pypi
me@machine:~/mypackage$ twine upload dist/* -r https://testpypi.python.org/pypi

这工作正常,但试图安装它

me@machine:~$ pip install -r https://testpypi.python.org/pypi mypackage

失败并出现以下错误:

Invalid requirement: '<?xml version="1.0" encoding="utf-8"?>'
Traceback (most recent call last):
[...]
pip._vendor.pyparsing.ParseException: Expected W:(abcd...) (at char 0), (line:1, col:1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/vol/home/me/miniconda3/envs/myenv/lib/python3.5/site-packages/pip/req/req_install.py", line 82, in __init__
    req = Requirement(req)
  File "/vol/home/me/miniconda3/envs/myenv/lib/python3.5/site-packages/pip/_vendor/packaging/requirements.py", line 96, in __init__
    requirement_string[e.loc:e.loc + 8]))
pip._vendor.packaging.requirements.InvalidRequirement: Invalid requirement, parse error at "'<?xml ve'"
4

1 回答 1

0

到底是怎么回事

使用 testpypi 服务器的命令行参数对于上传和安装是不一样的。对于 pip,该-r参数指定了一个需求文件,该文件显然可以是一个 URL。如果你添加一个-v你得到:

me@machine:~$ pip install -v -r https://testpypi.python.org/pypi mypackage

Looking up "https://testpypi.python.org/pypi" in the cache
No cache entry available
Starting new HTTPS connection (1): testpypi.python.org
"GET /pypi HTTP/1.1" 200 23968
Updating cache with response from "https://testpypi.python.org/pypi"
Invalid requirement: '<?xml version="1.0" encoding="utf-8"?>'
[...]

然而,来自的响应https://testpypi.python.org/pypi不是有效的需求文件并且 pip 崩溃。

如何解决这个问题

要使用 pypi 从其他来源安装pip install,您需要使用以下-i参数:

me@machine:~$ pip install -i https://testpypi.python.org/pypi mypackage

这应该可以按预期工作并mypackage从 testpypi 服务器安装。

于 2017-02-28T17:35:08.057 回答