More questions about python

Posted by Hairui on Sat 03 Jul 2004 06:25 AM — 13 posts, 45,566 views.

China #0
I wrote a AddAliases.py file which like this:


def AddSkAlias():
    world.AddAlias("skillsAlias","sk","skills",eEnbled,"")

if __name__!="__main__":
    world.note("Moudle AddAliases Imported")


Add I tried to import it in main script file like this:


import sys
sys.path.append("c:\fy4") # add the path of AddAliases.py
import AddAliases
AddSkAlias()


But the MUSHClient cried :
Quote:

Traceback (most recent call last):
File "<Script Block >", line 404, in ?
AddSkAlias()
NameError: name 'AddSkAlias' is not defined

Line in error:
AddSkAlias()


Can somebody help me to correct it?Thanks.
Amended on Sun 04 Jul 2004 07:51 PM by Hairui
Russia #1
You are importing AddAliases, so you need to call the functions contained in it through it's object:


import AddAliases
AddAliases.AddSkAlias()


If you want to make the AddSkAlias() function directly accessible in the importing module then you'll have to do one of the following imports:

import AddAliases.AddSkAlias as AddSkAlias
from AddAliases import AddSkAlias
from AddAliases import *


However, the best way to do it would be the second - from module import function. The first might work also, but the third should be avoided at all costs, especially if you are importing a large module.

Amended on Sat 03 Jul 2004 09:26 AM by Ked
China #2
I changed the script like this:
import AddAliases
AddAliases.AddSkAlias()

but i met with the error:
Quote:

Traceback (most recent call last):
File "<Script Block >", line 404, in ?
AddAliases.AddSkAlias()
AttributeError: 'module' object has no attribute 'AddSkAlias'

Line in error:
AddAliases.AddSkAlias()

And then i rechanged the scirpt to:

from Addaliases import AddSkAlias

This time the mushclient cried:
Quote:

Traceback (most recent call last):
File "<Script Block >", line 403, in ?
from Addaliases import AddSkAlias
ImportError: No module named Addaliases

Line in error:
from Addaliases import AddSkAlias

I am so puzzled now.:(
Amended on Sat 03 Jul 2004 01:27 PM by Hairui
Russia #3
The second attempt obviously failed because you mispelled the module name: Addaliases instead of AddAliases. But why the first one did is beyond me. Maybe the problem is with the function itself - invalid indentation for example? Post the AddAliases module using the [code][/code] here to preserve the formatting.
Amended on Sat 03 Jul 2004 12:48 PM by Ked
China #4
I corrected the second one to:
from AddAliases import AddSkAlias

and the result is:

Quote:
Traceback (most recent call last):
File "<Script Block >", line 403, in ?
from AddAliases import AddSkAlias
ImportError: cannot import name AddSkAlias

Line in error:
from AddAliases import AddSkAlias


It seemed this time i met with anthoer kind of error
Russia #5
The problem has to be with AddAliases module, but I can't say what it is without seeing it first. Something that happens to me sometimes is that I copy a module to Python/Lib to have easier access to it while testing, and then forget about it there and make a different copy of the module somewhere else. The import then fails because Python finds the old module in the /Lib directory before the new one wherever I put it.
Russia #6
Actually, I think I can try to guess what the problem is. That AddSkAlias function in the AddAliases option uses a Mushclient callback. The problem is probably that when the module is being imported, the interpreter looks for an object 'world' in that module's space and fails to find it (since 'world' is only added to the module immediately loaded by Mushclient and doesn't exist in modules loaded by those modules), so the function is skipped. Try rewritting that function like this:


def AddSkAlias(world):
    world.AddAlias("skillsAlias", "sk", "skills", "Enabled", "")


That will make 'world' an argument to the function, so the interpreter won't complain. When calling the function you'll need to do:

AddAliases.AddSkAlias(world)


instead of:

AddAliases.AddSkAlias()
China #7
I changed the moudle code to:
def AddSkAlias(world):
    world.AddAlias("skillsAlias", "sk", "skills", eEnabled, "")


the main script is :
import sys
sys.path.append("c:\fy4")
import AddAliases
AddAliases.AddSkAlias(world)

And this time the error is :

Quote:
Traceback (most recent call last):
File "<Script Block >", line 426, in ?
AddAliases.AddSkAlias(world)
File "C:\FY4\AddAliases.py", line 2, in AddSkAlias
world.AddAlias("skillsAlias", "sk", "skills", eEnabled, "")
File "<COMObject world>", line 2, in AddAlias
COM Error: (0x80020005)
Line in error:


It seemed the object world is a COMObject.But what is a COMObject?
Amended on Mon 05 Jul 2004 04:45 AM by Hairui
Russia #8
Hmmm, looking at your AddSkAlias function I am seeing the eEnabled flag used there. Are you sure that this flag is available for use in that module? Try substituting it with 1 to see if it works that way. That's the only explanation I can think of really, since it works for me just fine with world.Note().
China #9
I have tried to replace the eEnabled with 1
But the result is the same
China #10
I have found the ultimate reason why I had met with so many errors and been puzzled for these few days.
When I pressed the button of "Reload Script", the MushClient or its python script engine just reloaded the main script in script setting without reloading the script which are imported in the main script.Each time I edited the script to be imported and pressed the "Reload Scirpt", nothing changed to MUSHClient.
Thanks for your help,Ked
#11
Hate to be a thread necromancer, but...

This is my problem as well. I make changes in the imported file, and MUSHclient doesn't re-import it, only the main script. How do I get around that, or force a re-import? As it stands, I have to shutdown MUSH and restart it order for my changes in the imported class to show.

-Auren
#12
Ignore this, I found the thread in the Bug Reports section. These posts never happened *Jedi hand wave*.

-Auren