0

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?

  • Simply `import time` should work fine. – ForceBru Dec 17 '17 at 14:37
  • As I said, it does the same when I use 'import time' it just says 'time' is not a package –  Dec 17 '17 at 14:38
  • 2
    Then there must be a name collision. Make sure there are no files called `time.py` in your working directory. – ForceBru Dec 17 '17 at 14:40
  • 1
    Possible duplicate of [Cannot import a python module that is definitely installed (mechanize)](https://stackoverflow.com/questions/14295680/cannot-import-a-python-module-that-is-definitely-installed-mechanize) – tripleee Dec 17 '17 at 15:03

2 Answers2

2

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.

renno
  • 2,659
  • 2
  • 27
  • 58
  • If `time` is not importable (probably because there is a file name which shadows the standard module, as hinted in other comments) no variation like this is going to work, either. – tripleee Dec 17 '17 at 15:00
  • If you try `import time as s` you will get the same error OP asked, saying that time is not a package. I assumed OP didn't try to do import time... he later said he tried to import time, but I had already answered this. – renno Dec 17 '17 at 15:02
0

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)

Talat Parwez
  • 129
  • 4