簡體   English   中英

Python程序將在Spyder終端中運行,但不能在常規的Linux終端中運行

[英]Python program will run in Spyder terminal, but not in a regular linux terminal

(我正在使用ubuntu 12.04)

我做了這個python程序:

#!/bin/sh
# -*- coding: utf-8 -*-

#Created on Tue Nov 12 19:44:50 2013

#@author: matthew

import os

print "Multiple Command Runner"
print "<Made by Matthew Cherrey>"
print "-------------------------"
numbcommand = 0
allcoms = []
while 1:
    numbcommand = numbcommand + 1
    command = raw_input(" Command: ")
    allcoms.append(command)
    decide = raw_input("Press [Enter] to and another command, press [r] to run all commands: ")
    if decide == "r":
        break

commands = ""
first = True
for item in allcoms:
    if first:
        commands = item
    else:
        commands = commands + " && " + item
os.system(commands)

我希望能夠在終端中運行它。 我使用python編輯器: Spyder這具有“在系統終端中運行”的選項。 每當我這樣做時,我的程序就可以完美運行。 我可以輸入多個命令,然后全部運行。 當我將文件設置為可使用並運行/home/matthew/.runallcommands.py --python/home/matthew/.runallcommands.py ,首先使光標變為“ t”,然后單擊鼠標左鍵,即為拍攝屏幕該區域的圖片,並將其另存為名為“ OS”的照片到我的主文件夾中。 然后我收到此錯誤消息:

matthew@matthew-MS-7721:~$ /home/matthew/.runallcommands.py --python
Warning: unknown mime-type for "Multiple Command Runner" -- using "application/octet-stream"
Error: no such file "Multiple Command Runner"
Warning: unknown mime-type for "<Made by Matthew Cherrey>" -- using "application/octet-stream"
Error: no such file "<Made by Matthew Cherrey>"
/home/matthew/.runallcommands.py: 13: /home/matthew/.runallcommands.py: numbcommand: not found
/home/matthew/.runallcommands.py: 14: /home/matthew/.runallcommands.py: allcoms: not found
/home/matthew/.runallcommands.py: 17: /home/matthew/.runallcommands.py: Syntax error: "(" unexpected (expecting "do")

我不確定它是否與我如何調用文件有關,因為我的程序在spyder的終端中可以100%正常工作。

第一行指示系統使用Bourne shell(而不是Python解釋器)運行此代碼。

更改

#!/bin/sh

#!/usr/bin/env python

當從Python IDE運行它時,IDE知道它是一個Python腳本,因此它顯式調用Python解釋器,從而繞開了第一行的指令。

另請參閱Wikipedia上的Shebang

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM