How to fix SyntaxError: invalid syntax on a Raspberry Pi? - python

I have code wrote on Windows, compiles and works correctly, but fails on the raspberry pi
wrote some simple test code to see if it spits out the same error
a = input('this is a test string to split at space \n').split()
print(a)
typed 15:00 (need this data point for the bigger project) and i got this error
15:00
Traceback (most recent call last):
File "test.py", line 1, in <module>
a = input('this is a test string to split at : \n').split(':')
File "<string>", line 1
15:00
^
SyntaxError: invalid syntax
then tried 15 00 at the request of someone on discord, got this message
15 00
Traceback (most recent call last):
File "test.py", line 2, in <module>
a = input('this is a test string to split at : \n').split(':')
File "<string>", line 1
15 00
^
SyntaxError: unexpected EOF while parsing

You may be running different versions of python from windows to Raspberry Pi.
Thanks to this answer:
Note: this is only for Python 2. For Python 3, raw_input() has become
plain input() and the Python 2 input() has been removed.
Reference
a = raw_input('this is a test string to split at space \n').split(':')
print(a)

Related

How to get the precise position of an error within the line in Python

How to get the precise position of an error within the line in Python? The Python interpreter gives the line of the error and the type of the Error, but if there are more points in the line that could cause that error then there is ambiguity, here is a toy example:
example.py
xs = []
ys = {"item": xs}
zs= {"item":ys}
print(zs['item']['item']['item'])
Where the error is:
Traceback (most recent call last):
File "p.py", line 4, in <module>
print(zs['item']['item']['item'])
TypeError: list indices must be integers or slices, not str
Here, considering that xs, ys and zs could be the result of long computation, it could not be clear which one of the ['item'] triggered the TypeError.
I would prefer an error message like:
Traceback (most recent call last):
File "p.py", line 4, in <module>
print(zs['item']['item']['item'])
^-------
TypeError: list indices must be integers or slices, not str
That tells me that the problem is in the last accessing with ['item'].
I am using Python 3.8.16
This feature has been added in Python 3.11.1, in this new version the error message is:
Traceback (most recent call last):
File "/content/p.py", line 4, in <module>
print(zs['item']['item']['item'])
~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: list indices must be integers or slices, not str
Telling you that the error is caused by the third ['item'].
You can install it on a Linux system with:
apt install python3.11
And run a python file with the new Python version with:
python3.11 example.py

Same code differs in output across IDE's & pyshell

I was running this code on VScode.
age_1 = int(input("age : "))
print(age_1)
It shows this at it's terminal output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'print(age_1)
In Pycharm it shows this output after taking input as 12 :
age : 12
12
In PyShell it works like the Pycharm one. What did I do wrong in the VScode ?
Update :
When ran on a new file on VScode, it ran properly like the Pycharm one. But only for once . Then it shows up same error like before along with some new kinds of error everytime it ran.
Here's the correct one in VScode when ran the 1st time.
PS I:\CSE\LEARNING\PYTHON 3.0\practice> & "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
age : 12
12
Here's one error when ran another time.
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
File "<stdin>", line 1
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
^
SyntaxError: invalid syntax
Here's another error :
>>>print(age_1) age1 = int(input("age : "))
File "<stdin>", line 1
print(age_1) age1 = int(input("age : "))
^
SyntaxError: invalid syntax
& here's the final one .
>>> print(age1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age1' is not defined
P.S. I ain't skilled in coding. Explaining with some codes patiently will be appretiated.
The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. ... Then, you can use int() to convert your number to an integer. If this does not work, make sure that the value of a string does not contain any letters.I think you wrote a word that does not change int in the input. Maybe it's a space

python command line argument [duplicate]

This question already has an answer here:
How to get bash arguments with leading pound-sign (octothorpe)
(1 answer)
Closed 1 year ago.
I am trying to pass a command line argument like this because the actual string contains a # symbol before and after it
python3 test.py #fdfdf#
This is the error message I am getting
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(sys.argv[1])
IndexError: list index out of range
How can I pass a string which contains a # symbol in the beginning and the end as a command line argument?
Update:
OS: Ubuntu 20.04
You can try one of these and see which one works:
python3 test.py "#fdfdf#" or python3 test.py \#fdfdf\#

Apache Zeppelin: configuration of python interpreter on Windows

I'm having some troubles with running Zeppelin Python notebook on Windows. It looks like some output problem with several SyntaxError: invalid syntax errors. E.g. the code
%python
import numpy as np
print np.random.randn(10)
results in
^
SyntaxError: invalid syntax
[ 0.3144414 0.2340006 0.86525395 -0.24519029 -1.19862891 -0.88127259
-1.64757093 1.39753151 -0.50873422 -1.39649112]
File "<stdin>", line 1
z._displayhook()
^
SyntaxError: invalid syntax
File "<stdin>", line 1
^
SyntaxError: invalid syntax
File "<stdin>", line 1
Similarly when plotting with matplotlib the code is executed, but the actual figures are not shown (perhaps due to the output problems):
%python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
gives an error:
^
SyntaxError: invalid syntax
[<matplotlib.lines.Line2D object at 0x0000000004D737B8>]
File "<stdin>", line 1
z._displayhook()
^
SyntaxError: invalid syntax
File "<stdin>", line 1
^
SyntaxError: invalid syntax
File "<stdin>", line 1
ERROR
I'd appreciate any suggestions or help!
There is an easy work around. In the Interpreters management section
under the python interpreter set the following zeppelin.python property value to python -i
zeppelin.python python -i
(worked for me with v2.7.13)
It appears that Zeppelin doesn't currently support Windows's using python interpreter. You'll need to spin up a different envirornment using a VM.
https://issues.apache.org/jira/browse/ZEPPELIN-1436

python script for feeding input of another script line by line from some other file

Currently I am using bash script to handle input (from a file inputs.txt) to a python script with the command ./run.sh input.txt
{
while read line
do
echo $line;
done < $1
} | python main.py
The content of my file input.txt
string input 1
string input 2
string input 3
string input 4
string input 5
the end
I have to put the end at last because I get error
Traceback (most recent call last):
File "main.py", line 1, in <module>
print raw_input()
EOFError: EOF when reading a line
This is working fine but sometimes I work on windows. Can I write a python script to handle such scripts by inputting line by line. I cannot modify the code of the original script.

Categories