Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ImportError: No module named datetime

Hi am getting an error while trying a simple datetime import.

ImportError: No module named datetime

I am trying the following code in the git console.

>>> from datetime.datetime import strptime

I have tried re-installing python and it doesn't seem to work. What am I doing wrong?

like image 781
Akash Deshpande Avatar asked Jan 25 '14 06:01

Akash Deshpande


1 Answers

datetime.datetime is a class datetime inside a module datetime. You can't import a method of a class, which is effectively what you are trying to do. Instead, you can:

from datetime import datetime
datetime.strptime(...)

or to "extract" the method the way you seem to want it:

strptime = datetime.strptime

though the name on the left side of the = is completely up to you.

The error message itself is coming from the second datetime in datetime.datetime, not the first.

like image 132
Ryan Haining Avatar answered Oct 30 '22 12:10

Ryan Haining