Python 3.3中的请求处理

6

如何安装Requests以配合使用Python 3.3。

我使用Mac,已经安装了Python 3.3和2.7.1。路径如下。

2.7.1 : 

which python
/usr/bin/python

3.3:
/Library/Frameworks/Python.framework/Versions/3.3/bin

我尝试使用以下方法进行安装。
curl -O http://python-distribute.org/distribute_setup.py
sudo /Library/Frameworks/Python.framework/Versions/3.3/bin/python3 distribute_setup.py
sudo /Library/Frameworks/Python.framework/Versions/3.3/bin/python3 -m easy_install pip
sudo /Library/Frameworks/Python.framework/Versions/3.3/bin/pip install requests

最后一步出现错误:
 File "./requests/utils.py", line 22, in <module>

    from .compat import parse_http_list as _parse_list_header

  File "./requests/compat.py", line 112, in <module>

    from .packages import chardet2 as chardet

ImportError: cannot import name chardet2

我尝试将PYTHONPATH设置为:

echo $PYTHONPATH
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages
[~/tmp] $ 

仍然是同样的错误。

我也尝试在安装了chardet2之后使用,但是

   $ python3.3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import chardet2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'chardet2'
>>> 

编辑: 详细的pip.log文件

 Skipping link http://octodex.github.com/ (from https://github.com/kennethreitz/requests); not a file
    Skipping link https://github.com/site/terms (from https://github.com/kennethreitz/requests); not a file
    Skipping link https://github.com/site/privacy (from https://github.com/kennethreitz/requests); not a file
    Skipping link https://github.com/security (from https://github.com/kennethreitz/requests); not a file
  Using version 0.14.2 (newest of versions: 0.14.2, 0.14.1, 0.14.0, 0.13.9, 0.13.8, 0.13.7, 0.13.6, 0.13.5, 0.13.4, 0.13.3, 0.13.2, 0.13.1, 0.13.0, 0.12.1, 0.12.0, 0.11.2, 0.11.1, 0.10.8, 0.10.7, 0.10.6, 0.10.4, 0.10.3, 0.10.2, 0.10.1, 0.10.0, 0.9.3, 0.9.2, 0.9.1, 0.9.0, 0.8.9, 0.8.8, 0.8.7, 0.8.6, 0.8.5, 0.8.4, 0.8.3, 0.8.2, 0.8.1, 0.8.0, 0.7.6, 0.7.5, 0.7.4, 0.7.3, 0.7.2, 0.7.1, 0.7.0, 0.6.6, 0.6.5, 0.6.4, 0.6.3, 0.6.2, 0.6.1, 0.6.0, 0.5.1, 0.5.0, 0.4.1, 0.4.0, 0.3.4, 0.3.3, 0.3.2, 0.3.1, 0.3.0, 0.2.4, 0.2.3, 0.2.2, 0.2.1, 0.2.0)
  Downloading from URL http://pypi.python.org/packages/source/r/requests/requests-0.14.2.tar.gz#md5=488508ba3e8270992ad5b3fb54d364ca (from http://pypi.python.org/simple/requests/)
  Running setup.py egg_info for package requests

    Traceback (most recent call last):

      File "<string>", line 16, in <module>

      File "/private/var/folders/fc/k8b9ds214nv4lqv2x4395khc0000gn/T/pycharm-packaging377147531338803949.tmp/requests/setup.py", line 6, in <module>

        import requests

      File "./requests/__init__.py", line 52, in <module>

        from . import utils

      File "./requests/utils.py", line 22, in <module>

        from .compat import parse_http_list as _parse_list_header

      File "./requests/compat.py", line 112, in <module>

        from .packages import chardet2 as chardet

    ImportError: cannot import name chardet2

    Complete output from command python setup.py egg_info:

    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/private/var/folders/fc/k8b9ds214nv4lqv2x4395khc0000gn/T/pycharm-packaging377147531338803949.tmp/requests/setup.py", line 6, in <module>

    import requests

  File "./requests/__init__.py", line 52, in <module>

    from . import utils

  File "./requests/utils.py", line 22, in <module>

    from .compat import parse_http_list as _parse_list_header

  File "./requests/compat.py", line 112, in <module>

    from .packages import chardet2 as chardet

ImportError: cannot import name chardet2

----------------------------------------

Command python setup.py egg_info failed with error code 1 in /private/var/folders/fc/k8b9ds214nv4lqv2x4395khc0000gn/T/pycharm-packaging377147531338803949.tmp/requests

Exception information:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.2.1-py3.3.egg/pip/basecommand.py", line 107, in main
    status = self.run(options, args)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.2.1-py3.3.egg/pip/commands/install.py", line 256, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.2.1-py3.3.egg/pip/req.py", line 1042, in prepare_files
    req_to_install.run_egg_info()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.2.1-py3.3.egg/pip/req.py", line 236, in run_egg_info
    command_desc='python setup.py egg_info')
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.2.1-py3.3.egg/pip/util.py", line 612, in call_subprocess
    % (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command python setup.py egg_info failed with error code 1 in /private/var/folders/fc/k8b9ds214nv4lqv2x4395khc0000gn/T/pycharm-packaging377147531338803949.tmp/requests

chardet2requests 发行版的一部分,因此您可以使用 from requests.packages import chardet2 进行测试。 - Martijn Pieters
你的 Mac 上安装了 pip 吗? - rofls
@rofls:看一下安装日志,pip正在那里被安装。 - Martijn Pieters
from requests.packages import chardet2 回溯(Traceback)(最近的调用最先显示) File "<stdin>", line 1, in <module> ImportError: 没有名为 'requests' 的模块
- bsr
1
在奶酪商店页面(http://pypi.python.org/pypi/requests)上显示当前版本的“构建状态失败”。尝试从GitHub下载先前版本或当前tarball(如请求文档中所述),或者只需`pip-3.3 install git+https://github.com/kennethreitz/requests`(未经测试,但我会给出相同的几率)。 - abarnert
显示剩余2条评论
1个回答

12

芝士店页面目前显示版本0.14.2的“构建状态失败”。尝试下载并尝试构建/测试/安装它确实会在3.3上失败。这并不是太令人惊讶,因为0.14.1是第一个能与3.3一起使用的版本,而且可能还没有多少人在3.3上使用它...

那么除了以适当的方式报告错误之外,你可以做什么呢?两个选择:

  • 获取0.14.1 tarball并安装它。
  • 获取最新源并安装它。

第二个选择实际上更容易,也更好(当它可用时),而且在Requests的文档中也有建议。

然而,文档没有告诉您的是,您仍然可以使用最新源获得pip的好处和简单性:

pip-3.3 install git+https://github.com/kennethreitz/requests

我刚刚测试过这个,它运行良好并(如预期)给了我与按照Requests文档中的说明所得到的完全相同的结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接