How To Fix NameError: Name ‘datetime’ Is Not Defined In Python?

How To Fix NameError: Name ‘datetime’ Is Not Defined In Python?

You are working with Python and suddenly get the NameError: name ‘datetime’ is not defined in Python. It took you an hour to find a way to fix it. Rest assured, this article has the solution for you.

Before going into the main content, let’s learn what datetime is in Python.

What is datetime in Python?

Datetime is a library for working with date and time in Python. This is one of the most used libraries in Python.

Commonly used modules in datetime are date object, time object, datetime object, and timedelta object.

When does the NameError: name ‘datetime’ is not defined in Python happen?

This error is very common in Python. This error appears when we have not imported the library containing the functions we use in the program but try to use them. Like this:

# Have not imported but try to use datetime
today = datetime.date.today()
print(today)

# Have not imported but try to use datetime
timeToLearn = datetime.time(20,00,00)
print(timeToLearn)

Output:

NameError: name 'datetime' is not defined

In addition, this error can also come from missing spelling or using an undefined function. Like this:

def get_datetime(year, month, day):
  return date(year, month, day)

# Confuse datetime with get_datetime
newYear = datetime(2022, 1, 1)
print(newYear)

Output:

NameError: name 'datetime' is not defined

How to fix this error?

We will guide you to fix the error in the first case to help you get an overview of this error, and you can fix the other cases by yourself.

Import datetime before using it

Although datetime is a widely used library in Python, we still have to import it before using it. Put the command line: import datetime at the beginning of the file to import it. Like this:

import datetime

newYear = datetime.date(2022, 1, 1)
print(newYear)

newDay = datetime.time(0, 0)
print(newDay)

Output:

2022-01-01
00:00:00

From now on, you can use datetime without worrying about NameError: name ‘datetime’ is not defined in Python.

Efficiently import the datetime library

If we use: import datetime, Python will automatically import the whole datetime library, including the timedelta object, date object, time object, tzinfo object, and timezone object. If we only need to use date and time, we should only import what we need.

So, we use the command line: from datetime import date, time to import only the date object and time object. Like this:

from datetime import date, time

# Use date
today = date.today()
print(today)

# Use time
timeToLearn = time(20,00,00)
print(timeToLearn)

Output:

2022-10-13
20:00:00

Advanced import with as

If you want the time and date objects to have a shorter name, such as t or d. Use as when import. Like this:

from datetime import date as d, time as t

# Use d
today = d.today()
print(today)

# Use t
timeToLearn = t(20,00,00)
print(timeToLearn)

Output:

2022-10-13
20:00:00

Summary

We have just shown you how to fix the NameError: name ‘datetime’ is not defined in Python. Moreover, this article help you have more information about how to import a module in Python efficiently. If you find the knowledge in this article useful, don’t forget to share it with your colleagues.

Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *