string: syntax error on raspbian jelly, flawless on IDLE windows 8 - string

My script (python 3.6) raises an error when executed on my RPI (Linux raspberrypi 4.9.59-v7+ #1047), but runs flawlessly when executed with IDLE, on a windows 8.1 PC.
Basically, I want to create a gzip archive from "data.json" file.
Here is a light version:
import time
import gzip
inputFile = "/path/to/data.json"
dataArchiveName = f"{inputFile}-{time.localtime().tm_mday}_{time.localtime().tm_mon}_{time.localtime().tm_year}.gz"
...
When I run the script, the following exception is raised:
File "dataManager2.py", line 96
dataArchiveName = f"{inputFile}-{time.localtime().tm_mday}_{time.localtime().tm_mon}_{time.localtime().tm_year}.gz"
^
SyntaxError: invalid syntax
I do not understand why... Any idea ?
Thank you in advance

f-strings were introduced in Python 3.6, since you are using Python 3.5 on you Raspberry Pi, you will have to either install Python 3.6 on it or use older way of formatting strings, this should work:
dataArchiveName = "%s-%s_%s_%s.gz" % (inputFile, time.localtime().tm_mday, time.localtime().tm_mon, time.localtime().tm_year)

Related

Speech Recognition. Module installed but won't work

Trying to get the speech recognition module to work. I have it working on my Windows 10 laptop, my Raspberry pi3 but I can't seem to get it to work on Ubuntu! The module has been install but neither PyCharm or Thonny can find it.
I've searched through stack overflow for a solution and haven't been successful. I've tried uninstalling and reinstalling the module. I've tried various versions of installing the module such as using the setuup.py file or pip, tried moving the folders/files where they should be etc..
(Also tried telling the Computer he's a good boy but that didn't work either...)
I can use this line of code
python3 -m speech_recognition
With my mic plugged into the tower and it works, but when using a line in PyCharm or Thonny such as
import speech_recognition
print(speech_recognition.__version__)
I end up with
Traceback (most recent call last):
File "/home/elitree/SPRECOG/FU.py", line 1, in <module>
import speech_recognition
ModuleNotFoundError: No module named 'speech_recognition'
python version is 3.6
pip3 version is 19.1.1
Ubuntu version is Ubuntu 18.04.2 LTS
I can provide other system details if needed
I'm just not sure what to do now
As ForceBru explained to me - My IDE was using the wrong python interpreter.
Which is first identified by running the Python script in the terminal with python3 this_file.py
After this the problem could be fixed by identifying the correct interpreter using the command which python3 in the terminal and then configuring the IDE to use that interpreter.

module 'psutil' has no attribute 'process_iter' in python 3.5

I am using python 3.5.2 on windows10 machine and having problems running a py script.
I am getting this error: 'psutil has no attribute process_iter' if either use psutil.process_iter() or psutil.process.get_list().
I have psutil 5.4.3 installed.
Downloaded and tried this file https://pypi.python.org/pypi/psutil.. no luck
Here is the code:
import psutil
def processcheck(seekitem):
for proc in psutil.process_iter():
#for proc in psutil.get_process_list():
if proc.name() == seekitem:
....
for process in machine_process:
processcheck(process)
Any idea how can you get around this?
It works in Linux though.
Thank you.
I had this problem when my script was named psutil.py
Just rename your file if you have the same issue.

Error with module Socket on Python3.6

First the context : I Want to send some data with a Bluetooth connection.
And to do that I want to use Python 3.6 because it's with this programming language that I coded the rest of the algorithm. I use it on a Raspbian Jessie lite for raspberry pi based on Debian Jessie.
The problem is that when I use this code :
import socket
hostMACAddress = 'xx:xx:xx:xx:xx:xx' # The MAC address of a Bluetooth adapter on the server
port = 3
backlog = 1
size = 1024
s=socket.socket(socket.AF_BLUETOOTH,socket.SOCK_STREAM,socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
try:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print("data")
client.send(data)
except:
print("Closing socket")
client.close()
s.close()
This error look like :
Traceback (most recent call last):
File "TestSocket.py", line 5, in <module>
s =socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'
I used it from a site which indicate that it's functionnal for Python 3.3 and above.
It's complicate because without this attribute I don't know how use socket module to establish bluetooth connection and send data correctly.
I am not an expert in Python so any help is welcome or with Debian distribution.
Thinx for you time and sorry for any syntax error or misspellings english is not my mother tongue.
Raspbian comes with two Python versions installed, 2.7 & 3.4.
Python 2.7 is the default so typing python script.py at the command prompt will run the script against Python 2.7
If you use python3 script.py you should find your script works
Also please note that the latest Python version available for Raspbian Jessie is 3.4

UNZIP error python 3.5

i am trying to update a script from 2.7 to 3.5 i keep getting an error with the following.
def _openScure(self, new_image):
return os.system("unzip "+new_image)
the issue is with unzip in python 3.5 what should i replace it with?
error says. 'unzip' is not recognized as an internal or external command.
i have tried the following.
import zipfile
with zipfile.ZipFile("name.zip", "r") as zip_ref:
zip_ref . extractall (".")
running win7 python 3.5 any ideas i would be most grateful.
So heres a link to the entire script i am trying to update to 3.5. The issue i am having is with line 17. Any thoughts?
https://github.com/agusmakmun/Some-Examples-of-Simple-Python-Script/blob/master/Stegano-Extract-and-Read-File-From-Image/script.py

Syntax error resulting from builtin functions in Python33.exe

I'm quite new to python and I just installed python 3.3.2 in my Win 8 64bit PC.
I tried typing in several builtin functions and I get a syntax error for all of them
For example:-
>>>print len("parrot")
File "<stdin>", line 1
print len("parrot")
^
SyntaxError: invalid syntax
I uninstalled/reinstalled python several times and added the path variable automatically during installation as well as manually after installation.
I suspect this might be something related to Win 8 but I'm not sure. Does anyone else have this issue with Python33 on Win8?
Solutions to get python going would be greatly appreciated.
Thanks!
print is now a function
print(len("parrot"))
should work

Resources