Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'importing': 0.05; '(python': 0.07; 'tries': 0.07; 'imported': 0.09; 'subject:modules': 0.09; 'try:': 0.09; 'useless': 0.09; 'subject:How': 0.10; 'question.': 0.14; 'caches': 0.16; 'emit': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'it;': 0.16; 'modules),': 0.16; 'modules.': 0.16; 'once.': 0.16; 'exception': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'module': 0.19; 'not,': 0.20; 'code,': 0.22; 'import': 0.22; 'separate': 0.22; 'print': 0.22; 'this?': 0.23; 'error': 0.23; 'url:moin': 0.24; 'fairly': 0.24; 'script': 0.25; 'this:': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'specifically': 0.29; '[1]': 0.29; 'am,': 0.29; 'points': 0.29; 'sets': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'that.': 0.31; 'url:wiki': 0.31; 'url:2008': 0.31; 'figure': 0.32; 'run': 0.32; 'quite': 0.32; 'url:python': 0.33; 'copying': 0.34; 'there,': 0.34; "can't": 0.35; 'except': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'there': 0.35; 'really': 0.36; 'functions.': 0.36; 'replies': 0.36; 'done': 0.36; 'url:org': 0.36; 'problems': 0.38; 'nov': 0.38; 'to:addr :python-list': 0.38; 'anything': 0.39; 'heard': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'catch': 0.60; 'expression': 0.60; 'solve': 0.60; 'simply': 0.61; "you're": 0.61; "you'll": 0.62; 'high': 0.63; 'soon': 0.63; 'more': 0.64; 'due': 0.66; 'url:co': 0.67; 'dont': 0.67; 'email,': 0.69; 'difference.': 0.84; 'pasting': 0.84; 'careful': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=S73M5CTEZOuVmBq+ddZ/TWMbht9MoonEDkTC7mIS+Yk=; b=l16ifWXJEXdNhrFi062WmcHkdAMnuy2UJQhaGSY5dpo/902Qnqs7PHjObNDkEY5yi6 mtAQQWdifqBusm/y2QWyo89keetkjoo02ZrPGrHWAbKi/EYWlbNpRisXQOsAQBm+rZom ItS1UcJGcfNNPiJEOg1jUGdat2dQxXOzFj7tuw4MnF0aAGxp05OVqyRxBYhE2lmAKMm6 MZXCir4THP9rRQMjWzeXkBt3cpzIpYRF+Ji8b5EwBeuG/CtgPPuCitd0QczbLdq2KBWf VYnFidY2b3H06mopIe+c4N3icdD2+5uPqDKNBFDTLShotTJbS0mXWZFvkf8p01WM+eGS aJzg== MIME-Version: 1.0 X-Received: by 10.68.110.196 with SMTP id ic4mr9697809pbb.84.1383430099206; Sat, 02 Nov 2013 15:08:19 -0700 (PDT) In-Reply-To: <7a0df271-0961-4f0e-8aac-bb9ecd1bf011@googlegroups.com> References: <7a0df271-0961-4f0e-8aac-bb9ecd1bf011@googlegroups.com> Date: Sun, 3 Nov 2013 09:08:19 +1100 Subject: Re: How to use variables across modules From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383430108 news.xs4all.nl 15877 [2001:888:2000:d::a6]:32944 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58352 On Sun, Nov 3, 2013 at 8:33 AM, wrote: > 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) > > > 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.. I don't know what GPIO is, but I suspect that you'll do best to simply import it in module1 same as you do in main. You'll get the same module (Python caches imported modules), so any configuration done will apply to both. But you might not need to separate out your modules. It's quite alright to have a fairly large main module in Python, with lots of functions. You'd have to figure that out separately, as a design question. A few other points about your code, if I may! 1) Is Print a custom function of yours? If not, please be more careful with copying and pasting code - it makes a lot of difference. 2) If RPi.GPIO can't be imported, you emit a message and (presumably) keep going. That's pretty useless - your script will bomb with a NameError as soon as it tries to use GPIO. Better to simply not catch anything around that import, and if there's a problem, the exception will be displayed (usefully!) and the script terminated. "Have you not heard the expression 'Never test for an error condition you don't know how to handle'?" [1] 3) You're using Google Groups, which means your post is misformatted - and which means that replies will be, unless you specifically work to prevent it, very much misformatted and ugly. Please don't do that. If you must use Google Groups, please read https://wiki.python.org/moin/GoogleGroupsPython and follow it; but due to the many problems with GG, there are a number of people who simply killfile every post from there, so they won't even see it. Switching, either to a better newsreader or to email, will solve all those problems at once. [1] http://www.theregister.co.uk/2008/10/24/bofh_2008_episode_34/ ChrisA