-4

here is the screenshot of the error i got in python3

The error says that the there is no module named as "gtts.gTTS", and got this error many times while importing other modules. So can you brief me what is the logic behind importing modules? Can't we import class using "." operator? What is the problem; I can't understand!

#what is the difference between these two codes
import gtts.gTTS
from gtts import gTTS
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pranjal sharma
  • 69
  • 1
  • 1
  • 6

1 Answers1

2

Unlike Java, where you do something like import module.submodule.blah.blah.MyClass, in Python, you can only directly import modules. If you want to only import a certain class, function, or other named value from a module, you need to use the from ... import ... syntax.

In all likelihood, gtts is a module, and gTTS is a class within that module. Therefore, import gtts.gTTS makes no sense, since gTTS isn't a module (that's what the error says), you must use from gtts import gTTS

For example, import os.path works fine, since path is a submodule of os, but if I wanted to use the exists function in path, I would need to use from os.path import exists or import os.path; os.path.exists(...). I get a ModuleNotFoundError if I erroneously try import os.path.exsts.

scnerd
  • 5,836
  • 2
  • 21
  • 36
  • Okay..i got your point but **how do i know whether i am importing class,function or any module without looking in the main directory of all the module** . Is there any method to check. _If we do "dir" then i will get all the attributes ,how will i know which is module or class or function_. – Pranjal sharma Jun 07 '18 at 05:47