3

First, Already consulted:

  1. How do I run a Python script from C#?
  2. Call Python function from c# (.NET)
  3. calling python.py from C# .net
  4. Calling python script from C#
  5. Run Python .PY script from C#

While some of these has been somewhat helpful, I find my case specifically perplexing in how I want to solve it.I created a standard .NET Core project:

using System;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace automation1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ScriptEngine engine = Python.CreateEngine();
            engine.ExecuteFile(@"C:\Users\knoxb\Desktop\automation1.py");
        }
    }
}

On button click, I want to execute automation1.py:

import pyautogui as p

p.moveTo(300,300,2)

However, I am receiving the following exception: IronPython.Runtime.Exceptions.ImportException: 'No module named pyautogui'

Though I have already installed IronPython through NuGet and verified pyautogui was installed:

IronPython installed and package installed via Visual Studio

I find after hours of research without any solutions from the aforementioned questions or documentation that I needed to post this inquiry.

Thank you.

Bradly
  • 31
  • 1
  • 3
  • I have not run Python scripts yet, but with C#, as a guess, I think `pyautogui` is a library that (1) must be installed in machine and (2) must be imported (injected) in ScriptEngine manually or automatically. For (1), try `pip install pyautogui`. For (2), look at [here](https://www.needfulsoftware.com/IronPython/IronPythonCS2) – Tấn Nguyên Jul 02 '20 at 02:30
  • You also have a little bit confused with these 5 consults, which are used for controlling `python.exe` to run the compiled python file, **not compiling on the fly**. Meanwhile it seems that `ScriptEngine ` did it. – Tấn Nguyên Jul 02 '20 at 02:37
  • Thank you for your response. I was hoping by "injection" it would be as simple as: engine.ImportModule(@"pyautogui"); Which still kicks the same exception. Though, thank you for the link, I will read through this and hopefully find a solution. – Bradly Jul 02 '20 at 02:39
  • As for executing versus compiling, I have a working model where I execute a .py file by opening a process with python.exe, however, I am looking for more "instantaneous" method of executing python script from C# without a 10 second delay. – Bradly Jul 02 '20 at 02:41
  • Yes I prefer there runtime compiling because it brings many benefits: portability and running anywhere, dynamic using there output, not need to rebuild c# application on each update, not affected by real environment,... – Tấn Nguyên Jul 02 '20 at 03:00
  • As an update, the website you provided, I copied that example into my sample program and it still gives me the same exception... I cannot seem to get it to work either. – Bradly Jul 02 '20 at 03:15

0 Answers0