The answer to the question "Can I do this on the import line?" is no.
See the definition of the import statement in Python 2. The statement imports things from modules. There is a
datetime class inside the
datetime module. The best you can do is
from datetime import datetime
You already understand well what this does, as you used it perfectly in your question. It looks like you wanted to do
from datetime import datetime.strptime
but that is a syntax error because
datetime.strptime is not an indentifier.
You can't say
from datetime.datetime import strptime
either because Python would look for a module named
The import statement just doesn't work the way you want it to.
Note that the author of the
datetime module chose to make
strptime a classmethod (using
@classmethod) rather than a function. So if you want to use
strptime without the class qualifier you will have to do what you did, namely assign to a variable named