1

I have a project that suddenly stopped running correctly. I don't see why since I made no changes to it beforehand.

I get the following error when building it:

Traceback (most recent call last):
  File ".\engine.py", line 7, in <module>
    from controllers.game_panel_controller import GamePanelController
  File "D:\Documents - HDD\Code\Allies_RL_Prototype\controllers\game_panel_controller.py", line 3, in <module>
    import model.game as game
  File "D:\Documents - HDD\Code\Allies_RL_Prototype\model\game.py", line 1, in <module>
    from model.floor import Floor
  File "D:\Documents - HDD\Code\Allies_RL_Prototype\model\floor.py", line 5, in <module>
    from model.components.player_component import PlayerComponent
  File "D:\Documents - HDD\Code\Allies_RL_Prototype\model\components\player_component.py", line 1, in <module>

    import model.game as game
AttributeError: module 'model' has no attribute 'game'

Everything being read here is import statements and nothing else.

The folder structure here:

.
├──engine.py
├──controllers
|   └──game_panel_controller.py
└──model
    ├──game.py
    ├──floor.py
    └──components
        └──player_component.py

The weird part is that the problematic statement is

import model.game as game

when just a few statements above it, it runs that exact line successfully. It's not like it's an invalid module or a nonexistent file. What's wrong with it? Why would it suddenly stop running normally?

This is happening on windows 10, Python 3.5.4rc1, using windows powershell executing the program like so:

python .\engine.py
3
  • you do have init.py in model folder right? Commented Dec 17, 2018 at 4:16
  • I do not have an init.py file anywhere. This project has never needed one, I was under the assumption that it's no longer needed 3.5. Did something change so I need one now? Commented Dec 17, 2018 at 4:17
  • My bad, you are working with 3.5 so its not needed. Commented Dec 17, 2018 at 4:20

4 Answers 4

0

I believe the issue is that game_panel_controller.py is trying to import outside of it's immediate path. I think the scope of the PATH at that point includes the immediate directory and the library directories, but not the directory of the executing script.

So even though engine.py can import model.game, controllers.game_panel_controller cannot (because it's not's in the immediate directory of the game_panel_controller script).

You can do

from .. import model.game

Or you could use sym-links or a refactor of the code as well (or you could look into mangling the path prior to the import).

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

1 Comment

If this were the issue, I would expect the error to be ImportError: no module named model. Judging by the given error message, it is finding the model module, but not the game file within.
0

I don't know the exact reason, but I run into this issue before. You can try this, export PYTHONPATH=.

Comments

0

I think you've got a case of circular imports.

engine.py imports controllers.game_panel_controller, which imports model.game, which imports model.floor, which again imports model.game.

You said you haven't changed anything, so it shouldn't have worked before either. I can't explain it. You're sure nothing has changed?

Comments

0

I FIGURED IT OUT, but I only partially understand why this happens.

I realized I had changed the python version a month ago from 3.7.1 to 3.5.4rc1, which doesn't run it fine. Changing the windows environment variable back to point to the right folder fixed the issue, and it builds properly.

So I suppose doing what I was doing is not possible in 3.5.4rc1, but is possible in 3.7.1.

Now, specifically what change was made that makes it possible now when it wasn't possible before is still a mystery.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.