-2

I am unable to understand , how are we able to directly call load_workbook from openpyxl package. I went through the https://openpyxl.readthedocs.io/en/stable/api/openpyxl.html to understand . But I am unable to do so .

We are able to call the load_workbook function like this. Can somebody help me understand this. We are neither importing the module from the package nor we are importing the function from the package.module nor we are directly importing the package.module inside which openpyxl is defined. Does the root openpyxl package itself has the function defined in it?

import openpyxl as xl
xl.load_workbook()

enter image description here

Kumar
  • 949
  • 1
  • 13
  • 23
  • I don’t understand what you mean. In your example there *is* an import, so why do you come to the conclusion that the function can be used without importing it? – mkrieger1 Apr 26 '20 at 08:58
  • "Does the root openpyxl package itself has the function defined in it?" - if the code you have shown works, apparently yes. Does this answer your question? – mkrieger1 Apr 26 '20 at 09:00

1 Answers1

1

If you would to inspect the __init__.py of the packege you will find - amongst others - the following lines:

from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook

Then, going to reader.excel.py you will find the definition of load_workbook:

def load_workbook(filename, read_only=False, keep_vba=KEEP_VBA,
                  data_only=False, guess_types=False, keep_links=True):
    """Open the given filename and return the workbook .....

This (I believe) is done by the authors to create a convenient interface for the users, as these are very common and basic functions. So instead of needing to do:

openpyxl.reader.excel.load_workbook()

You can just do:

openpyxl.load_workbook()

For more information about What is __init__.py for?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Great answer . I went through the __init__ but I might not have did a thorough deep dive. But a great reply – Kumar Apr 26 '20 at 09:57