3

I've asked several people, and can't find any questions that work. My code seems identical, but I'm trying to import a title for a game. While the function works in the source file, after importing to another, it isn't found. Here are the files and the resulting error in cmd:

Game.py:

from Events import * from Character import * Opening() 

Character.py:

from Events import * from Game import * 

Events.py:

from Game import * from Character import * def Opening(): print " _____ _ _____ _____ _ _ " print "/ ___| | | / ___| / __ \ | | (_) " print "\ `--. _ _| |__ ______\ `--. _ __ __ _ ___ ___ | / \/ __ _ ___ ___ _ __ | |__ ___ _ __ _ __ _ " print " `--. \ | | | '_ \______|`--. \ '_ \ / _` |/ __/ _ \ | | / _` |/ __/ _ \| '_ \| '_ \ / _ \| '__| |/ _` |" print "/\__/ / |_| | |_) | /\__/ / |_) | (_| | (_| __/ | \__/\ (_| | (_| (_) | |_) | | | | (_) | | | | (_| |" print "\____/ \__,_|_.__/ \____/| .__/ \__,_|\___\___| \____/\__,_|\___\___/| .__/|_| |_|\___/|_| |_|\__,_|" print " | | | | " print " |_| |_| " 

but after running the Game.py file in the cmd, it brings the error:

Traceback (most recent call last): File "Game.py", line 2, in <module> from Events import * File "/tmp/so/Events.py", line 2, in <module> from Game import * File "/tmp/so/Game.py", line 8, in <module> Opening() NameError: name 'Opening' is not defined 
2
  • 2
    Why does Events import Game? Also I'd suggest reading PEP-8. Commented Dec 19, 2016 at 20:38
  • 1
    Try just import Events Events.Opening() Commented Dec 19, 2016 at 20:39

1 Answer 1

7

Your problem is a combination of circular imports and using "from import *".

The best solution is to organize your code so that you do not have a need for circular imports. What is a circular import? You have Game importing Event which then imports Game. You can see this in the stacktrace (I edited your question to include it) as you look at the line that is executing at the time of the error.

The second part of your problem is the way that Python's import mechanism and "from import *" works. The first time around, Game.py is being executed. The first line encountered is from Events import *. So python looks through sys.modules and does not find the module Events. So it starts to load Events.py. Loading of Events.py will execute the statements in order. The first statement is from Game import *. Since Game is not in sys.modules, it will be loaded. So the first statement is from Events import *. If this seems confusing right now, then yes it is: don't have circular imports. This time Events is found in sys.modules. However, it is not fully initialized because it is still being loaded. So Game finds all of the names defined at this time in Events, which there aren't any. Then it continues on and tries to find an object named Opening in the current scope but can't find it. Arguably, python should crash as soon as it encounters a circular import, and tell you not to do that, but it doesn't. It might work, if you are extra careful, but it is a bad idea anyway.

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

1 Comment

Thanks! That fixed the problem completely. I'll remember to avoid circular imports!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.