Define method from external module (Python) - python

I'm trying to define a method (let's call it hello) in main.py's Things class, from define.py.
The code I currently have is raising AttributeError: module 'runme' has no attribute 'promptMe' (full traceback below).
Here's main.py's code:
import define
class Things:
def doWhatever():
print("whatever")
Here's define.py's code:
import main
def hello():
print("Hello!")
main.Things.hello = hello()
I've tried other solutions such as def main.Things.hello: hello() and def main.Things.hello: print("Hello!") but none work.
Here's the traceback when running define.py:
Traceback (most recent call last):
File "define.py", line 5, in <module>
import main
File "/path/to/main.py", line 9, in <module>
import define
File "/path/to/define.py", line 10, in <module>
main.Things.hello = hello
AttributeError: module 'main' has no attribute 'Things'
All help would be greatly appreciated. Thanks!

Related

pythonnet Exception: System.NullReferenceException: Object reference not set to an instance of an object

I'm trying to learn pythonnet module, but at the very beginning I face some problems.
I'm trying to execute a simple code of it:
import clr
clr.AddReference("System")
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
import System
import System.Drawing
import System.Windows.Forms
class FormLogin(System.Windows.Forms.Form):
def __init__(self):
pass
def main():
System.Windows.Forms.Application.Run(FormLogin())
if __name__ == "__main__":
main()
Unfortunately, this exception returns:
Traceback (most recent call last):
File "C:\Users\MyUser\Desktop\Test\APP.py", line 21, in <module>
main()
File "C:\Users\MyUser\Desktop\Test\APP.py", line 18, in main
System.Windows.Forms.Application.Run(FormLogin())
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.Form.get_IsMdiChild()
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
Does anyone know how to fix this?

Function not defined error while using import

I created two files in python
First one is
mym.py
def hello():
print("Hello everyone")
return
def summ(x,y):
total=x+y
return total
and next one is
abc.py
import mym
hello()
x=summ(3,4)
print(x)
And the error msg which I am getting is...both the files are in same working directory and there is no error of module not found...its giving error of function not defined.
Traceback (most recent call last):
File "C:/Users/Nisha/AppData/Local/Programs/Python/Python39/abc.py", line 3, in <module>
hello()
NameError: name 'hello' is not defined
Traceback (most recent call last):
File "C:/Users/Nisha/AppData/Local/Programs/Python/Python39/abc.py", line 3, in <module>
x=summ(3,4)
NameError: name 'summ' is not defined
What is the problem in function definition I am unable to trace...
The abc.py needs to be changed to:
from mym import *
hello()
x=summ(3,4)
print(x)
Otherwise you cannot access the functions.
You can try like this:
import mym
mym.hello()
x = mym.summ(3,4)
print(x)

After setting __import__ to None why I can still import?

If I do this:
__import__ = None
Then import a module:
import random
I still can. Why?
It doesn't call the reference in your module. It uses the one in builtins. Try:
import builtins
builtins.__import__ = None
import random
And you'll see it fail with:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Python multiprocessing: Share data from dynamically created module on windows

I'd like to send data defined in a module that was dynamically loaded with imp via a Manager().Queue() on Windows. Is this possible? Here is a minimal testcase to demonstrate what I mean:
import imp
import sys
from multiprocessing import Manager
if __name__ == '__main__':
s = """
def payload():
print("It works!")
"""
mod = imp.new_module('testcase')
exec s in mod.__dict__
sys.modules['testcase'] = mod
payload = mod.payload
payload() # It works!
m = Manager()
queue = m.Queue()
queue.put(payload) # AttributeError: 'module' object has no attribute 'payload'
Note: The use of imp.new_module + exec is just to get the testcase in a single file. The same AttributeError is raised when using imp.load_source.
Note2: This testcase leaves out all the Pool/worker code as the error happens before then.
Here is the output with full traceback from running the above script:
It works!
Traceback (most recent call last):
File "testcase.py", line 23, in <module>
queue.put(payload) # AttributeError: 'module' object has no attribute 'payload'
File "<string>", line 2, in put
File "c:\Users\Andrew\dev\python\lib\multiprocessing\managers.py", line 774, in _callmethod
raise convert_to_error(kind, result)
multiprocessing.managers.RemoteError:
---------------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\Andrew\dev\python\lib\multiprocessing\managers.py", line 240, in serve_client
request = recv()
AttributeError: 'module' object has no attribute 'payload'
---------------------------------------------------------------------------
Thanks!

ImportError: cannot import name (not a circular dependency)

I have the problem on importing the class in the same package, and it seems not a circular dependency problem. So I'm really confused now.
my-project/
lexer.py
exceptions.py
I declared an exception in exceptions.py and wants to use it in lexer.py:
exceptions.py:
class LexError(Exception):
def __init__(self, message, line):
self.message = message
self.line = line
and in lexer.py:
import re
import sys
from exceptions import LexError
...
It shouldn't be circular dependency since lexer.py is the only file has import in it.
Thanks!!
exceptions conflicts with builtin module exception.
>>> import exceptions
>>> exceptions.LexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'LexError'
>>> from exceptions import LexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name LexError
Use different module name.

Categories

Resources