Python: how to use variables between modules

I am trying to understand how to use variables and settings for modules.

Am I right when I consider it reasonable to keep certain program functions in separate modules?

I have a main program module called main.py , and in it I have this:

 # Sets GPIO to HIGH = Relays OFF try: import RPi.GPIO as GPIO except RuntimeError: Print("Error importing RPi.GPIO!!") GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) # GPIO16 is relay1 GPIO.setup(16, GPIO.OUT, initial=GPIO.HIGH) # GPIO11 is relay2 GPIO.setup(11, GPIO.OUT, initial=GPIO.HIGH) 

Then I import module (in this module I have a function called Relay ) and try using the function with module1.Relay(1,1)

But module1 requires a GPIO from main.py in Work. How do I deal with this? I really don’t need the GPIO setup part in module1 , I don’t want it to run every time I start the module1.Relay(1,1) call ..

What is best for working with modules? (I am making a controller for my home heating system.)

+6
source share
2 answers

You are right in that you support the separate functionality of your application in different modules - it is perfectly reasonable to group your code in logically related units. This is not always trivial, and it makes you think clearly about it, which depends on what the separation of problems is. What you met here is called circular import .

There are two obvious ways to resolve cyclic import operations:

  • Remove the item used by several modules and place it in a separate module. In your case, GPIO including its initialization.
  • Do not import the item that is used in each module, and then import it once and pass it as an argument to the other things that it needs.

Extract it to your own module

Extracting GPIO initialization into its own module might look like this:

main.py

 from gpio import GPIO import module1 print "main: %s" % GPIO.__name__ # Do stuff wth GPIO module1.Relay() 

gpio.py

 # Sets GPIO to HIGH = Relays OFF try: import RPi.GPIO as GPIO except RuntimeError: print("Error importing RPi.GPIO!!") print "Setting up GPIO" GPIO.state = 'initialized' print "Done setting up" 

module1.py

 from gpio import GPIO def Relay(): print "Relay: %s" % GPIO.__name__ # Do stuff with GPIO 

Output when starting main.py :

 Setting up GPIO Done setting up main: RPi.GPIO Relay: RPi.GPIO 

As you can see, GPIO initialization in the global area of ​​the gpio.py module gpio.py launched only once, the first time the module is imported anywhere.

Pass as an argument

Another option, importing the GPIO once and passing it as an argument, might look like this:

main.py

 # Sets GPIO to HIGH = Relays OFF try: import RPi.GPIO as GPIO except RuntimeError: print("Error importing RPi.GPIO!!") print "Setting up GPIO" GPIO.state = 'initialized' print "Done setting up" import module1 print "main: %s" % GPIO.__name__ # Do stuff wth GPIO module1.Relay(GPIO) 

module1.py

 def Relay(gpio): print "Relay: %s" % gpio.__name__ # Do stuff with GPIO 

Both are valid ways in your case, in my opinion. I personally would prefer initialization in the central gpio.py module and then import from there, but this is just a matter of taste.

+3
source

Theoretically, there should be no problem just importing the GPIO from another module. For example, an example:

gpio.py

 # Dummy GPIO module some_value = 5 def set_value(new_value): global some_value some_value = new_value def get_value(): return some_value 

module1.py

 import gpio def Relay(a, b): print "module1.Relay:", gpio.get_value() 

main.py

 import gpio gpio.set_value(1337) import module1 module1.Relay(1,1) # Prints the new value (1337) 
+1
source

Source: https://habr.com/ru/post/957262/


All Articles