Click here to Skip to main content
16,020,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am running a python script using c#. The only issue is i have to install python environment and libraries on target machine.

Is there alternative method/library that has python interpreter pre-installed within and does not need Python to be installed on Target Machine?

What I have tried:

I have tried running code using Pyhtonnet library and it runs as expected.I am using numpy and scipy in my script.
Posted
Updated 9-Jun-23 1:14am

It is possible to run the script without having an environment installed on your users machine. You will however still need some libraries installed, so the answer to your question is yes and no -

These links should point you in the right direction -
How do I run a Python script from C#[^]
Steps - How to call a Python function from C# using the .Net library[^]
Calling Python from C#: an introduction to PythonNET[^]

You mentioned that you used the Python.NET library, the below steps worked for us -
1. Install the Python.NET library from Visual Studio, right-click on your project, select "Manage NuGet Packages," search for "PythonNet.
2.Import the namespaces in your C# code -
using Python.Runtime;

3. Initialize the Python runtime -
using (Py.GIL()) 
{
    PythonEngine.Initialize();
    // ...
}

4. Execute the Python script -
using (Py.GIL()) 
{
    PythonEngine.Initialize();
    PythonEngine.Exec("print('Hello, Python!')"); //sample...
//your actual Python script content goes here...    
}

5. Clean up after execution -
using (Py.GIL()) 
{
    PythonEngine.Initialize();
    PythonEngine.Exec("print('Hello, Python!')"); //sample...
//your actual Python script content goes here...
    PythonEngine.Shutdown();
}


Note that the Global Interpreter Lock (GIL) block (See links) is required to allow Python code to execute
 
Share this answer
 
Python scripts can only be run by a Python interpreter. So you must install it on any system that needs to run the script.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900