Nameerror: Name 'Datetime' Is Not Defined

NameError: name 'datetime' is not defined

You need to import the module datetime first:

>>> import datetime

After that it works:

>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)

NameError: name 'datetime' is not defined [while running 'ChangeDataType DistrictAllocationAndListStore-ptransform-570']

You can try to put import datetime within the function.

Why am I getting this NameError: name dt' is not defined

dt means nothing in your current code what the interpreter kindly tells you.

What you're trying to do is to call a datetime.datetime.fromtimestamp()

You can change your import to:

import datetime as dt

and then dt will be an alias for datetime package so dt.datetime.fromtimestamp(created) will work.

But this is not the only solution. The below also will work:

import datetime
datetime.datetime.fromtimestamp(created)
from datetime import datetime
datetime.fromtimestamp(created)

You can check this out to have a better understatement of the differences.

Python Django NameError: name 'datetime' is not defined

The problem is that you did not imported the datetime module (or at least not in a qualified way): you imported the date attribute.

Use the date class

You can replace datetime.date with just date (and this is the only element you need):

from django.db import models
from django.utils import timezone
from datetime import date
class Item(models.Model):

FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)

TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)

author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = date.today)
on_shopping_list = models.BooleanField()

def __str__(self):
return self.title

Perform an import of the module

Alternatively, you can import the module, and then use datetime.date:

from django.db import models
from django.utils import timezone
import datetime
class Item(models.Model):

FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)

TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)

author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=datetime.date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = datetime.date.today)
on_shopping_list = models.BooleanField()

def __str__(self):
return self.title

Use auto_now_add of the DateField

Regardless how we import this, in fact Django has already support for such default: with auto_now_add=True [Django-doc]: this will add a default value that is equal to today. Furthermore it will make the field blank=True, and editable=False, to prevent it to show up in forms, etc. So it is not completely equivalent, although it is likely what you mean:

# Note: makes the fields blank=True, and editable=False as well.

from django.db import models
from django.utils import timezone

class Item(models.Model):

FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)

TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)

author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", auto_now_add=True)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(auto_now_add=True)
on_shopping_list = models.BooleanField()

def __str__(self):
return self.title

NameError: name 'datetime' is not defined with datetime imported

df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]

I think that line is the problem.

Your Dataframe df at the end of the line doesn't have the attribute .time

For what it's worth I'm on Python 3.6.0 and this runs perfectly for me:

import requests
import datetime
import pandas as pd

def daily_price_historical(symbol, comparison_symbol, limit=1, aggregate=1, exchange='', allData='true'):
url = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit={}&aggregate={}&allData={}'\
.format(symbol.upper(), comparison_symbol.upper(), limit, aggregate, allData)
if exchange:
url += '&e={}'.format(exchange)
page = requests.get(url)
data = page.json()['Data']
df = pd.DataFrame(data)
df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]
#I don't have the following function, but it's not needed to run this
#datetime.datetime.fromtimestamp()
return df

df = daily_price_historical('BTC', 'ETH')
print(df)

Note, I commented out the line that calls an external function that I do not have. Perhaps you have a global variable causing a problem?

Update as per the comments:

I'd use join instead to make the URL:

url = "".join(["https://min-api.cryptocompare.com/data/histoday?fsym=", str(symbol.upper()), "&tsym=", str(comparison_symbol.upper()), "&limit=", str(limit), "&aggregate=", str(aggregate), "&allData=", str(allData)])

Django tutorial: NameError: global name 'datetime' is not defined

Add this on top of file models.py:

import datetime

NameError: name 'get' is not defined (still issues)

"get" is a method from requests library. Make sure that you have imported get from requests or else use

response = requests.get('https://www.imdb.com/title/tt1439629/episodes?season=' + str(sn))


Related Topics



Leave a reply



Submit