从命令行运行代码时出错,这些错误不在PyCharm中

时间:2016-07-06 17:44:20

标签: python linux bash shell

之前已经提出过这个问题,但没有一个解决方案似乎符合我的问题。

我有一个bash脚本与我的PyCharm的项目根目录保存在同一目录中。这包含以下代码:

#!/bin/bash

declare -a threshold=(some array of numbers);

for var in "${threshold[@]}"; do
      `python src/main/propertyPredictor.py data/triples.json data/sentenceMatrixFiltered.json data/output/predictedProperties.json $var`
      `python src/main/testFeatures.py data/featuresKept.json data/output/testLabels.json data/output/hyperTestLabels.json $var data/triples.json data/output/devLabels.json`
      `python src/main/logisticBagOfWords.py data/output/predictedProperties.json data/output/devLabels.json data/featuresKept.json data/output/evaluation.txt data/output/ data/output/summaryEval.csv`
done

这只是一个机器学习管道。

当我在PyCharm中运行上述每一个时,我没有错误。但是,当我通过. scriptname.shsh scriptname.shbash scriptname.sh从项目根目录中的命令行运行我的bash脚本时,以及当我在命令行上运行每个脚本时,我都会收到错误对于每个文件:

对于predictor.py,一切正常,但是当我运行第二个文件时,我得到:

Traceback (most recent call last):
  File "src/main/testFeatures.py", line 214, in <module>
    testLabels = testSentenceLabels()
  File "src/main/testFeatures.py", line 154, in testSentenceLabels
    return dict_list
UnboundLocalError: local variable 'dict_list' referenced before assignment

作为一个例子。

当我查看并运行实际的bash脚本时,每行都会出错:

src/main/propertyPredictor.py:84: RuntimeWarning: divide by zero encountered in double_scalars
  return abs(numer-denom)/numpy.abs(float(denom))
src/main/propertyPredictor.py:84: RuntimeWarning: invalid value encountered in double_scalars
  return abs(numer-denom)/numpy.abs(float(denom))
-bash: loading: command not found
Traceback (most recent call last):
  File "src/main/testFeatures.py", line 214, in <module>
    testLabels = testSentenceLabels()
  File "src/main/testFeatures.py", line 154, in testSentenceLabels
    return dict_list
UnboundLocalError: local variable 'dict_list' referenced before assignment
-bash: We: command not found
/Users/dhruv/anaconda/lib/python2.7/site-packages/sklearn/metrics/classification.py:958: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)
Traceback (most recent call last):
  File "src/main/logisticBagOfWords.py", line 329, in <module>
    testSet = str(os.path.splitext(sys.argv[2])[0]).split("/")[8]
IndexError: list index out of range
-bash: We: command not found
src/main/propertyPredictor.py:84: RuntimeWarning: divide by zero encountered in double_scalars
  return abs(numer-denom)/numpy.abs(float(denom))
src/main/propertyPredictor.py:84: RuntimeWarning: invalid value encountered in double_scalars
  return abs(numer-denom)/numpy.abs(float(denom))
-bash: loading: command not found
Traceback (most recent call last):
  File "src/main/testFeatures.py", line 214, in <module>
    testLabels = testSentenceLabels()
  File "src/main/testFeatures.py", line 154, in testSentenceLabels
    return dict_list
UnboundLocalError: local variable 'dict_list' referenced before assignment
-bash: We: command not found

这只是循环。

以下是关于PyCharm解释器的信息:

import sys; print(sys.executable)
import os; print(os.getcwd())
import sys; print(sys.path)

/Users/dhruv/anaconda/bin/python
/Users/dhruv/Documents/university/ClaimDetection/src/main
['/Users/dhruv/Documents/university/ClaimDetection/src/main', '/Users/dhruv/Documents/university/ClaimDetection', '/Users/dhruv/anaconda/lib/python27.zip', '/Users/dhruv/anaconda/lib/python2.7', '/Users/dhruv/anaconda/lib/python2.7/plat-darwin', '/Users/dhruv/anaconda/lib/python2.7/plat-mac', '/Users/dhruv/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/dhruv/anaconda/lib/python2.7/lib-tk', '/Users/dhruv/anaconda/lib/python2.7/lib-old', '/Users/dhruv/anaconda/lib/python2.7/lib-dynload', '/Users/dhruv/anaconda/lib/python2.7/site-packages', '/Users/dhruv/anaconda/lib/python2.7/site-packages/Sphinx-1.3.5-py2.7.egg', '/Users/dhruv/anaconda/lib/python2.7/site-packages/aeosa', '/Users/dhruv/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg']

从命令行运行相同的命令:

/Users/dhruv/anaconda/bin/python
/Users/dhruv/Documents/university/ClaimDetection
['', '/Users/dhruv/anaconda/lib/python27.zip', '/Users/dhruv/anaconda/lib/python2.7', '/Users/dhruv/anaconda/lib/python2.7/plat-darwin', '/Users/dhruv/anaconda/lib/python2.7/plat-mac', '/Users/dhruv/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/dhruv/anaconda/lib/python2.7/lib-tk', '/Users/dhruv/anaconda/lib/python2.7/lib-old', '/Users/dhruv/anaconda/lib/python2.7/lib-dynload', '/Users/dhruv/anaconda/lib/python2.7/site-packages', '/Users/dhruv/anaconda/lib/python2.7/site-packages/Sphinx-1.3.5-py2.7.egg', '/Users/dhruv/anaconda/lib/python2.7/site-packages/aeosa', '/Users/dhruv/anaconda/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg']

注意到这里存在差异,我首先在命令行上进入python解释器并执行:

import sys
sys.path.append("/Users/dhruv/Documents/university/ClaimDetection/")

等等。

然后我甚至将这些行添加到我的~./bash_profile文件中:

PYTHONPATH=/Users/dhruv/Documents/university/ClaimDetection/location:$PYTHONPATH
PYTHONPATH=/Users/dhruv/Documents/university/ClaimDetection/src/main/location:$PYTHONPATH
export PYTHONPATH

我仍然在第二个文件中遇到相同的错误。

我在这里做错了什么想法?我还有一个/~.profile文件 - 如果这可能有帮助我可以包含这些......

0 个答案:

没有答案