繁体   English   中英

在bash中运行python脚本时获取无效的语法错误

[英]getting invalid syntax error while running python script in bash

我有一个python3.6.0脚本,可以在Windows上很好地工作。 但是当我在bash中运行它时,出现语法错误。 这是代码:

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)

这是错误:

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

尝试

python3 New\ Text\ Document.py 

Bash正在使用python 2来运行它。

在许多Linux发行版中, python指的是Python 2,您需要使用python3来运行Python 3脚本。

根据您的发行版,您应该能够使用Python 3来实现yumapt-get的软件包,它可以与Python 2很好地共存,部分原因是决定对可执行文件使用不同的名称。

更详细地讲, apt-get install -y python3 (可能是sudo或询问您的管理员)应该在基于Debian的平台(Mint,Ubuntu,您拥有什么)上安装Python 3; yum install python36应该在各种基于RPM的平台(Fedora,CentOS等,同样可能是sudo或类似的东西)上安装Python 3.6。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM