Python module can't be imported

The console gives me this error
“Python module can’t be imported - controller…”

but in my code i just have

import bge

And, as expected, every keyword of this module isn’t recognized…

The strange thing is that if i load another blend file, with many scripts that import bge, everything works fine…
So, where am i wrong?
In the working scripts i start with the same line…

(all the scripts are in the same directory, and all the .blends too…)

I’m guessing you’re running the script. Put the text file in a Python controller, and attach that to a sensor, like an Always sensor, for example. The ‘bge’ module is an internal module that is present when the game runs, so you can’t (and aren’t supposed to) run it like an external module.

Quite a noob problem the one you suggested :slight_smile:
No, it’s a module controller, connected to 5 keyboard sensors, that runs a module.py in a diroctory. In fact, if i just press p, and i don’t activate any of the sensors connected with the controller, the console doesn’t raise any error(because the module is not run).

The problem is here: the “structure” is equal to the one of the other blend, but this one doesn’t work…

Sometimes the import statement must be the first statement of the code, even before any docstrings.

I have that from time to time, but do not know why and when.

Just in case: You are running with 2.5x, are you?

Yeah, i’m in 2.5, one of the last built from graphicall

The “import bge” is the first line of the module, and “import” is the first statement…
How do you solve this problems?

Usually with trial and error.

i like to place a doc string in front of all my modules. Sometimes I get similar problems as you have, that Python says it can’t find something. Placing imports at the first line usually helps.

in your case I would try to import something else, like GameLogic, Rasterizer (yes they are still present ;)) , mathutils or os. You do not need to use it, but you can see if the import is possible or if this is a problem to bge.

Eidit: You could post a demo blend, so we could try as well.

Sure, here you are.
Put this script in /scripts/

import bge

def movement():
 g = bge.logic
 s = g.getCurrentScene()
 c = g.getCurrentController()
 o = c.owner
 
 down = o.sensors["up"]
 left = o.sensors["left"]
 right = o.sensors["right"]
 up = o.sensors["down"]
 select = o.sensors["Q"]
 
 if o["stato"]>0 and left.positive:
  #add movement of the selector
  #note: study how to use matrix rotation, and apply that to worldRotation and localRotation(maybe with matrix multiplication
  o["stato"]=o["stato"]-1
 elif o["stato"]<3 and right.positive:
  #add movement of the selector
  o["stato"]=o["stato"]+1
 
if select.positive:
 if o["stato"]==3:
  g.endGame()
 if o["stato"]==2:
  bge.NewSceneInvoker = s
  s.suspend()
  g.addScene("Options",1)
 if o["stato"]==1:
  g.addScene("Generale") #scena vuota dalla quale si carica tutto con LibLoad()
  #load game
  pass
 if o["stato"]==0:
  g.addScene("Generale")
  #start new game
  pass
  

def optionmovement():
 g = bge.logic
 s = g.getCurrentScene()
 c = g.getCurrentController()
 o = c.owner
 
 down = o.sensors["up"]
 left = o.sensors["left"]
 right = o.sensors["right"]
 up = o.sensors["down"]
 select = o.sensors["Q"]
 exit = o.sensors["W"]
 tick = s.objects["tick"]
 width = s.objects["width"]
 height = s.objects["height"]
 
 if not hasattr(bge,"screenresolution"):
  bge.screenresolution=[1,1366,768]
 
 
 if o["stato"]>0 and up.positive:
  o.wolrdPosition[1]+=0.500
  o["stato"]=o["stato"]-1
 elif o["stato"]<2 and down.positive:
  o.worldPosition[1]-=0.500
  o["stato"]=o["stato"]+1
 
 if exit.positive:
  bge.NewSceneInvoker.resume()
  s.end()
 
 
 if select.positive:
 
 
  if o["stato"]==2:
   #write the changes to the rungame file
   ###
   #add control for os, and do something similar for unix
   ###
   with open("Run Game.bat", mode="w", endoding="utf-8") as f_w:
    f_w.write("blenderplayer.exe ")
    if bge.screenresolution[0]==1:
     f_w.write("-f ")
    else:
     f_w.write("-w ")
    f_w.write(str(bge.screenresolution[1]))
    f_w.write(str(bge.screenresolution[2]))
    f_w.write("Game.blend")
   
   
   bge.NewSceneInvoker.resume()
   s.end()
   
   

  if o["stato"]==0:
   if bge.screenresolution[0]==1:
    bge.screenresolution[0]=0
    tick.setVisible(0)
   else:
    bge.screenresolution[0]=1
    tick.setVisible(1)






 if o["stato"]==1:
  resolutions=[[640,480],[800,600],[1024,600],[1024,768],[1280,720],[1280,768],[1360,768],[1366,768],[1440,900],[1600,900],[1600,1200],[1920,1080]]
  #set the resolution || in bge.screenresolution, alla quale si accede quando si scrive il nuovo file
  if left.positive:
   o["resolution"]+= 1
  elif right.positive:
   o["resolution"]-= 1
   
  if o["resolution"]>11:
   o["resolution"]=0
  elif o["resolution"]<0:
   o["resolution"]=11

  
  bge.screenresolution[1]=resolutions[o["stato"]][0]
  bge.screenresolution[2]=resolutions[o["stat"]][1]
  widht["Text"]=bge.screenresolution[1]
  height["Text"]=bge.screenresolution[2]

And put in that directory an init.py too. (i can’t upload rar,zip,7z files…)

I tryed loading other module, but i didn’t managed to… (tryed with GameLogic and os)

Attachments

Game.blend (693 KB)

I tried with 2.54 and it seems to be fine, except that the above code is a bit misaligned after cut&paste.
That means… it does not help you :(.

If you want to transfer python code within a blend file, you could use following method. You could have a copy of the file internal. The recipient could (but don’t have to) place it on disc.

Yeah, i used your method in another blend file :slight_smile:
Btw thanks, i’ll try downloading a new built

Isn’t it weird, that i had the same problem…lol
it worked fine in 2.54…but not in 2.55

I tried at home. Yes, 2.55 acts really weired it does not even find the module Start_Module within the folder. But another modeule “test”. But this is not executed.

2.54 is fine. Very mysterious.

Should i open a new bug in the traker?
Damn, it’s the second in a week, and till the last week i haven’t found any… Unlucky development for me, 2 more steps to stability for blender :slight_smile:

Yes, I think so. It looks like something is broken between 2.54 and 2.55

Solved!
There was an indentation mistake…

Can i ask another question?

How can i use rotations inside the bge? I know i must use matrices, but how can rotate along the z axes by X degrees?
Or how can i set the local rotation of a parented object to a determinate rotation?

I think it would be better to open a new thread for this.

You should open a new thread; if you do, I’ll help you out (if I see it).