How to import module of myself in mushclient

Posted by Lichleo on Mon 27 Jun 2022 12:47 PM — 8 posts, 21,166 views.

#0
Python can work in my environment. But when I write a module and use import xxx, the function defined the the module cannot be used. The variable defined in the module can be used, but function cannot be used.

Traceback (most recent call last):
File "<Script Block >", line 15, in <module>
xyjLib.test()
AttributeError: module 'mudLib.xyjLib' has no attribute 'test'
USA Global Moderator #1
Please show the code and not just the error message.
#2
Please help me! Thanks a lot!

The code of testLib.py:

strTest = 'str test'
world = object

def test():
      world.note("test lib func OK..")


The code of testscript.py:

import sys, os
import testLib

world.note("test python lib start..")
world.note(testLib.strTest)
testLib.test()
world.note("test python lib  OK..")
world.note(str(os.getcwd()))


I put in the folder:
D:\mud\mushclient506\scripts\testscript.py
D:\mud\mushclient506\testLib.py

Then I set script as testscript.py, and got the following error at line testLib.test()
Quote:

Traceback (most recent call last):
File "<Script Block >", line 6, in <module>
testLib.test()
File "D:\mud\mushclient506\testLib.py", line 6, in test
world.note("test lib func OK..")
AttributeError: type object 'object' has no attribute 'note'


And if I put both file in script folder, the module lib cannot be found
D:\mud\mushclient506\scripts\testscript.py
D:\mud\mushclient506\scripts\testLib.py

Quote:

Traceback (most recent call last):
File "<Script Block >", line 2, in <module>
import testLib
ModuleNotFoundError: No module named 'testLib'
USA Global Moderator #3
This
world = object

is very clearly causing this
AttributeError: type object 'object' has no attribute 'note'
.

Don't do that.
#4
In the script, 'world' can be directly used.
But in module, I should add 'world = object', or else it will give:
NameError: name 'world' is not defined

I made some change. Now it works.
D:\mud\mushclient506\pythonBot\testscript.py
D:\mud\mushclient506\pythonBot\testLib.py

In test script, I add testLib.world = world

import sys, os
from pythonBot import testLib

testLib.world = world
      
world.note("test python lib start..")
world.note(testLib.strTest)
testLib.test()
world.note("test python lib  OK..")
world.note(str(os.getcwd()))


testLib.py has no change:

strTest = 'str test'
world = object

def test():
      world.note("test lib func OK..")
USA Global Moderator #5
Quote:
But in module, I should add 'world = object'

Nope. Doing that is completely wrong.

Python's object has no MUSHclient methods in it. Assigning literally anything will stop Python from telling you that it's not defined, but it will not get you the note method that you want it to have, because that's not how things work.
If you need a variable to store specific functionality, willy nilly assigning some random unrelated thing won't get you there.

Quote:
In test script, I add testLib.world = world

Sure, because the world global variable is loaded in the outer script module and not inside the imported module until you pass it over, because in Python globals are only global within their own module and not across all modules.

You can also try adding this to at the top of your outer script before your other imports:

import builtins
builtins.world = world
Amended on Tue 28 Jun 2022 01:17 PM by Fiendish
#6
Thanks a lot for you help!
I also have another question.

testLib.py:

def test(s):
      world.note(s)

test("Run from module")


testScript.py:

import builtins
builtins.world = world

from pythonBot import testLib

testLib.test('Run from script')


Only 'Run from script' will be given.
test("Run from module") will be skipped because builtins is used.

How to let it not skipped?
Amended on Tue 28 Jun 2022 01:30 PM by Lichleo
USA Global Moderator #7
Sorry, at the top of your testLib.py module now you can add


import builtins
world = builtins.world


Of course you can also use
testLib.world = world
like before. Just make sure that it happens before anything inside testLib tries to access world.