0

I want the main.py to have access to file.py and vise versa

>main.py
>folder
   |-->file.py

for example, I have 2 classes located in two files:

in main.py

class MyClass():
   #somecode

in file.py

class Tools1():
   #somecode

I have tried to use import at the start of each file but then I got an ImportError

main.py

from folder.file import Tools1

file.py

from main import MyClass

then I got this error when I run the main.py

ImportError: cannot import name 'Tools1' from partially initialized module 'folder.file' (most likely due to a circular import)

Is what I am doing posible in python? If so what I am doing wrong?

1 Answers1

0

you should not import main from folder.file, because of recursion problem:

  1. main will try to import folder.file.Tools1, which is a part of foler.file
  2. folder.file has an import of main.MyClass at the top, which is a part of main
  3. then, folder.file will try to import main
  4. main has an import of folder.file.Tools1 at the top

Then it follows to step 1, and it will roll around recursively.

Better to write code without complex collisions, just import your Tools1 from main and operate with them right in main. If you want import both of them, you can add create one more file and import both of them in that file.

Mikhail
  • 1
  • 1