10

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
raw-bin hood
  • 5,839
  • 6
  • 31
  • 45
  • 1
    What is the error you get? – Martijn Pieters Apr 12 '13 at 15:39
  • File "/Users/ripple/Dropbox/Python/datetime.py", line 2, in import datetime File "/Users/ripple/Dropbox/Python/datetime.py", line 4, in month = datetime.datetime.now().strftime("%B") – raw-bin hood Apr 12 '13 at 15:40
  • 1
    you named that test module datetime.py?! that cannot work, obviously. – ch3ka Apr 12 '13 at 15:41
  • 1
    @ch3ka It is not obvious at all why it can't work, unless you know the module system rather well and know one relatively obscure detail (that the current directory is searched). –  Apr 12 '13 at 15:41
  • well, if it wasn't obvious, this incident might be a great motivation to learn about the module system ;) – ch3ka Apr 12 '13 at 15:45

4 Answers4

23

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    I renamed it thescript.py I got the same error. Also, I got the error on another script called NHLWeb.py. I just used the name datetime.py to show the error from another code. – raw-bin hood Apr 12 '13 at 15:45
  • File "/Users/ripple/Dropbox/Python/thescript.py", line 1, in import datetime File "/Users/ripple/Dropbox/Python/datetime.py", line 4, in AttributeError: 'module' object has no attribute 'now' – raw-bin hood Apr 12 '13 at 15:46
  • @JosephLee: Look at that traceback. `/Users/ripple/Dropbox/Python/datetime.pyc` probably still exists, – Martijn Pieters Apr 12 '13 at 15:48
  • Also, the folder python does not hold python libraries, only the scripts that I write.... I don't know if that helps – raw-bin hood Apr 12 '13 at 15:49
  • @JosephLee: Doesn't matter; modules can be scripts and scripts can be modules. They are all python files. – Martijn Pieters Apr 12 '13 at 15:50
19

Here is what is happening:

  1. you named your file datetime.py for testing
  2. then you wrote import datetime inside it, because that's the module you want to try out

What happens when you run the file, is that:

  1. Python first looks into your local directory for a module called datetime
  2. Since Python only finds your original file, it makes an empty module file called datetime.pyc, because it expects you to build upon it

Luckily, the nice people at StackOverflow made you aware of this naming error, and you renamed your datetime.py file to something else. But confusingly you still get errors and the frustration slowly builds...

Solution:

  1. Always make sure that your filenames aren't equal to any Python module you want to import
  2. If you still forget, then make sure to delete or rename both the local .py script and the local .pyc file. This should fix your problem. :)

The reason this is such a common error, is that people like testing stuff when programming. And when they do, the natural inclination of most people is to make a script with the same name as the thing they want to test. However that's one of the first gotchas most Python developers run into. If the cool people that designed Python read Donald Norman before making their system, this would never be a problem. But as it is, we just have to adjust to how the Python module system works before naming our files. Note: There are still reasons for the Python module system working this way, however that doesn't preclude it from being confusing to beginners.

Kebman
  • 1,901
  • 1
  • 20
  • 32
1

Your second line is overwriting what python understands the word 'datetime' to mean in later code. You should either use

import datetime                 # the complete module
month = datetime.datetime.now().strftime("%B")

or

from datetime import datetime   # one part of the main module
month = datetime.now().strftime("%B")
ejrb
  • 338
  • 3
  • 9
1

while saving the scripts make sure you give it different name (other than date time) and save it in c:/Python34/Scripts; i' m sure this will work then.