CPaolo
Posts: 9
Joined: Tue Apr 18, 2017 5:51 pm

Syntax Error in Script

Tue Apr 18, 2017 6:02 pm

Hello all, i'm new to this wonderful world and i own a RPI2 B with the DHT11 sensor. I'm using a script in python for uploading the Temperature online.
From yesterday after the reboot (after apt-get update and upgrade) i get a syntax error in this line:

print "Temp={0:f}*C Humidity={1:f}%".format(temperature, humidity)

between " and .

But i did not change the script. What should be happen ?
Can someone help me ?

Thankyou all
CPaolo

gordon77
Posts: 4988
Joined: Sun Aug 05, 2012 3:12 pm

Re: Syntax Error in Script

Tue Apr 18, 2017 6:23 pm

Can you upload all your code to here, or provide a link to where you got it?

User avatar
DougieLawson
Posts: 38854
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Syntax Error in Script

Tue Apr 18, 2017 6:40 pm

Is it python2 or python3 code? Are you trying to run python2 code using python3?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

CPaolo
Posts: 9
Joined: Tue Apr 18, 2017 5:51 pm

Re: Syntax Error in Script

Tue Apr 18, 2017 7:03 pm

This is the code........
I don't know if the python version is changed ......


#!#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

############################################################
# This code uses the Beebotte API, you must have an account.
# You can register here: http://beebotte.com/register
############################################################

import time
import Adafruit_DHT
from beebotte import *

### Replace API_KEY and SECRET_KEY with those of your account
bbt = BBT("00000000000000000000000", "00000000000000000000000000000000000000")

period = 300 ## Sensor data reporting period (1 minute)
pin = 4 ## Assuming the DHT11 sensor is connected to GPIO pin number 4

### Change channel name and resource names as suits you
temp_resource = Resource(bbt, 'PI2B', 'temperatura')
humid_resource = Resource(bbt, 'PI2B', 'umidita')
now = time.strftime("%c")

def run():
while True:
### Assume
humidity, temperature = Adafruit_DHT.read_retry( Adafruit_DHT.DHT11, pin )
if humidity is not None and temperature is not None:
print "Temp={0:f}*C Humidity={1:f}%".format(temperature, humidity)
print time.strftime("%c")
try:
#Send temperature to Beebotte
temp_resource.write(temperature)
#Send humidity to Beebotte
humid_resource.write(humidity)
except Exception:
## Process exception here
print "Error while writing to Beebotte"
else:
print "Failed to get reading. Try again!"

#Sleep some time
time.sleep( period )

run()





Thanks
CPaolo

User avatar
DougieLawson
Posts: 38854
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Syntax Error in Script

Tue Apr 18, 2017 7:05 pm

That's a python2 program.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

CPaolo
Posts: 9
Joined: Tue Apr 18, 2017 5:51 pm

Re: Syntax Error in Script

Tue Apr 18, 2017 7:07 pm

Hello, Python version is still 2.7

CPaolo

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Syntax Error in Script

Tue Apr 18, 2017 9:41 pm

CPaolo wrote:Hello, Python version is still 2.7
When you type python at the terminal, what do you get, 2.7.x? On arch, I get 3.6.0 On my pi3, I do get 2.7.9. If your program needs 2.7, trusting that just looking in the path for "python" is version 2.7 might be risky. Perhaps a shebang that asks for a specific at least major version of python would be better.

CPaolo
Posts: 9
Joined: Tue Apr 18, 2017 5:51 pm

Re: Syntax Error in Script

Wed Apr 19, 2017 5:32 pm

yes, 2.7

If i restore a backup SD the script works .......

CPaolo

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Syntax Error in Script

Wed Apr 19, 2017 7:18 pm

CPaolo wrote:yes, 2.7
Did you change the shebang in your program to python 2.7

#!/usr/bin/python2.7

after verifying that it is there by running at the command line:

/usr/bin/python2.7

The error that you get seems to occur for me in the interpreter when I copy your line of code in and change the variables to sensible integer constants when I'm in arch running python 3.6.0 and it does not occur in raspbian under 2.7.9 .

CPaolo
Posts: 9
Joined: Tue Apr 18, 2017 5:51 pm

Re: Syntax Error in Script

Fri Apr 21, 2017 7:16 pm

I don't understand ......

Return to “Beginners”