Craver2000
Craver2000

Reputation: 453

How to import variable from a primary script into a function of a secondary script, which is called by the primary script

I would like to import a variable from a primary script into a function of a secondary script, whoses function are called by the primary script. However, there seems to be some quirks when I run the script, as the execution of the secondary scripts doesnt seem to go smoothly.

My primary script script1.py has a while loop like this:

variable_of_interest=1    
while True:

        from script2 import primitives1,main
        main(primitives1)
        time.sleep(0.01)
        print "test print" 
        variable_of_interest+=1

It calls the function main and primitives 1 from a secondary script

My secondary script script2.py is:

device = ssd1351(serial)

def primitives1(device, draw):
    global variable_of_interest #I want the function of this sec script to from script1 import variable_of_interest from the primary script, so I added this
    from script1 import variable_of_interest  #I want the function of this sec script to import variable_of_interest from the primary script, so I added this
    # First define some constants to allow easy resizing of shapes.
    padding = 2
    shape_width = 20 
    top = padding
    bottom = device.height - padding - 1
    # Write two lines of text.
    size = draw.textsize('123456789101112131415')
    x = device.width - padding - size[0]
    draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
    draw.rectangle((x, top + 2, x + size[0], top + 16 + size[1]), fill="black")
    draw.text((device.width - padding - size[0], top + 44), 'My variable is: %s ' % variable_of_interest, fill="white") #Here is why the variable from the primary script is important
    time.sleep(0.01)


def main(prim_func):
    print("Testing basic canvas graphics...")
    for _ in range(2):
        with canvas(device) as draw:
            prim_func(device, draw)
            print("Drawing on display")
    time.sleep(0.01)

As mentioned, there seems to be a delay and some quirks (as if the script did not register this execution the first time) when I do this import. I feel that there shpuld be a better way of bringing the variable of the primary script into the secondary script, whose functions are being called by the primary script. Rather than using from script1 import variable_of_interest in the function of script 2 being called, is there a better way that the function being called, can register the variable of the primary script?

Upvotes: 0

Views: 45

Answers (1)

kdheepak
kdheepak

Reputation: 1366

I would use something like the following

.
├── constants.py
├── script1.py
└── script2.py

0 directories, 3 files

And use from constants import variable in both script1.py and script2.py.

Basically, I would avoid circular imports in Python. It can make life difficult in general, since it is hard to reason about the code. In Python specifically circular imports can either not work or cause problems if it does work. So, I wouldn't say it is a good idea.

Edit: Consider the following example.

.
├── package
│   ├── __init__.py
│   ├── script1.py
│   ├── script2.py
│   └── variable.py
└── script.py

1 directory, 5 files

where, the files are as follows.

package/__init__.py

# This is package/__init__.py, it is an empty file just to make the package folder a Python package.

package/script1.py

from .variable import variable_holder

def function1():
    variable_holder["variable_of_interest"] += 1

package/script2.py

from .variable import variable_holder

def function1():
    variable_holder["variable_of_interest"] += 10

And finally script.py

from package.script1 import function1
from package.script2 import function2
from package.variable import variable_holder

if __name__ == "__main__":

    print(variable_holder)
    function1()
    print(variable_holder)
    function2()
    print(variable_holder)

This is the output of running python script.py.

$ python script.py
{'variable_of_interest': 0}
{'variable_of_interest': 1}
{'variable_of_interest': 11}

This is how you can use a variable_of_interest as a global variable. I hope that makes sense. Let me know if you have more questions.

Upvotes: 1

Related Questions