qchar90
qchar90

Reputation: 474

global name 'test' is not defined

I have a two files .py File A and B,

File A using methods from file B and file B using methods from file A

file A

from file_b import *

def abc():
# something
   cba()

file B

from file_a import *

def cba():
# something
   abc()

if i trying run script for file A, i get Error

global name 'cba' is not defined

If i change my imports to :

import file_a

and

file_a.abc()

My script works properly

It is possibility to use from file_a import * ?

Did I do something wrong?

Upvotes: 0

Views: 93

Answers (1)

srattigan
srattigan

Reputation: 664

I have 3 files for a Python PyGame.

  1. settings.py
  2. sprites.py
  3. game.py

In settings I have my global variables and some other useful constants. If I import my settings into my sprites file using

from settings import *

then in my main file, game.py, I just import my sprites. If I use

from sprites import *

then I am setting the sprites AND the content of my settings file ALSO. If I were to say

from sprites import player
from sprites import enemy

then I would NOT be getting the content of settings, even though they are imported into that NameSpace... or file. If I want access to the tuples representing colours in my game.py file, I have to import them.

I hope this clears up the problem you are having or gives a better idea of why this is happening, as mentioned in the first comment- it is a circular reference.

Upvotes: 1

Related Questions