【问题标题】:Python program will run in Spyder terminal, but not in a regular linux terminalPython 程序将在 Spyder 终端中运行,但不能在常规 linux 终端中运行
【发布时间】:2013-11-26 04:23:29
【问题描述】:

(我使用的是 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% 正常运行。

【问题讨论】:

  • 去掉 周围的 Made by Matthew Cherrey 并尝试
  • 我有 Debian 7.0 (Wheezy) 并使用以下命令执行您的文件: 运行良好...
  • 以点开头的文件名是一个非常糟糕的选择。

标签: python linux ubuntu spyder


【解决方案1】:

第一行指示系统使用 Bourne shell 运行它,而不是 Python 解释器。

改变

#!/bin/sh

类似

#!/usr/bin/env python

当您从 Python IDE 运行它时,IDE 知道它是 Python 脚本,因此它显式调用 Python 解释器,绕过第一行的指令。

另见Shebang on Wikipedia

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 2021-09-12
    • 2017-10-24
    • 1970-01-01
    • 2021-12-09
    • 2021-03-11
    • 2018-09-23
    • 2013-01-15
    • 2021-11-21
    相关资源
    最近更新 更多