I am trying to call a python script from a .net program. When I call the script directly from my console, everything works fine, but when I call it from a .net program it crashes with the following exception.
Traceback (most recent call last):
File "run.py", line 1, in <module>
import pika
File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\envs\py37\lib\site-packages\pika\__init__.py", line 10, in <module>
from pika.connection import ConnectionParameters
File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\envs\py37\lib\site-packages\pika\connection.py", line 13, in <module>
import ssl
File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\envs\py37\lib\ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified module could not be found.
The script I'm using is pretty long, but the bug is reproducible with something as simple as
import pika
(I'm using pika 1.1.0, but pika isn't the only package that fails, a lot do).
This isn't the first time I run into this bug. I ran into it with Pycharm before, and it seems to be a common issue Python 3.7 anaconda environment - import _ssl DLL load fail error . This only happens with Python 3.7, but I sadly can't go back to 3.6 or lower. The fix in Pycharm is to append the environment variables (more specifically, the PATH variable) explicitely. I tried doing the same in .net but the bug remained.
The .net script looks something like this
using System.Diagnostics;
using System.IO;
using NLog;
using System;
namespace ConsoleApp1
{
class Program
{
static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
string cmd = "run.py";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "PATH_TO_PYTHON_3.7_ENV.exe";
start.Arguments = string.Format("\"{0}\"", cmd);
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd();
string result = reader.ReadToEnd();
Console.WriteLine(stderr);
Console.ReadKey();
}
}
}
}
}
Has anyone run into this problem before and found a way to fix it?