How to Solve Python NameError: name ‘time’ is not defined

by | Programming, Python, Tips

This error occurs when you try to use the time module without importing it first. You can solve this error by importing the module using the import keyword. For example,

import time
print(time.gmtime(0))

This tutorial will go through how to solve the error with code examples.


NameError: name ‘time’ is not defined

Python raises the NameError when it cannot recognise a name in our program. In other words, the name we are trying to use is not defined in the local or global scope. A name can be related to a built-in function, module, or something we define in our programs, like a variable or a function.

The error typically arises when:

  • We misspell a name
  • We do not define a variable or function
  • We do not import a module

In this tutorial, the source of the error NameError: name ‘time‘ is not defined is usually due to not importing the module. Let’s look at an example.

Example

The Python time module provides various ways to represent time in code, including as objects, numbers, and strings. It also provides functionalities for timing code execution and suspending code execution. Let’s look at an example of using time to get the Unix epoch.

val = time.gmtime(0)
print(val)

Let’s run the code to get the result:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 val = time.gmtime(0)
      2 print(val)

NameError: name 'time' is not defined

The error occurred because we did not import the time module. Although time is a built-in module, we still need to import it.

Solution #1: Use import keyword

We can import the module by putting an import statement at the top of the program. Let’s look at the updated code:

import time

val = time.gmtime(0)
print(val)

Let’s run the code to get the Unix epoch:

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

The gmtime() method returns a struct_time which is an object with a named tuple interface. We can access the values by index and by attribute name. For example:

print(val[0])
print(val.tm_year)
1970
1970

Solution #2: Use from keyword

We can also use the from keyword to import a specific variable, class or function from a module. In this case, we want to import the gmtime class from the time module.

Using the from keyword means we do not have to specify the time module in the rest of the program, we only need the gmtime class.

Let’s look at the updated code:

from time import gmtime

val = gmtime(0)

print(val)

Let’s run the code to get the Unix epoch:

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

The from keyword is also useful for importing multiple classes, functions or variables from a module. Let’s look at an example of importing the time and ctime classes from the time module.

from time import time, ctime

t = time() 
ct = ctime(t)
print(ct)

In the above code, we use the time() method to get today’s time in seconds since the Unix epoch and then use ctime() to express the time as a datetime string. Let’s run the code to get the result:

Tue Jun 14 23:31:43 2022

Summary

Congratulations on reading to the end of this tutorial!

For further reading on NameErrors, go to the articles:

How to Solve Python NameError: name ‘os’ is not defined

How to Solve Python NameError: name ‘csv’ is not defined

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee