Search code examples
pythonanaconda3

TypeError("'module' object is not callable") - Anaconda vs Vanilla Python


I'm relatively new to python but I'm stumped by a bug I've recently encountered. On Anaconda Python 3.9 (Running Python 3.9.7) the following snippit works well and does what's expected. However when I use any other version of Vanilla Python 3.9, I get the error message:

(<class 'TypeError'>, TypeError("'module' object is not callable"), <traceback object at 0x000002CBC415B140>) on every line with "copy" in it.

This is the code that causes issues:

def callwebCombine(self):
        dir_containing_files = self.folder_plaintext
        project = self.ProjNameInput_Plaintext
        dest_wb = Workbook()
        for root, dir, filenames in os.walk(dir_containing_files):
            for file in filenames:
                print("Combining")
                file_name = file.split('.')[0]
                # Absolute Path for Excel files
                file_path = os.path.abspath(os.path.join(root, file))
                # Create new sheet in destination Workbook
                dest_wb.create_sheet(file_name)
                dest_ws = dest_wb[file_name]
                # Read source data
                source_wb = load_workbook(file_path, read_only=False)
                source_sheet = source_wb.active
                row_count = 4
                for row in dest_ws.iter_rows(min_row=5, max_row=100, min_col=1, max_col=1):
                    for cell in row:
                        if cell.value is not None:
                            row_count += 1
                for row in source_sheet.rows:
                    for cell in row:
                        dest_ws[cell.coordinate] = cell.value
                        dest_ws.sheet_format = copy(source_sheet.sheet_format)
                        dest_ws.sheet_properties = copy(source_sheet.sheet_properties)
                        dest_ws.merged_cells = copy(source_sheet.merged_cells)
                        dest_ws.page_margins = copy(source_sheet.page_margins)
                        dest_ws.conditional_formatting = copy(source_sheet.conditional_formatting)
                        for rn in range(len(source_sheet.row_dimensions)):
                            dest_ws.row_dimensions[rn] = copy(source_sheet.row_dimensions[rn])

Oh also, I do have copy imported from copy. Changing it to just import copy, doesn't make a difference.


Solution

  • copy is a module. A module is not callable.

    e.g.

    import copy
    copy()  # error
    

    copy.copy() is a function of that module.

    To import the function, you need

    from copy import copy
    copy()  # works