0

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

Sanico
  • 39
  • 7

2 Answers2

1

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')
Sravan JS
  • 161
  • 8
  • the ```service```variable is a Resource built by a google method to interract with the API so I don't really know how to declare the variable before. I tried in the `__name__ == '__main__'` to create the variable but I'm still getting the ```NameError``` – Sanico Sep 18 '21 at 16:45
  • I have updated the code. Can you try that – Sravan JS Sep 18 '21 at 16:57
  • Thanks it's working! Do you have any idea why the `service` variable was not working? – Sanico Sep 18 '21 at 17:53
0

Try importing this way:

from .client import main as client_main
saedx1
  • 984
  • 6
  • 20
  • In ```synchronization.py``` I'm getting ```ImportError: attempted relative import with no known parent package```. I think it's because client and synchronization are in the same folder. So I tried ``from client import main as client_main``` but it doesn't change anything when I run ```main.py``` – Sanico Sep 18 '21 at 16:37