0

Python won't allow me to import classes into eachother. I know there is no "package" solution in Python, so I'm not sure how this could be done. Take a look at my files' codes:

file Main.py:

from Tile import tile
tile.assign()
class main:
    x = 0
    @staticmethod
    def assign():
        tile.x = 20

file Tile.py:

from Main import main
main.assign()
class tile:
    x = 0
    @staticmethod
    def assign():
        main.x = 20

I get the error "class tile can not be imported".

1
  • 1
    your naming convetion is backwards, use Tile for the class and tile.py for the file, otherwise it is confusing. Commented Jan 29, 2013 at 20:37

4 Answers 4

3

If file A imports file B, and file B imports file A, they would keep importing each other indefinitely until the program crashed.

You need to rethink your logic in a way that doesn't require this circular dependency. For example, a 3rd file could import both files and perform both assignments.

Sign up to request clarification or add additional context in comments.

7 Comments

although I'm with you about avoiding circular imports he could do it using import Tile instead of from Tile import tile, Python won't import the same module twice if it doesn't have to
No, but it will be unable to compile certain code depending on the order in which it performs the imports.
But if all use of the imported modules is within functions/methods, as it would be in this case, then circular imports of modules can work fine.
Scratch that, wouldn't work, because of the module scope calls to main.assign and tile.assign.
So there isn't any way to mimic Java's package solution in Python? By working with variables and methods that are within different files.
|
1

You have your import backwards and the from needs to have the name of the module

from Tile import tile

6 Comments

I'm sure you mean from Tile import tile
My files have it that way, I just wrote the code here wrong by accident. Regardless I still get the same error.
@azonicrider Can you post the full traceback then?
Excuse my noobness, but I'm not sure what traceback is. Is it the full error message?
so when you run this in the interpreter it will error and print stuff to your terminal. copy all those error messages (or traceback) and past it in with your question. It's a good idea to do this with all your questions.
|
1

Python begins executing Main.py. It sees the import, and so goes to execute Tile.py. Note that it has not yet executed the class statement!

So Python begins executing Tile.py. It sees an import from Main, and it already has that module in memory, so it doesn't re-execute the code in Main.py (even worse things would go wrong if it did). It tries to pull out a variable main from the Main module, but the class statement binding main hasn't executed yet (we're still in the process of executing the import statement, advice that line). So you get the error about there not being a clsss main in module Main (or Tile, if you started from there).

You could avoid that by importing the modules rather than importing classes out of the modules, and using qualified names, but then you'd fall one line down when Main.main doesn't work. Your code makes no sense I'm a dynamic language; you can't have both the definition of class main wait until after tile.assign has been called and the definition of class tile wait until after main.assign has been called.

If you really need this circular dependency (it's often, but not always a sign that something has gone wrong at the design stage), then you need to separate out "scaffolding" like defining classes and functions and variables from "execution", where you actually call classes and functions or use variables. Then your circular imports of the "scaffolding" will work even though none of the modules will be properly initialized while the importing is going on, and by the time you get to starting the "execution" everything will work.

Comments

-1

All You have to do is add

    def __init__(self) -> None:
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.