How to use variables across modules

juel4700 at gmail.com juel4700 at gmail.com
Sat Nov 2 17:33:54 EDT 2013


Im a newbee at python, and im trying to figure out how to use variables and setups across modules.

Am I right when i think its smart to keep seperate functions of a program in seperate modules?

I have a main program module called main.py and in that main.py i have this:

    # Sets GPIO's 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)

I then import a module (in that module1 i have a function called Relay) and try to use the function with module1.Relay(1,1)

But the function in module1 needs the GPIO from the main.py to Work. How do I go about with this? I really dont want the GPIO setting part in the module1, I dont want it to be run everytime I run the module1.Relay(1,1) call..

What is best practice for working across modules. (im making a controller for my house' heat system, so it would be nice, if I can do this the right way, the first time.)

Im and experienced vbs and php coder, but a real newbe when it comes to python ;)

I Really hope you Guys will lead me in the right direction..

Kind regards Juel



More information about the Python-list mailing list