Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.05; 'context': 0.05; 'error:': 0.05; '__name__': 0.07; 'computed': 0.07; 'executed': 0.07; 'main()': 0.07; 'msg': 0.07; 'python': 0.09; '22,': 0.09; '__future__': 0.09; 'compute': 0.09; 'defined,': 0.09; 'defined.': 0.09; 'differently.': 0.09; 'msg)': 0.09; 'nameerror:': 0.09; 'output,': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'spelled': 0.09; 'def': 0.10; 'suggest': 0.11; 'dec': 0.15; 'sat,': 0.15; 'skip:f 30': 0.15; "'__main__':": 0.16; '2**20': 0.16; '36,': 0.16; 'alexander': 0.16; 'ans': 0.16; 'confusion': 0.16; 'doesnt': 0.16; 'first:': 0.16; 'fromaddr': 0.16; 'isnt': 0.16; 'main():': 0.16; 'partly': 0.16; 'pipe,': 0.16; 'smtplib': 0.16; 'subprocess': 0.16; 'temp': 0.16; 'toaddrs': 0.16; 'wrote:': 0.17; 'typing': 0.17; 'variables': 0.17; 'memory': 0.18; 'putting': 0.20; 'file.': 0.20; 'import': 0.21; 'error.': 0.21; 'noted': 0.22; 'received:emailsrvr.com': 0.22; 'sends': 0.22; 'defined': 0.22; "i'd": 0.22; 'work.': 0.23; 'command': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; '(most': 0.27; 'skip:# 10': 0.27; '(as': 0.27; 'disk': 0.27; 'in.': 0.27; 'is?': 0.27; "doesn't": 0.28; 'lines': 0.28; 'actual': 0.28; 'run': 0.28; 'post': 0.28; 'cpu': 0.29; 'credentials': 0.29; 'division': 0.29; 'url:mailman': 0.29; "skip:' 10": 0.30; 'checks': 0.30; 'received:(smtp server)': 0.30; 'error': 0.30; 'expect': 0.31; 'code': 0.31; '(and': 0.32; 'december': 0.32; 'url:python': 0.32; 'file': 0.32; 'running': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'defining': 0.33; 'ram': 0.33; 'traceback': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'server': 0.35; 'pm,': 0.35; 'there': 0.35; 'but': 0.36; 'wanted': 0.36; 'url:org': 0.36; 'totally': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'virtual': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'end': 0.40; 'url:mail': 0.40; 'your': 0.60; 'lost': 0.60; 'subject:, ': 0.61; 'procedure': 0.61; 'here': 0.65; 'want,': 0.65; 'email,': 0.66; 'soon': 0.70; 'communicate.': 0.84; 'cpu,': 0.84; 'returns.': 0.84; 'temperature': 0.84; 'terrible': 0.84; 'joel': 0.91 X-Virus-Scanned: OK Date: Sat, 22 Dec 2012 13:12:36 -0800 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python, email temperature References: <543ec063-14c1-4ca6-a911-ae6f58c22a8c@googlegroups.com> <4d1cf59e-7fb0-4b93-bfe8-db484edb5a45@googlegroups.com> In-Reply-To: <4d1cf59e-7fb0-4b93-bfe8-db484edb5a45@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 155 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356211303 news.xs4all.nl 6923 [2001:888:2000:d::a6]:45219 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35391 On 12/22/2012 12:54 PM, KarlE wrote: > On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote: >> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam wrote: >> >> Hi! >> >> >> >> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email. >> >> >> >> >> The two programs work very well on their own, but this doesnt work. >> >> >> >> this works: server.sendmail(fromaddr, toaddrs, msg) >> >> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature) >> >> >> >> despite the command "print cputemp" working in the same program. >> >> >> >> When i run the program i get the error: >> >> >> >> Traceback (most recent call last): >> >> File "sendcpu.py", line 36, in >> >> msg = cpu_temperature >> >> NameError: name 'cpu_temperature' is not defined >> >> >> >> Does anyone know why the program claims that cpu_temperature isnt defined, when it is? >> >> >> >> You should copy and paste the code here including the context around the error. You say print cputemp works, but cpu_temperature is not defined. They are spelled differently. Start there >> >> >> >> >> Thanx! >> >> >> >> //Alexander >> >> >> >> -- >> >> http://mail.python.org/mailman/listinfo/python-list >> >> >> >> >> -- >> Joel Goldstick > Hi! > > I made a typing error, and couldnt edit the post :( this is the code: > > > #!/usr/bin/env python > from __future__ import division > from subprocess import PIPE, Popen > import psutil > import smtplib > > def get_cpu_temperature(): > process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) > output, _error = process.communicate() > return float(output[output.index('=') + 1:output.rindex("'")]) > > > def main(): > cpu_temperature = get_cpu_temperature() > cpu_usage = psutil.cpu_percent() > > ram = psutil.phymem_usage() > ram_total = ram.total / 2**20 # MiB. > ram_used = ram.used / 2**20 > ram_free = ram.free / 2**20 > ram_percent_used = ram.percent > > disk = psutil.disk_usage('/') > disk_total = disk.total / 2**30 # GiB. > disk_used = disk.used / 2**30 > disk_free = disk.free / 2**30 > disk_percent_used = disk.percent > # > # Print top five processes in terms of virtual memory usage. > # > print 'CPU temperature is: ', cpu_temperature > > fromaddr = 'myemailadress' > toaddrs = 'myemailadress' > #msg = 'There was a terrible error that occured and I wanted you to know!' > msg = cpu_temperature > > # Credentials (if needed) > username = 'myusername' > password = 'mypassword' > > # The actual mail send > server = smtplib.SMTP('smtp.gmail.com:587') > server.starttls() > server.login(username,password) > server.sendmail(fromaddr, toaddrs, cpu_temperature) > server.quit() > > > > > if __name__ == '__main__': > main() > > running it gives the following error: > > pi@raspberrypi /home/python $ python sendcpu.py > Traceback (most recent call last): > File "sendcpu.py", line 36, in > msg = cpu_temperature > NameError: name 'cpu_temperature' is not defined > pi@raspberrypi /home/python $ > > > isnt cpu_temperature defined? > > First: Learn about Python SCOPES. You are defining variables inside (as local variables) the procedure main, but they are lost as soon as main returns. If you want values computed inside main but available outside main, you should return them. Second: Some confusion over what order things are executed in. The code in main is run when you call main -- ans that's at the very end of the file. The lines before the call to main expect to use the value cpu_temperature when you have not yet called main to compute the value (and which doesn't even return the value as noted above). The confusion is partly caused by having some of your code inside main and some of it outside main and expecting the two parts to communicate. I'd suggest putting everything up through the server.quit() into procedure main.