C:\ Python \ Python37 \ python.exe:在路径中找不到'main'模块

时间:2018-12-15 21:24:31

标签: c# python module process

所以我正在用C#编写程序,但是我对该语言还很陌生,所以我重用了我很久以前创建的python脚本。它在命令行,空闲,vs代码和Linux上都可以正常工作,但是在c#应用程序中,我有一个静态方法,该方法运行python网络搜寻器并返回错误(如果存在),并返回脚本输出。 但是,当我尝试在c#应用程序中运行python脚本时,python文件失败并显示

C:\Python\Python37\python.exe: can't find 'main' module in 'C:\Users\me\source\repos\myapp\myapp\bin\Debug'.

网络爬虫看起来像这样

import sys
from bs4 import BeautifulSoup as Soup
from urllib.request import Request as Req, urlopen as uOpen
from urllib.parse import quote
from random import randint as random

def search(query, index):
    query = query.strip()
    searchquery = query.replace(' ', '+')
    request = Req(f'https://www.google.co.uk/search?q={searchquery}&tbm=isch', headers={'User-Agent':'Mozilla/5.0'}) 
    client = uOpen(request)
    soup = Soup(client.read(), 'html.parser')
    client.close()
    images = [image['src'] for image in  soup.findAll(f'img', {'alt':f'Image result for {query}'})]
    if len(images) < 1:
        raise IndexError('didnt find any images')
    if index == None:
        return images[random(0, len(images) - 1)]
    return images[index]


args = sys.argv[1:]

query = ' '.join(args[0:-1])
index = args[-1]

imageURL = search(query, int(index))

print(imageURL)

提醒您,在cmd,idle,vs代码和linux中使用时,它可以正常工作。 至于c#方面,它看起来像这样。

public static string RunPythonScript(string FileName, string[] args)
    {
        ProcessStartInfo StartInfo = new ProcessStartInfo();

        StartInfo.FileName = "C:\\Python\\Python37\\python.exe";
        StartInfo.Arguments = String.Format("{0} {1}", FileName, string.Join(" " , args));
        StartInfo.UseShellExecute = false;
        StartInfo.CreateNoWindow = true;
        StartInfo.RedirectStandardError = true;
        StartInfo.RedirectStandardOutput = true;
        using (Process Execution = Process.Start(StartInfo))
        {
            using (StreamReader Reader = Execution.StandardOutput)
            {
                string StdError = Execution.StandardError.ReadToEnd();

                if (!(StdError == null || StdError == ""))
                    return StdError;

                string Result = Reader.ReadToEnd();

                return Result;
            }
        }
    }

0 个答案:

没有答案