简体   繁体   中英

Error when trying to import 'time'

ModuleNotFoundError: No module named 'time.sleep'; 'time' is not a package

This is the error I get when I type import time.sleep as sleep using 3.7.0a IDLE. Not sure about the as sleep part, but the import time.sleep seems to be broken or something like that. I tried the same thing with import time as well, got the same result. Could someone please explain?

Edit: I was told that I should try 'import time' first and then 'time.sleep', but as >I said before:

  I tried the same thing with 'import time' as well... 

that does not work either. And another suggestion was that maybe I had >another file named time.py, and that it confused the programme. But as far as I >know (from a full search through my computer), I do not have another time.py >file that might be the cause. Any other suggestions please?

You can do the following and it will work:

from time import sleep

The reason your import did not work is because time.sleep is not a module. sleep is a method (function). If you use import time and then time.sleep() it will work as well.

sleep is a method of time module, so first you need to import the module and then you can use methods of it, In your case:

>>> import time 
>>> time.sleep

or

>>> from time import sleep

should work, But as you said import time is also not working, So you need to make sure that there's no time.py file present in your directory (from where you're invoking the python shell)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM