First, Already consulted:
- How do I run a Python script from C#?
- Call Python function from c# (.NET)
- calling python.py from C# .net
- Calling python script from C#
- 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.
pyautoguiis a library that (1) must be installed in machine and (2) must be imported (injected) in ScriptEngine manually or automatically. For (1), trypip install pyautogui. For (2), look at herepython.exeto run the compiled python file, not compiling on the fly. Meanwhile it seems thatScriptEnginedid it.