Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75294 > unrolled thread

NameError: name 'requests' is not defined ?

Started by"水静流深" <1248283536@qq.com>
First post2014-07-28 11:52 +0800
Last post2014-07-28 05:07 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  NameError: name 'requests' is not defined ? "水静流深" <1248283536@qq.com> - 2014-07-28 11:52 +0800
    Re: NameError: name 'requests' is not defined ? Miki Tebeka <miki.tebeka@gmail.com> - 2014-07-27 21:27 -0700
    Re: NameError: name 'requests' is not defined � Steven D'Aprano <steve@pearwood.info> - 2014-07-28 05:07 +0000

#75294 — NameError: name 'requests' is not defined ?

From"水静流深" <1248283536@qq.com>
Date2014-07-28 11:52 +0800
SubjectNameError: name 'requests' is not defined ?
Message-ID<mailman.12374.1406519536.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

there is a simple file `mydown.py`  saved in `D:\Python34\Lib\site-packages`
 there is only one line in mydown.py .
 
 import requests  
 
  
 
 C:\Users\pengsir>d:\Python34\python
 Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AM
 D64)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import mydown
 >>> dir(mydown)
 ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
 '__package__', '__spec__', 'requests']
 >>> url='http://quote.eastmoney.com/hk/02222.html?StockCode=02222'
 >>> requests.get(url)
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 NameError: name 'requests' is not defined
 
 why it run into   "NameError: name 'requests' is not defined"?‍

[toc] | [next] | [standalone]


#75297

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2014-07-27 21:27 -0700
Message-ID<fb1fff77-bac8-4f6a-b1ff-0eb014db9d99@googlegroups.com>
In reply to#75294
Greetings,

> there is only one line in mydown.py .
>  
> 
> import requests  
> ...
> >>> import mydown
> >>> requests.get(url)
> 
> Traceback (most recent call last):
> 
>   File "<stdin>", line 1, in <module>
> 
> NameError: name 'requests' is not defined
You need to call mydown.requests, but I think you're missing the point of modules and imports. I suggest you go over https://docs.python.org/3.4/tutorial/

HTH,
--
Miki

[toc] | [prev] | [next] | [standalone]


#75298 — Re: NameError: name 'requests' is not defined �

FromSteven D'Aprano <steve@pearwood.info>
Date2014-07-28 05:07 +0000
SubjectRe: NameError: name 'requests' is not defined �
Message-ID<53d5daa0$0$29977$c3e8da3$5496439d@news.astraweb.com>
In reply to#75294
On Mon, 28 Jul 2014 11:52:03 +0800, 水静流深 wrote:

> there is a simple file `mydown.py`  saved in
> `D:\Python34\Lib\site-packages`
>  there is only one line in mydown.py .
>  
>  import requests

Just because mydown has imported requests, doesn't mean it is visible 
everywhere. Each module is a separate namespace, so that 

    requests = "something"

inside module A doesn't break 

    import requests

inside module B.

Inside mydown you import requests, but you still need to import it in the 
interactive interpreter too. Don't worry, the second import will be very 
fast, once a module is imported once the second and subsequent times is 
very quick.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web