1

I am using python and cannot solve the problem myself. I defined the variables(rb. data) in the 'main file' below. And I imported 'hp file' to use hotplate function. ( i611Robot, Teachdata, motionparam are included in 'A file' )

from hp import *
from A import *

def main():
    rb = i611Robot()
    data = Teachdata()
    m1 = ....
    ....
    hotplate()

if __name__ == '__main__':
    main()

And this is the 'hp file' which is imported.

from A import *
def hotplate() :
    rb.motionparam( m1 )
    .......

But the problem is when I play 'main file', it says

File "main.py" line.., in <module>
    main()
File "main/py", line ...., in main
    hotplate()
File ".../hotplate.py", in line .., in hotplate
    rb.motionparam( m1 ) 
NameError : global name 'rb' is not defined

I dont know how to use 'rb' defined in 'main file' when using in the function of 'hp file'

이승연
  • 21
  • 1
  • 3
  • you haven't used `global` keyword, so why are you saying you use global variables? – Lei Yang Mar 08 '22 at 03:03
  • better send as argument - `hotplate(rb)` – furas Mar 08 '22 at 03:26
  • Does this answer your question? [Visibility of global variables in imported modules](https://stackoverflow.com/questions/15959534/visibility-of-global-variables-in-imported-modules) – CrazyChucky Mar 08 '22 at 03:27
  • 2
    you create `rb` inside `main()` - so it is local variable. You would have to add `global rb` inside `main()` to make it global. But preferred method is to send it as argument `hotplate(rb)` in `main()` and `def hotplate(rb):` in hp file – furas Mar 08 '22 at 03:28
  • It works. Thank you all. But there is another problem. There are a lot of parameter, variable, names like rb (used in imported function) Then, I have to to def hotplate(rb, m1, m2, m3......) like this?? The parameter,variables is too much to import like this one by one. – 이승연 Mar 08 '22 at 06:44

0 Answers0