1

If we have multiple classes in a single Python file, is it possible to import modules or libraries independently in those classes?

I have found that unlike Java, Python has no style guide that suggests to separate classes in individual files. Even the Python modules have multiple classes; for example the calendar module has 10 classes in a single file (calendar module source code).

In my scenario, I have a Python file called planet.py. It has multiple classes. One of the classes requires to import pyttsx3 module. Then it needs to get a reference to the pyttsx3.Engine instance before using it. Currently I am importing the module and creating the instance in class __init__ method like below.

class Animal:
    pass

class Speaker:    
    def __init__(self):        
        import pyttsx3
        self.engine = pyttsx3.init()
    def speak(self, text):
        self.engine.say(text)
        self.engine.runAndWait()

Some other programmers suggest me not to import modules inside functions. They said it will execute the import statement every time the function is called.

I do not know how to do so. Apart from the main question, what is the best practice to follow in scenarios like this?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
arshovon
  • 13,270
  • 9
  • 51
  • 69
  • 1
    Python modules are not imported multiple times. The statement may be executed multiple times, but it'll shortcut once Python sees the module has already been loaded. That overhead is minimal. See eg https://stackoverflow.com/questions/2029523/how-to-prevent-a-module-from-being-imported-twice . –  Oct 27 '17 at 05:25
  • Any reason you don't separate this class into its own module, where you import `pyttsx3` at the top of that module? –  Oct 27 '17 at 05:26
  • The reason for having imports at the top of a module is 2-fold (afaik): 1/ people can immediately see the required modules, 2/ imports happen early, and failed imports thus show up early. An import inside a function may be called after the code has been running for, e.g., 2 hours (if the function is only called then), and if the import fails then, that's a bit harsh on the user. –  Oct 27 '17 at 05:28
  • Nice to learn something new on `import` statement. You are right about importing modules at top of the file. It is just a scenario. What I am eager to know is `If we have multiple classes in a single Python file, how to import other module/modules independently in those classes?` – arshovon Oct 27 '17 at 05:33
  • 1
    You don't: if you have multiple classes in a single file, they are probably related, and imports are likely to be relevant to all of them. –  Oct 27 '17 at 05:35
  • Yes, you are absolutely right. Let's see if any other option is available. – arshovon Oct 27 '17 at 05:38

1 Answers1

3

Import statements should usually be at the top of a python file (convention). According to convention, you should group classes with similar intentions and requirements together in one file. When you import a library, all classes in the file can use them. If a certain class does not need similar imports, or needs a lot of libraries imported which other classes do not need, it should, technically, be in a different file, and the file can be imported and instantiated.

Import statements in functions won't do any harm (apart from a very minimal delay time if run repeatedly), although, as @Evert said, can be a pain to the user experience if they fail.

Saransh Malik
  • 726
  • 1
  • 4
  • 16