输入:找不到使用Shell脚本运行python的命令

问题描述

我只是使用此shell脚本在ssh中运行我的python代码,并且可以运行,但是我想在本地计算机上运行它,并且以某种方式使用Windows和git-bash时,它无法识别我的代码。我认为问题出在shell脚本而不是python代码,因为它之前运行正确。

错误

SyntaxError: invalid Syntax 
line 1: import: command not found 
line 3: Syntax error near unexpected token `(`
line 3: 'def get_stdin():'

脚本:

if [[ "$OSTYPE" =~ ^msys ]]; then
  dos2unix *.sh // I've put this in and taken it out and I don't think it does anything 
  py -3 $*
else
  dos2unix *.sh
  python3
fi
./make_voc.py

相关的Python代码

import sys

def get_stdin():
    text = sys.stdin.read()

命令

cat INPUTFILE | ./shellscript.sh > OUTPUTFILE

解决方法

  1. 确保您的代码(Shell和Python)均为DOS格式。
  2. 将您的python代码称为python3 ./make_voc.py

如果仍然无法正常运行,请确保可以在Shell脚本中以及在Windows中创建的新Python代码文件中分别运行Hello World代码(不是从Linux / Unix复制)。在两个代码文件都正常工作之后,请从外壳程序代码中调用python代码。

没有'if'...保持简单。例如在myShell.sh中:(仅两行):

从myShell.sh回显Hello World

python3 myPython.py

您应该在屏幕上看到两条Hello World消息。

如果您的Shell解释器无法在cmd窗口中运行,那么您可能会发现拥有适用于Linux的Windows子系统很有用。请参阅canada.ca/en/revenue-agency.html Windows 10中的默认bash不能像Linux中的真正Bash那样完全起作用。因此它无法调用您的python代码。