2015-02-11 150 views
0

我正在使用多個命令行參數執行python腳本,但使用shell腳本。將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 

whic小時之後獲得錯誤:

/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 

進口行給出的錯誤是這樣的:

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 
+0

@mu無:不好意思,更新它 – nlper 2015-02-11 11:07:39

回答

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

腳本只是:

你嘗試的命令調用它:

  • 錯過了PYTHONPATH設置這是目前在腳本
  • 經過gearman_worker模塊作爲一個文件,而不是一個Python模塊導入
+0

你能請更新問題 – nlper 2015-02-11 11:09:57

+0

完成那。你很近。 – 2015-02-11 11:10:10

+0

我不想使用shell腳本,我想直接從終端執行命令。我做不到,你能幫忙嗎 – nlper 2015-02-11 12:04:05