将shell脚本命令转换为命令行

时间:2015-02-11 10:58:25

标签: python shell sh

我正在使用多个命令行参数执行python脚本,但使用shell脚本。

执行shell脚本执行的

命令是:

./scripts/run_qa.sh data/questions/questions.txt data/lexicons/paralex data/weights/paralex.txt data/database > output.txt

run_qa.sh文件如下所示(请解释它是如何工作的):

#!/bin/bash
set -u
set -e
if [ $# != 4 ]; then
    echo "Usage: run.sh questions lexicon weights db"
    exit 1
fi
questions=$1
lexicon=$2
weights=$3
db=$4
PYTHONPATH=$PWD/python python -m lex.gearman_worker $lexicon $weights $db < $questions

我尝试在命令行

中执行如下的python命令
python -m python/lex/gearman_worker.py data/lexicons/paralex data/weights/paralex.txt data/database > output.txt

给出错误:

/usr/bin/python: Import by filename is not supported.

Update1:​​

gearman_worker.py文件导入其他文件,如:

import lex.parse
import lex.semantics
from collections import namedtuple
from collections import defaultdict

import line会出现如下错误:

ImportError: No module named lex.lexicon

Update2(在linux终端上执行):

export PYTHONPATH=$/mnt/paralex-evaluation-gearman/python
PYTHONPATH = ./python python -m python/lex/gearman_worker data/lexicons/paralex data/weights/paralex.txt data/database > output.txt

给出:

PYTHONPATH: command not found

然后

python -m python/lex/gearman_worker data/lexicons/paralex data/weights/paralex.txt data/database > output.txt

给出:

File "/mnt/paralex-evaluation-gearman/python/lex/gearman_worker.py", line 3, in <module>
    import lex.lexicon
ImportError: No module named lex.lexicon

1 个答案:

答案 0 :(得分:0)

您只需执行以下命令:

PYTHONPATH=./python python -m lex.gearman_worker ARGUMENT_2 ARGUMENT_3 ARGUMENT_4 < ARGUMENT_1

如果这不起作用,那么您可能必须导出PYTHONPATH设置:

export PYTHONPATH=${PWD}/python
python -m lex.gearman_worker ARGUMENT_2 ARGUMENT_3 ARGUMENT_4 < ARGUMENT_1

您传递给脚本的原始参数列为ARGUMENT_N

脚本只是:

  • 设置一些合理的默认值(请参阅the documentation for set
  • 测试已提供的正确数量的参数
  • 调用上面的命令

您尝试调用它:

  • 错过了脚本
  • 中的PYTHONPATH设置
  • 将gearman_worker模块作为文件传递,而不是传递给python模块