ModuleNotFoundError when importing a module that is importing itself another module

File structure:

AppFolder/
  |
  |-- main.py
  |
  |-- Google
            |
            |-- client.py
            |
            |-- synchronization.py

In my main.pyI’m trying to import synchronization.py. In synchronization.py I’m importing client.py

I get the error ModuleNotFoundError: No module named 'client' with my files configured like this :

main.py

import Google.synchronization as googleCalendar

def main():
    googleCalendar.getEvents()


if __name__ == '__main__':
    main()

synchronization.py

import client

def main():
    """Connects the application with a google API"""
    global service
    service = client.main()

def getEvents():
    return service.events().list(calendarId = 'primary')

if __name__ == '__main__':
    main()

Someone else had a similar issue and I tried multiple things like from Google.synchronization import * or from . import synchronization but nothing changed.

The only thing that “resolved” the issue is putting the imports inside the if __name__ == '__main__': for main.pyand synchronizationlike this :

synchronization.py

if __name__ == '__main__':
    import client
    main()

But now, when I run main.py, I get the error : NameError: name 'service' is not defined. And I don’t know how to repair this new issue.

Is there another way to import my files that could alleviate both problems? Or another way to create my variable service?

Thanks in advance

Answer

Try

from .import client

def main():
    """Connects the application with a google API"""
    service = client.main()
    return service

def getEvents():
    return main().events().list(calendarId = 'primary')