0

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
-
-
-
-
  • Are any of the files used by Python on a Network drive? I have had issues when full pathnames exceed around 120 characters. – jdweng Jul 24 '19 at 16:33
  • You are probably using different interpreters with different installed packages. To find out your interpreter path: https://stackoverflow.com/questions/2589711/find-full-path-of-the-python-interpreter . To install packages to ipython: https://stackoverflow.com/questions/8663046/how-to-install-a-python-package-from-within-ipython – G. B. Jul 24 '19 at 17:11
  • 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 Jul 24 '19 at 18:38
  • Thanks jdweng. All files are on my PC. – Allen Hawthorne Jul 24 '19 at 18:40

1 Answers1

-1

I found the problem. I needed to include:

using IronPython.Runtime; using IronPython;