2017-02-14 68 views
0

我正在使用Python的bot在網站上創建帳戶。當我在安裝了python的Windows機器上使用它時,它工作正常。我昨天買了一臺Linux VPS(Ubuntu 16.04),我的腳本顯然不再工作了。這是我得到我的Linux機器上的錯誤:Python的新手問題,腳本在其他操作系統上正常工作

Traceback (most recent call last): 
    File "roblox.py", line 2, in <module> 
    from utils import * 
    File "/home/py/newbot/utils.py", line 18 
    key, *values = line.split(" ") 
     ^
SyntaxError: invalid syntax 

腳本行其指的是這樣的

def string_to_dict(headers): 
headers_dict = {} 
for line in headers.split("\n"): 
    if not line: continue 
    line = line.strip() 
    key, *values = line.split(" ") 
    key = key[:-1] 
    if not (key and values): continue 
    headers_dict[key] = " ".join(values) 
return headers_dict 

任何想法可能在哪裏呢?

+2

您使用的是不支持該語法的Python版本。如果你在Ubuntu 16上,嘗試調用'python3' –

+1

可能運行python2而不是3;在執行時指定版本(/ usr/bin/python3 /home/py/robox.py)或更新#! @ 最佳。 (#!/ usr/bin/python3) – wom

回答

0

正如在其他答案中提到的,你需要在Python3中運行它。最簡單的回答是:

  1. 安裝python3,這可能還不是你的機器上:$ sudo apt-get update; sudo apt-get install python3

  2. 得到你的腳本文件使用認領的習慣。將#!/usr/bin/env python3添加到腳本的第一行以指示shell調用python3而不是python2 - 大多數系統都將/ usr/bin/python別名爲python2安裝。

您可以在Bash shell(Ubuntu 16.04的默認shell)here中找到更多關於源文件的信息。

更長的答案 - 如果您想知道爲什麼代碼不能在Python2中工作,但在Python3中有效 - 是Python3引入了在PEP 3132中的擴展迭代解包,但是此功能並未在Python2中實現。

1

此語法在Python 2中無效。請使用python3運行腳本。

+0

Welp,謝謝!現在工作正常,你救了我的一天。 –

相關問題