If you are a Python developer who has encountered the error message “NameError name ‘datetime’ is not defined”, you are not alone. In this tutorial, we will explore the possible causes of this error and provide step-by-step instructions on how to fix it.

We will cover common causes of the error and provide solutions to help you get your code up and running quickly. So, let’s get started!
Why does the NameError: name 'datetime' is not defined
error occur?
This error occurs when you try to use the datetime module in your Python code, but Python cannot find the datetime module in its namespace. The following are some of the scenarios in which this error usually occurs.
- You have not imported the datetime module.
- You have imported the datetime module using a different name.
How to fix the NameError: name 'datetime' is not defined
?
The datetime
module in Python is a built-in module that provides classes for working with dates and times. It allows you to create and manipulate dates, times, and time intervals, as well as format dates and times for display. The datetime
module includes several classes, such as date
, time
, datetime
, timedelta
, and tzinfo
, which can be used to perform various operations related to date and time.
Since this module is part of the Python Standard Library, you don’t need to separately install it. You can simply import it and start using it.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Let’s now look at the above scenarios that may result in the above error in detail.
The datetime
module is not imported
It can happen that you are trying to use the datetime
module without even importing it. This is because Python does not recognize the datetime
library and its functions until it is imported into the code.
For example, let’s try to use the datetime
module without importing it and see what we get.
# note that the array module is not imported # print the current date and time print(datetime.datetime.now())
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 4 1 # note that the array module is not imported 2 3 # print the current date and time ----> 4 print(datetime.datetime.now()) NameError: name 'datetime' is not defined
We get a NameError
stating that the name datetime
is not defined. To use the datetime
library, you need to import it first.
import datetime # print the current date and time print(datetime.datetime.now())
Output:
2023-04-19 15:04:45.453132
Here, we are importing the datetime
module first and then using it to get the current date and time. You can see that we did not get any errors here.
The datetime
module is imported using a different name
If you import the datetime module using a different name, for example import datetime as dt
, and then try to use the name “datetime” to use it, you will get a NameError
because the name “datetime” is not defined in your current namespace.
Let’s look at an example.
import datetime as dt # print the current date and time print(datetime.datetime.now())
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import datetime as dt 3 # print the current date and time ----> 4 print(datetime.datetime.now()) NameError: name 'datetime' is not defined
We get a NameError: name 'datetime' is not defined
. This is because we have imported the datetime
module with the name dt
but we’re trying to use it using the name datetime
.
To fix this error, you can either access datetime
using the name that you have used in the import statement or import datetime
without an alias.
import datetime as dt # print the current date and time print(dt.datetime.now())
Output:
2023-04-19 15:06:33.573697
In the above example, we are importing datetime
as dt
and then using dt
to access the datetime
module’s methods.
Alternatively, as seen in the example in the previous section, you can import datetime
without any aliases and simply use datetime
to avoid the NameError
.
Conclusion
In conclusion, encountering a NameError: name 'datetime' is not defined
error can be frustrating, but it is a common issue that can be easily fixed. By ensuring that the datetime
module is imported correctly and that the correct syntax is used when calling its functions, you can avoid this error and successfully execute your code.
You might also be interested in –