名称错误:未定义全局名称“This_is_a u Function_name”

2024-10-02 04:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为downloadfile的函数,因此我在Shell中键入:

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

请记住,downloadfile函数正在另一个名为vario的函数中使用。在

遵循典型程序:

^{pr2}$

函数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

我无法理解一个已导入的函数,甚至包含她导入的模块也无法运行。你能帮助我吗?在


Tags: the函数inimportdfiffeedcounter
1条回答
网友
1楼 · 发布于 2024-10-02 04:25:38

函数在中定义的模块中查找全局变量。换句话说,全局变量只对每个模块可见。在

from mod3 import downloadfile添加到mod2的源。在

{3{解释器也有全局解释器,如果它是你的全局解释器,那么就使用你的全局解释器模块。在

相关问题 更多 >

    热门问题