Quantcast
Viewing all articles
Browse latest Browse all 5986

Python Script Fails to Find TensorFlow Module When Executed from .NET Core Application (ModuleNotFoundError)

I'm trying to execute a Python script from my .NET Core application, but I'm encountering a ModuleNotFoundError: No module named 'tensorflow'. The script runs perfectly fine when executed manually from the command line, and TensorFlow is detected without any issues. However, when I run the script through my .NET Core application, it fails to find the TensorFlow module.

Here’s the relevant C# code:

private async Task<string> RunPythonScript(string scriptPath){    try    {        var start = new ProcessStartInfo        {            FileName = "/usr/bin/python3",  // Path to Python            Arguments = $"\"{scriptPath}\"",            RedirectStandardOutput = true,            RedirectStandardError = true,            UseShellExecute = false,            CreateNoWindow = true,            WorkingDirectory = "/var/www/myapp/publish/",        };        // Setting environment variables        start.EnvironmentVariables["PYTHONPATH"] = "/home/ubuntu/.local/lib/python3.12/site-packages";        start.EnvironmentVariables["PATH"] = "/home/ubuntu/.local/bin:/usr/bin:/bin";        using (var process = Process.Start(start))        {            string result = await process.StandardOutput.ReadToEndAsync();            string error = await process.StandardError.ReadToEndAsync();            if (!string.IsNullOrEmpty(error))            {                Console.WriteLine($"Python error: {error}");                return $"Python error: {error}";            }            return result;        }    }    catch (Exception ex)    {        return $"Error occurred while running Python script: {ex.Message}";    }}

I've confirmed that TensorFlow is installed in the /home/ubuntu/.local/lib/python3.12/site-packages directory and that this path is included in PYTHONPATH. The script executes without issues from the terminal, so I'm not sure why it fails to find the module when executed via the .NET Core application.

import osimport sysimport json  sys.path.append('/home/ubuntu/.local/lib/python3.12/site-packages')import tensorflow as tf  ---> exception from grape_classification import process_request# oneDNN optimizasyonlarını devre dışı bırakmaos.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
  • Manually Running the Python Script: The script runs successfully from the terminal and finds the TensorFlow module without any issues.
  • Setting PYTHONPATH and PATH in the .NET Core Application: I added the necessary environment variables to ensure the Python environment and TensorFlow are correctly referenced when running the script through the .NET Core application.
  • Verifying TensorFlow Installation: I confirmed that TensorFlow is installed in the specified directory (/home/ubuntu/.local/lib/python3.12/site-packages) and that the path is correctly set in PYTHONPATH.
  • Checking File Permissions: I ensured that the required files and directories have the appropriate permissions to be accessed by the .NET Core application.
  • Running on Windows Local Environment: The script also runs perfectly fine on my Windows local environment, where it correctly finds and uses the TensorFlow module.

I expected the script to run seamlessly from the .NET Core application, just like it does when executed manually on both the Ubuntu server and Windows local environment, and to successfully find and load the TensorFlow module. However, despite these efforts, the script still fails to locate the module when executed from the .NET Core app on the Ubuntu server.


Viewing all articles
Browse latest Browse all 5986

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>