0
votes

I'm trying to execute a simple Python script from C# program. The Python script imports cv2 and numpy. This script works when executing from command line, but cannot find cv2 when executing from C#. I'm using Anaconda3, IronPython, Windows 10, Visual Studio 2015 Profesional. I'm new to Python, but I think this may be a path/location problem.

The instruction:

engine.ExecuteFile(@"C:\Development\Test.py");

gives the following exception:

"No module named cv2"

See the code below:

Using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace EmbededPyConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
           ExecutePythonScript();
        }
        private static void ExecutePythonScript()
        {
            ScriptEngine engine = Python.CreateEngine();
            engine.ExecuteFile(@"C:\Development\Test.py");
        }
    }
}
Python test script:

import cv2
import numpy as np
-
-
-
-
1
Are any of the files used by Python on a Network drive? I have had issues when full pathnames exceed around 120 characters. - jdweng
You are probably using different interpreters with different installed packages. To find out your interpreter path: stackoverflow.com/questions/2589711/… . To install packages to ipython: stackoverflow.com/questions/8663046/… - G. B.
Thanks G.B. So Anaconda and IronPython use different interpreter? As I said, I'm new to Python. I can say that the Test.py runs fine from the Anaconda3 power shell prompt, which is using the environment and interpreter I set up. The error only occurs when I run the C# program, which uses IronPython for creating the engine, but I have no idea what that entails or what needs to be identified. It may well be a case of two interpreters, but I'm not sure what to do about it. IronPython package was obtained with NuGet, if that helps. - Allen Hawthorne
Thanks jdweng. All files are on my PC. - Allen Hawthorne

1 Answers

-1
votes

I found the problem. I needed to include:

using IronPython.Runtime; using IronPython;