0

I execute the program via 'python main.py', inside main.py, I have a few global variables that I use throughout the program. Here is directory structure:

.
|-- display
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- view.py
|   `-- view.pyc
|-- engines
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- multithread.py
|   |-- multithread.pyc
|   |-- parse.py
|   `-- parse.pyc
|-- execute.py
|-- global.py
|-- __init__.py
|-- main.py
|-- master_device_list
`-- objects
    |-- arista.py
    |-- arista.pyc
    |-- baseinterface.py
    |-- baseinterface.pyc
    |-- baseplatform.py
    |-- baseplatform.pyc
    |-- brocade.py
    |-- brocade.pyc
    |-- cisco.py
    |-- cisco.pyc
    |-- citrix.py
    |-- citrix.pyc
    |-- initialize.py
    |-- initialize.pyc
    |-- __init__.py
    |-- __init__.pyc
    |-- juniper.py
    |-- juniper.pyc
    |-- ubuntu.py
    |-- ubuntu.pyc
    |-- unknown.py
    `-- unknown.pyc

When I execute, I get this error:

3 directories, 37 files
root@jumpbox:~/staging# 
root@jumpbox:~/staging# python main.py 


Traceback (most recent call last):
  File "main.py", line 77, in <module>
    main()
  File "main.py", line 44, in main
    parse_engine(database,check)
  File "/root/staging/engines/parse.py", line 17, in parse_engine
    ntw_device.append(device)
NameError: global name 'ntw_device' is not defined

ntw_device is a global variable in main.py. What import statement would I require to make this work?

  • What `import`s have you tried so far? Did you try `../main`? You are just going to tie yourself in knots trying to do this, particularly if those global variables are used extensively. I know its a pain to apply retrospectively, but it really is better to pass those variable as parameters to function calls and avoid using globals. Alternatively, hold the globals in a "well-known" module (or class) and access them using getter and setter functions. – cdarke Sep 02 '17 at 06:57
  • Possible duplicate of [Python - Visibility of global variables in imported modules](https://stackoverflow.com/questions/15959534/python-visibility-of-global-variables-in-imported-modules) – Rolf of Saxony Sep 02 '17 at 06:58

0 Answers0