Home > Mobile >  Where to include import statements referenced in module functions?
Where to include import statements referenced in module functions?

Time:01-09

I'm working on my first project and am trying to figure out how something works. If I have a module that store some functions I will reference in my main program that depend on another module, where do I include the import statement?

For example:

# Title: func.py

import os

def my_function(path):
    if not os.path.isfile(path):
        parser.error(f'The file {path} does not exist.')

Do I include import os here, or can I simply have it in the main document?

CodePudding user response:

You must import the module in the file that references the module. So if you have a file that somewhere calls os.path.isfile, you need to import os at the top of that file.

-- comment by larsks

  •  Tags:  
  • Related