admin管理员组文章数量:1419897
I have a C# project which is a simple console application, which takes an input from the user and returns an output.
In this solution I have created a Python file named app.py along with a virtual environment named env and a requirements.txt file. This Python file takes an input and returns an output.
Using the following commands I can run the Python file:
python -m venv env
.\env\Scripts\activate
pip list
python -m pip install -U pip
python -m pip install -U groq
python -m pip install -U python-dotenv
Although it seems I didn't use the requirements.txt file (I'm new to Python)
Can I use this Python file with all its dependencies in C#? In a way that I can take input from the user using C# (console application), pass it to the Python file, and receive the output that the Python file gives in C# and display it to the user?
The version is .NET 9 and Python 3.12.
I saw a number of libraries like ironpython, but apparently they are for using Python code in C# or vice versa.
I have a C# project which is a simple console application, which takes an input from the user and returns an output.
In this solution I have created a Python file named app.py along with a virtual environment named env and a requirements.txt file. This Python file takes an input and returns an output.
Using the following commands I can run the Python file:
python -m venv env
.\env\Scripts\activate
pip list
python -m pip install -U pip
python -m pip install -U groq
python -m pip install -U python-dotenv
Although it seems I didn't use the requirements.txt file (I'm new to Python)
Can I use this Python file with all its dependencies in C#? In a way that I can take input from the user using C# (console application), pass it to the Python file, and receive the output that the Python file gives in C# and display it to the user?
The version is .NET 9 and Python 3.12.
I saw a number of libraries like ironpython, but apparently they are for using Python code in C# or vice versa.
Share Improve this question asked Jan 29 at 7:14 armanarman 8993 gold badges14 silver badges30 bronze badges 5- Starting a new process for each Python script execution is expensive – Bhargav Commented Jan 29 at 7:20
- Could you explain what your overall goal is? Something like this is certainly doable, but including multiple programming languages tend to increase the development cost, and also make deployment and configuration more complex. So are you sure this is the best approach for whatever you are trying to do? – JonasH Commented Jan 29 at 7:30
- @JonasH My goal is to use artificial intelligence. I have written artificial intelligence code with Python. This project is a sample. In the product I use microservices and create a service specifically for artificial intelligence. This is a simple project where I want to create an artificial intelligence in C#. Given that there is no official library for C# or I did not find it (groq), I decided to implement the part of the connection with artificial intelligence with Python itself. – arman Commented Jan 29 at 7:37
- 2 It is not clear what exactly you mean by "Artificial intelligence" since this is a rather broad term. But there are plenty of AI libraries for both c# and python, so if you want an AI in c# you should probably implement it in c#. If you are using microservices it makes even less sense to mix python and c#, since you would just query your microservice. One of the primary advantages of microservices is allowing different services to use different languages. – JonasH Commented Jan 29 at 7:54
- What I meant was that the service is written entirely in Python for artificial intelligence. In this case, the artificial intelligence is a chatbot using groq. – arman Commented Jan 29 at 8:01
1 Answer
Reset to default 1You can use Process.Start()
and call the python script.
C#
using System.Diagnostics;
class Program
{
static void Main()
{
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
var startInfo = new ProcessStartInfo
{
FileName = "env\\Scripts\\python.exe",
Arguments = "app.py",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using var process = new Process { StartInfo = startInfo };
process.Start();
process.StandardInput.WriteLine(name);
string result = process.StandardOutput.ReadToEnd();
Console.WriteLine("\nPython returned:");
Console.WriteLine(result);
}
}
Python app.py
name = input("Enter name: ")
print(f"Hello, {name} from Python!")
As mentioned in comments: It's doable: At what cost?
- Performance Overhead.
- Dependency Management.
- Maintenance
What is best?
- Consider rewriting the functionality in C# if possible
- Consider using REST APIs if the Python service is available that way
本文标签: Can I call python script in cStack Overflow
版权声明:本文标题:Can I call python script in c#? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745307594a2652740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论