从bash脚本调用时,PythonC Import无法正常运行

时间:2020-05-28 04:09:22

标签: python bash python-c-api

我目前正在创建一个bash文件来多次测试程序。该程序包括在c ++内部实现的PythonC,当我正常调用文件./lpopt -f File时,它可以正常工作。但是,如果我在bash脚本中包含同一行,则无法导入模块。我是否需要单独提供bash python路径?如果可以,怎么办?

这是我的代码中处理PythonC导入的部分

#include <python2.7/Python.h>
#include <iostream>
#include <algorithm>
#include "predictor.hpp"

using namespace std;


predictor::predictor()
{
    Py_Initialize();

    pName = PyString_FromString("predictor");

    pModule = PyImport_Import(pName);

    if (pModule == nullptr) {
    PyErr_Print();
    std::cerr << "Fails to import the module predictor, check installation.\n";
    }

编辑:我忘记添加bash脚本。产生错误的行是 $ LPOPT_PRD -f $ d / encoding.asp> $ LPOPTPRDENCODING

#!/bin/bash

LPOPT='/home/danielb/Downloads/src/FLPOPT/bin/lpopt'
LPOPT_PRD='/home/danielb/Documents/LpoptVersions/bin/lpopt'

d=$1
mkdir -p "/home/danielb/Downloads/ModifiedEncodings/${d}"

LPOPTENCODING="/home/danielb/Downloads/ModifiedEncodings/${d}/${d}Lpopt.asp"
LPOPTPRDENCODING="/home/danielb/Downloads/ModifiedEncodings/${d}/${d}LpoptPrd.asp"

$LPOPT -f $d/encoding.asp > $LPOPTENCODING
$LPOPT_PRD -f $d/encoding.asp > $LPOPTPRDENCODING

for instance in $(find ${PWD}/${d} -name '*-*'); do

    LPEN=$(gringo $LPOPTENCODING $instance | wc -l)
    printf "%s,%s\n" $(echo $instance | cut -d '/' -f 7 | cut -d '-' -f 1) $LPEN >> "/home/danielb/Downloads/ModifiedEncodings/${d}/LpoptInstanceStats.txt" 


    LPPRDEN=$(gringo $LPOPTPRDENCODING $instance | wc -l)
    printf "%s,%s\n" $(echo $instance | cut -d '/' -f 7 | cut -d '-' -f 1) $LPPRDEN >> "/home/danielb/Downloads/ModifiedEncodings/${d}/LpoptPrdInstanceStats.txt" 


    EN=$(gringo $d/encoding.asp $instance | wc -l)
    echo $EN
    printf "%s,%s\n" $(echo $instance | cut -d '/' -f 7 | cut -d '-' -f 1) $EN >> "/home/danielb/Downloads/ModifiedEncodings/${d}/InstanceStats.txt" 


    echo "Completed instance"

done

这是带有错误消息的附加命令

sudo bash TestingScript.sh KnightTourWithHoles
[sudo] password for danielb: 
ImportError: No module named predictor
Fails to import the module predictor, check installation.
TestingScript.sh: line 13:  7121 Segmentation fault      (core dumped) $LPOPT_PRD -f $d/encoding.asp > $LPOPTPRDENCODING

为防万一,为了方便起见,我要导入的模块位于与站点包稍有不同的目录中。供参考

>>> print(predictor.__file__)
/home/danielb/miniconda2/lib/python2.7/site-packages/predictor-0.0.0-py2.7.egg/predictor.pyc
>>> import time
>>> print(time.__file__)
/home/danielb/miniconda2/lib/python2.7/lib-dynload/time.so

0 个答案:

没有答案