2014-07-04 8 views
0
내가 downloadfile라는 기능을 갖는하고

그래서 내가 셸에서 입력 정의되지 않은 :나가서 설명하자면 NameError : 전역 이름 'This_is_a_Function_Name'

>>> import mod3 
>>> from mod3 import downloadfile 

마음에 downloadfile 기능이 다른 내에서 사용되고 있음을 부담하는 것이 중요하다 함수 vario. 일반적인 절차에 따라

:

>>> import mod2 
>>> from mod2 import vario 

기능 vario 다음 코드를 가지고 :

def vario(feed): 
    import df 
    for item in feed.entries: #change feed to the name e.g. g = feedparser.parse('RSS-URL') change it to g 
     print(item[ "summary" ], item[ "title" ], item[ "published" ]) 
     # Identify ZIP file enclosure, if available 
     enclosures = [ l for l in item[ "links" ] if l[ "rel" ] == "enclosure" ] # it's saying take every l, where the 'rel' value is 'enclosure' 
     if (len(enclosures) > 0): 
      # ZIP file enclosure exists, so we can just download the ZIP file 
      enclosure = enclosures[0] 
     sourceurl = enclosure[ "href" ] 
     cik = item[ "edgar_ciknumber" ] 
     targetfname = df.target_dir+cik +' - ' +sourceurl.split('/')[-1] #df.target_dir change made 
     retry_counter = 3 
     while retry_counter > 0: 
      good_read = downloadfile(sourceurl, targetfname) # go and write the downloadfile function! 
      if good_read: 
       break 
      else: 
       print("Retrying:", retry_counter) 
       retry_counter -= 1 

하지만 g와 >>> vario(g)에 피드의 이름을하려고 할 때마다 나는이 오류에 직면하고있다 알겠습니다 :

Traceback (most recent call last): 
    File "<pyshell#24>", line 1, in <module> 
    contie(g) 
    File "E:\Py_env\df2.py", line 15, in contie 
    good_read = downloadfile(sourceurl, targetfname) # go and write the downloadfile function! 
NameError: global name 'downloadfile' is not defined 

가져온 기능과 가져온 기능을 포함하는 모듈이 어떻게 작동 할 수 있는지 서 있어야합니다. 나 좀 도와 줄 수있어?

답변

4

함수는에 정의 된 모듈에서 전역 변수 을 찾습니다. 즉, 모듈 당 만 볼 수 있습니다..

mod2의 출처에 from mod3 import downloadfile을 추가하십시오.

당신의 파이썬 인터프리터 세션은 자신 만의 모듈입니다 (전역 네임 스페이스도 있습니다). 따라서 vario() 함수를 복사하여 인터프리터 세션에 붙여 넣으면 대신 전역 변수가 사용됩니다.

+0

신의 축복 !!! 너무 단순하지만 아직 어떻게하는지 이해할 수 없었습니다! – MrIcyBalls

관련 문제