简体   繁体   English

QProcess无法启动python脚本

[英]QProcess can't start python script

The problem is that the python script doesn't run.问题是 python 脚本没有运行。 I expect to see an output file (tmp.json) in the directory but I do not.我希望在目录中看到一个输出文件 (tmp.json),但我没有。 There are some questions with the same issue but the solutions did not work for me.有一些问题与相同的问题有关,但解决方案对我不起作用。 The python.exe path is correct as I used to use python.h instead of Qprocess to embed python. python.exe 路径是正确的,因为我曾经使用 python.h 而不是 Qprocess 来嵌入 python。

main主要的

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QProcess>


int main(int argc, char *argv[])
{
    
    QProcess p;
    QStringList params;
    params << "F:/NLP/google_corpus/scrape_python/qt/cpy2/someFunction.py";
    p.start("C:/Users/A/AppData/Local/Programs/Python/Python39/python.exe", params);
    QString p_stdout = p.readAll();
    QString p_stderr = p.readAllStandardError();
    if ( p.state() == QProcess::NotRunning ) {
        return -2;
    };
    p.waitForFinished(-1);

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

someFunction.py someFunction.py

import json
with open('F:\NLP\google_corpus\scrape_python\qt\cpy2tmp.json', 'w') as json_obj:
        json.dump(2, json_obj)

The character \\ is used for escape sequences in Python, so you should write \\\\ to express \\ in strings.字符\\在 Python 中用于转义序列,因此您应该编写\\\\来表示字符串中的\\

import json
with open('F:\\NLP\\google_corpus\\scrape_python\\qt\\cpy2tmp.json', 'w') as json_obj:
        json.dump(2, json_obj)

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

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