1

This is a pretty simple error. Same code being run directly in the notebook cell (works) VS being defined in a function and then called in the cell (doesn't work)

If I run this code in the notebook, it works fine:

import datetime
now = datetime.datetime.now()

#other date stuff for later
today = datetime.datetime(now.year,now.month,now.day)
yesterday = datetime.date.today() - datetime.timedelta(1)
thisyear = datetime.datetime(now.year,1,1)
daynum = (today-thisyear).days

#print report date info
print('Report for the date: %s  (day #%d)'%(yesterday,daynum))
print('Produced at: [%s]'%now)

however if I define a function with that code in another file (same folder), in a file called defs.py:

def currentdates():
    import datetime
    now = datetime.datetime.now()

    #other date stuff for later
    today = datetime.datetime(now.year,now.month,now.day)
    yesterday = datetime.date.today() - datetime.timedelta(1)
    thisyear = datetime.datetime(now.year,1,1)
    daynum = (today-thisyear).days

    return(now,today,yesterday,thisyear,daynum)

and then try to run it in the notebook, it doesn't work:

#setup dates
from defs import currentdates
from obspy import UTCDateTime

[now,today,yesterday,thisyear,daynum] = currentdates()

#print report date info
print('Report for the date: %s  (day #%d)'%(yesterday,daynum))
print('Produced at: [%s]'%now)

This is the error I get:

NameError                                 Traceback (most recent call last)
<ipython-input-15-d85dba8d0bba> in <module>()
      5 from obspy import UTCDateTime
      6 
----> 7 [now,today,yesterday,thisyear,daynum] = currentdates()
      8 
      9 #print report date info

/home/morgan/SPYDER/defs.py in currentdates()
     57     daynum = (today-thisyear).days
     58 
---> 59     return(now,today,yesterday,thisyear,daynum)
     60 
     61 print('..imported: currentdates() - outputs: [now,today,yesterday,thisyear,daynum]')

NameError: global name 'year' is not defined

I have looked at all the questions I can but they all have more complex issues as far as I can tell.

Other very similar questions I have seen and not been able to answer my question from (they generally seem to apply to 'circular referencing')

Botch-job solution

If I define: "year=2016" before I call the function, it works fine. Maybe the ipython notebook can't communicate with the calendar/clock on the computer? It shouldn't have to as the function is doing this surely? This sounds more like a bug than a code issue, maybe?

Community
  • 1
  • 1
mjp
  • 1,618
  • 2
  • 22
  • 37
  • 1
    I know nothing about IPython, but perhaps ``this`` has some special meaning. Maybe there is something like a preprocessor that is replacing it with something else, leaving ``year`` as a bare name. Otherwise, the exception makes no sense and there is probably a stray .py/.pyc/.pyo you are unaware of being found. – Ben Mar 21 '16 at 22:07
  • Hey @Ben - I just discovered I can add a year=2016 command just before I call the function and it works fine without an error. I don't want to have to do this in the final code but maybe it sheds some more light on the error. – mjp Mar 21 '16 at 22:11
  • Try using a variable name like ``currentyear`` instead to prove/disprove my theory. – Ben Mar 21 '16 at 22:12
  • Hey @Ben sorry I don't understand what you want me to change. Prior to setting year=2016, I don't have a variable called year and don't call one one. The closest I have is 'thisyear'. In the definition, now.year is used but this is part of the datetime module. Could you be more specific as to what you want me to test? – mjp Mar 21 '16 at 22:29
  • 1
    I am guessing that ``this`` has a special meaning, so try changing your variable name from ``thisyear`` to ``currentyear``. – Ben Mar 21 '16 at 22:31
  • 1
    Bang on man, that was the problem. Changed it from 'thisyear' to 'currentyear' and it worked. Is there some way to check what variable names are already being secretly used? -- If you want to submit it as an answer below (rather than a comment) I can accept it so the question is marked as answered, or I can do it. Thanks! – mjp Mar 21 '16 at 22:48
  • 1
    No clue whatsoever. Like I said, I know nothing about IPython, but given your code and exception, it seemed like the most likely explanation. – Ben Mar 22 '16 at 02:33
  • IPython does not do anything special with the word `this`, and the preprocessors it uses do not even run on imports. I would guess you imported the module, then changed it on disk, and IPython was still using the old code when you were expecting it to use the new code. – Thomas K Mar 22 '16 at 10:06
  • Hey Thomas K, I'm not sure that can be it. I tested whether there was any loading problems by completely closing the ipython notebooks (incl. ending the process from the terminal) and restarting the kernel in SPYDER (the GUI for python that I use) so there shouldn't have been anything misloaded? – mjp Mar 22 '16 at 16:18

0 Answers0