MrIcyBalls
MrIcyBalls

Reputation: 83

NameError: global name 'This_is_a_Function_Name' is not defined

I am having a function called downloadfile so i type in the Shell:

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

It is important to bear in mind that the downloadfile function is being used inside another function called vario.

Following the typical procedure:

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

The function vario has the following code:

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

But I am confronted with this error whenever i try to >>> vario(g) with g the name of the feed I get:

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

I am unable to understand how a function that has been imported and had even the module containing her imported cannot operate. Can you help me?

Upvotes: 0

Views: 719

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121924

Functions look up globals in the module they are defined in. In other words, globals are only visible per module.

Add from mod3 import downloadfile to the source of mod2.

Your Python interpreter session is it's own module (it has a global namespace too), so if you copy and paste the vario() function into your interpreter session, it'll use the globals there instead.

Upvotes: 4

Related Questions