简体   繁体   English

输入:未找到使用 shell 脚本运行 python 的命令

[英]Input: command not found running python with shell script

I was just using this shell script to run my python code in ssh and it was working, but I want to run it on my local machine and somehow using Windows and git-bash it is not recognizing my code.我只是使用这个 shell 脚本在 ssh 中运行我的 python 代码并且它正在工作,但我想在我的本地机器上运行它并且以某种方式使用 Windows 和 git-bash 它无法识别我的代码。 I think the problem is in the shell script instead of the python code because it was running correctly before.我认为问题出在 shell 脚本而不是 python 代码中,因为它之前运行正确。

The error:错误:

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

The script:剧本:

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

The relevant Python code相关Python代码

import sys

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

The command命令

cat INPUTFILE | ./shellscript.sh > OUTPUTFILE
  1. ensure your codes (both Shell & Python) are in DOS format.确保您的代码(Shell 和 Python)是 DOS 格式。
  2. call your python code as python3./make_voc.py将您的 python 代码称为 python3./make_voc.py

if still not working then ensure you can run a Hello World code in a Shell script & in a new Python code file separately that you created in Windows (not copied from Linux/Unix).如果仍然无法正常工作,请确保您可以在 Shell 脚本中运行 Hello World 代码,并在您在 Windows 中创建的新 Python 代码文件中单独运行(不是从 Linux/Unix 复制的)。 After you got your both code files working then call python code from your shell code.在你的两个代码文件都工作之后,然后从你的 shell 代码调用 python 代码。

No 'if'...keep it simple.没有'如果'......保持简单。 For example in myShell.sh: (only two lines):例如在 myShell.sh 中:(只有两行):

echo Hello World from myShell.sh从 myShell.sh 回显 Hello World

python3 myPython.py python3 myPython.py

you should see both Hello World messages on your screen.您应该会在屏幕上看到两条 Hello World 消息。

If your shell interpreter doesn't work in your cmd window then you might find it useful to have Windows subsystem for Linux. Refer to canada.ca/en/revenue-agency.html Default bash in Window 10 is not fully functional like real Bash in Linux. So it can not call your python code. If your shell interpreter doesn't work in your cmd window then you might find it useful to have Windows subsystem for Linux. Refer to canada.ca/en/revenue-agency.html Default bash in Window 10 is not fully functional like real Bash在Linux。所以它不能调用你的python代码。

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

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