NameError: global name 'datetime' is not defined

Hello,
I’m following the Data Dashboard tutorial and I’m getting stuck on Chapter 4, specifically with the importation of datetime. I get an error message that reads “NameError: global name ‘datetime’ is not defined.” Below the link to the app.

Thanks
Lou

Have you imported datetime before calling it?

Example,

from datetime import datetime

Hi Allan,

Yes, tried:

a) from datetime import datetime,

b) import datetime, and

c) from datetime import *

Got same error message in all.

(interestingly, it ran correctly yesterday)

perhaps you can post your clone link. See this link for instructions if that sentence didn’t make sense.

How to ask a good question

If you don’t get an import error then we know that datetime was imported correctly so it is there.

Off the top of my head make sure your importing datetime into each module you use it in.
Check the line of code that’s causing the error and make sure the import statements exist in that specific module.

Clone link:
https://anvil.works/build#clone:VV3CUZPB3VNOTWKA=2BEMABWYIEN3A2DKMEEOK7KG

I don’t get that error when I run your code. Just that there is a missing server function. If I remove the call to that server function the datetime module loads as expected.

Still having problems?

Yes, still got it today. Didn’t see the missing server function error when I ran it though.

I’m not sure we’re looking at the same versions do you have a live version and a published version?
If so, I’m probably looking at the published version which is not the version you are currently working on.

For me… your code looks like this.

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    self.build_revenue_graph()
    self.build_marketing_graph()
    self.build_signups_graph()

But in the version I’m looking at you don’t have a get_marketing_data function in your server module.

That’s correct. The tutorial indicates to include the get_marketing_data function within the python script used to access the anvil server. If I include it in the server module it runs with no problems. Here’s the code in the script:

import anvil.server
anvil.server.connect('ACCESS_KEY')

@anvil.server.callable
def get_marketing_data():
    # access data on your local machine and return as a Python list
    return [{'strategy':'Strategy A', 'count':200},
            {'strategy':'Strategy B', 'count':185},
            {'strategy':'Strategy C', 'count':175}]
            
anvil.server.wait_forever()