V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wjjjj7
V2EX  ›  Python

新人求问, mac os 系统,想使用 requests 库,安装后提示成功,但是在 Python import 时显示 name 'request' is not defined。求解决办法 T0T

  •  
  •   wjjjj7 · 2018-01-22 12:40:44 +08:00 · 13508 次点击
    这是一个创建于 2258 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 pip 安装了 requests 库,下面是终端:

    wujingyideMacBook:~ my$ sudo pip install requests Password: The directory '/Users/my/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/my/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting requests Downloading requests-2.18.4-py2.py3-none-any.whl (88kB) 100% |████████████████████████████████| 92kB 92kB/s Collecting certifi>=2017.4.17 (from requests) Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB) 100% |████████████████████████████████| 153kB 210kB/s Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |████████████████████████████████| 143kB 140kB/s Collecting idna<2.7,>=2.5 (from requests) Downloading idna-2.6-py2.py3-none-any.whl (56kB) 100% |████████████████████████████████| 61kB 171kB/s Collecting urllib3<1.23,>=1.21.1 (from requests) Downloading urllib3-1.22-py2.py3-none-any.whl (132kB) 100% |████████████████████████████████| 133kB 159kB/s Installing collected packages: certifi, chardet, idna, urllib3, requests Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

    上面显示安装成功后,在终端启用了 python,但是还是显示 name 'request' is not defined

    wujingyideMacBook:~ my$ python Python 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information.

    import requests r = request.get("http://www.baidu.com") Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'request' is not defined

    下面这段是在 pycharm 里显示的错误:

    import requests r = requests.get("http://www.baidu.com") r.status_code r.encoding = 'utf-8' r.text

    Traceback (most recent call last): File "/Users/my/PycharmProjects/demo/learn.py", line 1, in <module> import requests ImportError: No module named requests

    Process finished with exit code 1

    但是在 pycharm 的 progect interpreter 里明明添加了 requests 库

    本人用的是 mac,自带的 python2.7,目前安装了 pycharm oc,之前安装过 anaconda,又卸载了,不知道是不是电脑坏境有问题,求解

    mianju
        1
    mianju  
       2018-01-22 13:03:15 +08:00   ❤️ 1
    会不会是 pip 环境和 python 的执行环境不在一块?
    Sylv
        2
    Sylv  
       2018-01-22 13:23:15 +08:00 via iPhone   ❤️ 2
    第一个错是因为 request.get 拼错了,漏了 s。
    Sylv
        3
    Sylv  
       2018-01-22 13:28:14 +08:00 via iPhone
    另一个问题是 mac 下 pip 命令不需要 sudo,使用 sudo 反而可能造成权限问题。
    wjjjj7
        4
    wjjjj7  
    OP
       2018-01-22 13:37:51 +08:00
    拼写错误改了之后运行成功了,之后 pycharm 跑这段也莫名的不报错了,感谢感谢。虽然我还是觉得我的系统环境有问题,不过能用就行~
    wjjjj7
        5
    wjjjj7  
    OP
       2018-01-22 13:39:22 +08:00
    @Sylv 一开始没加 sudo,但是安装失败了,后来按照网上的说法加了 sudo 才安好,不知道后面还会有什么问题
    julyclyde
        6
    julyclyde  
       2018-01-22 14:52:25 +08:00   ❤️ 1
    @wjjjj7 重要的不是做什么,而是为什么
    sudo 不是随便用的
    tsoingkam
        7
    tsoingkam  
       2018-01-22 15:36:48 +08:00 via iPhone
    requests 吧
    wjjjj7
        8
    wjjjj7  
    OP
       2018-01-22 16:12:22 +08:00
    @julyclyde 已经研究了两天了找不出环境的问题,只能靠 sudo 暂时解决库的安装问题,小白一只,以后多跟大神们学习 T T
    TimePPT
        9
    TimePPT  
       2018-01-22 17:13:49 +08:00   ❤️ 1
    @wjjjj7 尽快学会使用 virtualenv 吧,Mac 上随意 sudo pip install 会出人命的
    houshuu
        10
    houshuu  
       2018-01-22 22:15:55 +08:00   ❤️ 1
    我是用 brew 安装 python 的,命令上 python2+pip2, python3+pip3 分开,很好管理。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3555 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:48 · PVG 12:48 · LAX 21:48 · JFK 00:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.