Reputation: 3383
I am new to python. I am confused with importing in python and I am using two python files.
re1.py:
import datetime
import re2
re2.py:
print datetime.datetime.now()
When I run the re1.py file, it gave the error,
print datetime.datetime.now()
NameError: name 'datetime' is not defined
What is the best way to solve this error ?
Upvotes: 1
Views: 1212
Reputation: 18
your code should be:
re1.py:
import datetime
import re2
re2.py:
import datetime
print datetime.datetime.now()
import re2
doesn't means simply replace the statement with another file:
import datetime
# re2.py
import datetime
print datetime.datetime.now()
you have to make sure all the modules you import are working.
Upvotes: 0
Reputation: 59974
When you import datetime
in re1.py
, you import it in the scope of only the re1.py
file, and not in re2.py
. In other words, if you import something in one module, it won't cross over onto the other.
To fix this, you must import datetime
in re2.py
(and you don't necessarily need it in re1.py
)
Upvotes: 7