Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't import import datetime in script [duplicate]

I cannot import datetime from a python script, but I can from the terminal command line.

1)import datetime
2)From datetime import datetime

month = datetime.datetime.now().strftime("%B")
print month

These lines of code work when entered one by one into the command line Any ideas?

I'm running 2.7 on mac

like image 315
raw-bin hood Avatar asked Apr 12 '13 15:04

raw-bin hood


People also ask

How do I fix AttributeError type object datetime datetime has no attribute datetime?

datetime' has no attribute 'datetime'. To resolve the problem, we just have to “import datetime” instead of “from datetime import datetime” as we did it above.

Do you need to install datetime?

datetime is a built-in Python module. Developers don't need to install it separately.


1 Answers

You named your script datetime.py, located at /Users/ripple/Dropbox/Python/datetime.py. This is being imported instead of the standard library module, because the directory of the main script is the first place Python looks for imports.

You cannot give your scripts the same name as a module you are trying to import. Rename your script. Make sure you also delete the bytecode cache at /Users/ripple/Dropbox/Python/datetime.pyc.

like image 86
Martijn Pieters Avatar answered Oct 09 '22 12:10

Martijn Pieters