module import "name is not defined" [solved]

Hello again, I have been experimentating on splitting my game code into several separated .py files, after a lot of research I managed to learn part of the process, but what happens now is the following:

import direct.directbase.DirectStart

from direct.gui.OnscreenImage import OnscreenImage
from pandac.PandaModules import PandaNode, LightNode, TextNode, NodePath, Camera
from pandac.PandaModules import Filename
from pandac.PandaModules import PointLight, AmbientLight
from pandac.PandaModules import LightRampAttrib, AuxBitplaneAttrib
from pandac.PandaModules import CardMaker
from pandac.PandaModules import Shader, Texture
from pandac.PandaModules import Point3,Vec2,Vec3,Vec4,BitMask32
from pandac.PandaModules import *

from direct.task import Task
from direct.actor import Actor
from direct.interval.IntervalGlobal import *
from direct.showbase.DirectObject import DirectObject
from direct.filter.CommonFilters import CommonFilters
from direct.gui.OnscreenText import OnscreenText 

import random, sys, os, math

...

from graphics import * 
...

MYDIR=os.path.abspath(sys.path[0])
MYDIR=Filename.fromOsSpecific(MYDIR).getFullpath() 

...		
#GraphicsManager() is a class from graphics.py
g = GraphicsManager()
...
run()

The above code is from the main file(main.py), in the same folder, the module graphics.py can be found, I have managed to acess this module with from graphics import*, but then I get when running the code the following:

NameError: global name 'Actor' is not defined

I find it strange, as I have already imported all necessary things in main.py, anyway I have even added from direct.actor import Actor again in graphics.py but to no use, any help is welcome. Below are parts of graphics.py

Class which manages Graphics
class GraphicsManager():
	def __init__(self):
		self.loadActor()
		
	def loadActor(self):						
		self.player = render.attachNewNode("player")         	
		self.playerActor = Actor.Actor(MYDIR+"/data/graphics/3D/player/player")
        	self.playerActor.reparentTo(self.player)
		self.playerActor.setY(self.player.getY()-0.5) 
		self.player.setPos( -1.4,0,-1.1 )
		self.playerActor.loop("still")

Hey, and thanks for all the help you all gave me before, you´re truly patient.

Each separate file (“module”) is seen by Python as a separate namespace so you cannot refer to variables from main.py in graphics.py. You are correct to add an import of Actor in your graphics.py which should fix that specific error.
I tested your code and you should be getting an error, but for “MYDIR” not Actor. If you want to use MYDIR in graphics.py you will need to either define it in the same way as in main.py or pass it as a variable when instancing the GraphicsManager class.

You might find it useful to create a file for values you want to refer to in many Python files. So you might have something like:
my_vars.py

MODEL_PATH = "assets/models/"
TEXTURE_PATH = "assets/textures/"

Now you can import this into any Python file and use the values:

import my_vars
model = loader.loadModel(my_vars.MODEL_PATH + "man")
texture = loader.loadTexture(my_vars.TEXTURE_PATH + "man.png")
model.setTexture(texture)

Or:

modelPath = getModelPath()
modelPath.appendDirectory("$MAIN_DIR/assets/models")
modelPath.appendDirectory("$MAIN_DIR/assets/textures")

...

model = loader.loadModel("man") 
texture = loader.loadTexture("man.png") 
model.setTexture(texture)

Hi,I found my problem was really the Actor, i was using gedit, the problem was I moved my .py files to a new folder.When I opened gedit, then opened a recently modified file(the list above exit) modified and saved the file, gedit would recreate the files in their original location, but in terminal I was already running the .py files in the new folder, so, my saves weren´t going to where they needed to.

To solve this I just did a gedit “save as” of each file in their correct place, then gedit understood where it should save the files.

But your tips were very usefull too, thanks for the help!