0

I have script1.py that is running/calling another script (script2.py).

script1.py

import sys
Wpath ='C:\PycharmProjects'
sys.path.insert(1, Wpath) # insert at 1, 0 is the script path (or '' in REPL)

import script2 as my_script
input_folder_name = 'TEST'
def run_Script():
    # todo

    if len(input_folder_name) != 0:
        my_script.SpecificFolders(outlook.Folders[user].Folders)
    else:
        my_script.AllFolders(outlook.Folders[user].Folders)

run_Script()

script2.py

if __name__ == "__main__":

    import win32com.client
    import os
    import glob
    import re
    import sys

    path = 'C:\PycharmProjects'
    os.chdir(path)

    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    user = outlook.Application.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress

    def SpecificFolders(folders):
        my_list = []
        for folder in folders:
            if (folder.name == input_folder_name) and (input_folder_name != ""):
                print(folder.name)
            my_list.append(folder.name)
            my_list += SpecificFolders(folder.Folders)
        return my_list

When I run script1.py I get the message:

AttributeError: module 'script2' has no attribute 'SpecificFolders'

Can someone tell me why script1 is not "seeing" the function (SpecificFolders) inside script2? And if possible, how to fix this error?

eduardo2111
  • 379
  • 3
  • 21
  • The function definition shouldn't be in the `if` – Barmar Nov 10 '21 at 17:47
  • Nothing that you want to be exported should be in `if __name__ == '__main__':` – Barmar Nov 10 '21 at 17:47
  • It's because the script2 file isn't the entrypoint, so the __name__ isn't equal to __main__. because of that, the function is not being declared, and therefore is not being exported. – 2pichar Nov 10 '21 at 17:52
  • @Barmar but if I remove the `if __name__ == '__main__':` I get the same error – eduardo2111 Nov 10 '21 at 17:59
  • You must have done something else wrong, because that should solve this problem. – Barmar Nov 10 '21 at 18:01
  • @Barmar I reset python (cleared memory cache), then I removed that `if __name__ == '__main__':`, and I get the error : name 'outlook' is not defined. Although I have it defined in the script2 – eduardo2111 Nov 10 '21 at 18:07
  • It should be `my_script.outlook` – Barmar Nov 10 '21 at 18:08
  • @Barmar Now that I removed `if __name__ == '__main__':`, when I `import script2 as my_script` it runs the script2, and this causes an error because I haven't given the inputs yet to script 2. Can you help to figure out a way, so that I can import script2 and only runs when called, not while importing it ? – eduardo2111 Nov 10 '21 at 18:17
  • I remember now that I added `if __name__ == "__main__":` to prevent import from running the script – eduardo2111 Nov 10 '21 at 18:18
  • 1
    What inputs? The parts of script2 that should be run as a standalone script should be in the `if`. The parts that should be exported should not be in the if. Don't just remove the if, organize it properly. – Barmar Nov 10 '21 at 18:20
  • @Barmar I understand that the code in indent below `if __name__ == "__main__":` won't run if imported. I organized the code in multiple ways, but I get an error everytime. I also read multiple articles about `if __name__ == "__main__":`, but I can't pass the errors. Can you help me, please? – eduardo2111 Nov 10 '21 at 19:05
  • You need to unindent the top-level code in the script when you take it out of the `if`. – Barmar Nov 10 '21 at 19:45

0 Answers0