Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Python
➜ How to import module of myself in mushclient
How to import module of myself in mushclient
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Lichleo
(6 posts) Bio
|
Date
| Mon 27 Jun 2022 12:47 PM (UTC) |
Message
| 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' | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #1 on Mon 27 Jun 2022 02:28 PM (UTC) |
Message
| Please show the code and not just the error message. |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Lichleo
(6 posts) Bio
|
Date
| Reply #2 on Tue 28 Jun 2022 10:35 AM (UTC) |
Message
| 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'
| Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #3 on Tue 28 Jun 2022 10:51 AM (UTC) |
Message
| This
is very clearly causing this
AttributeError: type object 'object' has no attribute 'note'
.
Don't do that. |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Lichleo
(6 posts) Bio
|
Date
| Reply #4 on Tue 28 Jun 2022 11:36 AM (UTC) |
Message
| 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..")
| Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #5 on Tue 28 Jun 2022 12:39 PM (UTC) Amended on Tue 28 Jun 2022 01:17 PM (UTC) by Fiendish
|
Message
|
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
|
https://github.com/fiendish/aardwolfclientpackage | Top |
|
Posted by
| Lichleo
(6 posts) Bio
|
Date
| Reply #6 on Tue 28 Jun 2022 01:20 PM (UTC) Amended on Tue 28 Jun 2022 01:30 PM (UTC) by Lichleo
|
Message
| 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? | Top |
|
Posted by
| Fiendish
USA (2,533 posts) Bio
Global Moderator |
Date
| Reply #7 on Tue 28 Jun 2022 01:51 PM (UTC) Amended on Tue 28 Jun 2022 01:54 PM (UTC) by Fiendish
|
Message
| Sorry, at the top of your testLib.py module now you can add
import builtins
world = builtins.world
Of course you can also use like before. Just make sure that it happens before anything inside testLib tries to access world. |
https://github.com/fiendish/aardwolfclientpackage | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
9,880 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top