1

Lets say I have this super Python script that needs to run cv2 in the future...

import cv2

def method():
    print("Hello")
    parameter = "l"
    return "OOPS"

method()

And in C# something like this.

Process p = new Process();
    p.StartInfo = new ProcessStartInfo(@"D:\Programming\Python\python.exe", fileName)
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };
    p.Start();
    string output = p.StandardOutput.ReadToEnd();

But this does throw an error "ImportError: DLL load failed". Alright seems like it is lookin in wrong directories for libraries since I have about 4 Python interpreters. Follows quick fix.

string path = @"D:\Programming\Python;" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONHOME", @"D:\Programming\Python;", EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONPATH ", @"D:\Programming\Python\Lib; D:\Programming\Python\DLLs", EnvironmentVariableTarget.Process);
            string fileName = @"..\Python\hello.py";

            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(@"D:\Programming\Python\python.exe", fileName)
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            p.Start();
            string output = p.StandardOutput.ReadToEnd();

Import DLL is fixed now, but another wild bug appeared named,
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

At this point I am lost and dont know what should I do next... any ideas are welcome, have a nice day.

UPDATE: Deleted all other python interpretors aside from anaconda and one virtual env and tried following:

  1. Run Python script from Visual Studio Code with given interpretor, works fine.
  2. Run it from Anaconda prompt, aswell.
  3. Added manually to system environment variables
    PATH=D:\Programming\Python
    PYTHONHOME=D:\Programming\Python PYTHONPATH=D:\Programming\Python\Lib;D:\Programming\Python\DLLs;D:\Programming\Python\Lib\site-packages

So now I can successfully call "python" from cmd, like that and check version, the virtual env is python 3.6 and this is the right one. Python is correct
But this is where all the fun begins you would expect "hello" in your console... hell incarnate

Barthilas
  • 23
  • 6
  • Take a look at this answer: https://stackoverflow.com/a/15331222/546000; it might help. You're on the right path, but the one thing I see in this answer different is the explicit inclusion of /site-packages/ in the `PYTHONPATH` variable. – Dmitriy Khaykin Dec 01 '18 at 14:34
  • Are you using a Windows machine? If so, you can run powershell, and from powershell, run the python script https://stackoverflow.com/a/53324757/7927820 you have to add `python xxx.py` in `process.StartInfo.Arguments` – Hanjun Chen Dec 01 '18 at 14:41
  • @DmitriyKhaykin Tried that but no luck, but thank you – Barthilas Dec 01 '18 at 15:02
  • @HanjunChen I am but when i call from powershell python it does not even recognize it, but python from cmd is the anaconda one and doesnt work too, however strangely when I run that script in Visual Studio Code with the correct interpreter with OpenCV it works – Barthilas Dec 01 '18 at 15:02
  • The module need to be installed in the Lib\site-packages folder of your python installation. (e.g. C:\Users\YourUser\AppData\Local\Programs\Python\Python37\Lib\site-packages). You can install it with pip command from the windows command line (e.g. pip install opencv-python) – Ezra May 23 '21 at 11:01

1 Answers1

1

Did not find correct answer to this problem, but discovered workaround in p2exe or pyinstaller.
Simply call pyinstaller.py --onefile xx.py and create exe file and pass that into process.

Barthilas
  • 23
  • 6