Reza
Reza

Reputation: 113

getting invalid syntax error while running python script in bash

i have a python3.6.0 script which works perfectly well on windows. but when i run it in bash i get syntax error. here is the code:

import itertools

lines=["Hamlet","William Shakespeare","Edited Barbara B Mowat Paul Werstine","Michael Poston Rebecca Niles","Folger Shakespeare Library","httpwwwfolgerdigitaltextsorgchapter5playHam","Created Jul 31 2015 FDT version 092","Characters Play","line 17 POLONIUS father Ophelia Laertes councillor King Claudiusthis line substituted GHOST"]
LinesMap=dict()
for line in lines:
list=[l for l in line.split(' ')]
d = dict(itertools.zip_longest(*[iter(list)] * 2, fillvalue=None))
LinesMap = {**LinesMap, **d}

print(LinesMap)

this is the error:

[reza@localhost ~]$ python New\ Text\ Document.py 
File "New Text Document.py", line 16
LinesMap = {**LinesMap, **d}
             ^
SyntaxError: invalid syntax

Upvotes: 2

Views: 7315

Answers (2)

tripleee
tripleee

Reputation: 189908

On many Linux distros, python refers to Python 2, and you need to use python3 to run a Python 3 script.

Depending on your distro, you should be able to yum or apt-get a package with Python 3, which can coexist nicely alongside Python 2, partly thanks to the decision to use different names for the executables.

In more detail, apt-get install -y python3 (probably with sudo or ask your admin) should install Python 3 on Debian-based platforms (Mint, Ubuntu, what have you); and yum install python36 should install Python 3.6 on various RPM-based platforms (Fedora, CentOS, etc, again probably with sudo or something equivalent).

Upvotes: 3

ApprenticeHacker
ApprenticeHacker

Reputation: 22031

Try

python3 New\ Text\ Document.py 

Bash is using python 2 to run this.

Upvotes: 1

Related Questions