Python 名称错误:全局名称';这是一个函数名';没有定义
我有一个名为Python 名称错误:全局名称';这是一个函数名';没有定义,python,Python,我有一个名为downloadfile的函数,因此我在Shell中键入: >>> import mod3 >>> from mod3 import downloadfile 请务必记住,downloadfile函数正在另一个名为vario的函数中使用 遵循典型程序: >>> import mod2 >>> from mod2 import vario 功能vario具有以下代码: def vario(feed):
downloadfile
的函数,因此我在Shell中键入:
>>> 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
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
康蒂(g)
文件“E:\Py\u env\df2.Py”,第15行,续
good_read=downloadfile(sourceurl,targetfname)#去写downloadfile函数吧!
NameError:未定义全局名称“downloadfile”
我无法理解一个已经导入的函数,甚至包含她导入的模块都无法运行。您能帮助我吗?函数在定义全局变量的模块中查找全局变量。换句话说,全局变量仅在每个模块中可见
将从mod3导入下载文件添加到mod2
的源中
Python解释器会话是它自己的模块(它也有一个全局名称空间),因此如果您将vario()
函数复制并粘贴到解释器会话中,它将使用其中的全局变量。函数在模块中查找它们定义的全局变量。换句话说,全局变量仅在每个模块中可见
将从mod3导入下载文件添加到mod2
的源中
Python解释器会话是它自己的模块(它也有一个全局名称空间),因此如果您将vario()
函数复制并粘贴到解释器会话中,它将使用其中的全局变量