如何使用来自 IronPython 的包调用 Python 脚本?
How do I call Python scripts with packages from IronPython?
如何在我的 C# 项目中实现需要外部模块(例如 BeautifulSoup4 和请求)的 Python 脚本?我想在 Python 脚本中调用一个函数,但它一直向我抛出一个 "IronPython.Runtime.Exceptions.ImportException: 'No module named requests'"。
我没有编写任何 IronPython 脚本,因为我使用的是另一个用户制作的 Python 脚本。我用 Python 设置了一个虚拟环境来导入必要的模块。
我不知道从哪里着手解决这个问题。我查看了多个站点,但它们都要求我编写一个 IronPython 脚本。除了设置必要的脚本之外,是否没有其他调用函数的选项?
C#代码:
static dynamic pythonFile;
static void Main(string[] args)
{
// Loading Python file
var ipy = Python.CreateRuntime();
pythonFile = ipy.UseFile("test.py"); //throws error when this launches.
DisplayMain();
Console.ReadKey();
}
Python 代码(例如小片段):
import requests
def get_stock(stock_name, price_metric):
print(stock_name + " " + price_metric)
def get_industry_indicators(startTime, endTime):
print(startTime + " " + endTime)
def get_market_cap(industry, startTime, endTime):
print(industry + " " + startTime + " " + endTime)
def get_headers(link):
print(requests.get(link))
我希望能够调用我的 python 函数 (get_headers) 并传入 link,然后在我的 C# 控制台上打印它。请帮忙!
我发现了错误。必须包含所需模块的搜索路径。这是一个例子。
var engine = Python.CreateEngine();
var searchPaths = new List<string>();
searchPaths.Add(AppDomain.CurrentDomain.BaseDirectory + @"\Lib\site-packages");
searchPaths.Add(@"C:\...\projectName\Lib\");
engine.SetSearchPaths(searchPaths);
engine.ExecuteFile("test.py");
如何在我的 C# 项目中实现需要外部模块(例如 BeautifulSoup4 和请求)的 Python 脚本?我想在 Python 脚本中调用一个函数,但它一直向我抛出一个 "IronPython.Runtime.Exceptions.ImportException: 'No module named requests'"。 我没有编写任何 IronPython 脚本,因为我使用的是另一个用户制作的 Python 脚本。我用 Python 设置了一个虚拟环境来导入必要的模块。
我不知道从哪里着手解决这个问题。我查看了多个站点,但它们都要求我编写一个 IronPython 脚本。除了设置必要的脚本之外,是否没有其他调用函数的选项?
C#代码:
static dynamic pythonFile;
static void Main(string[] args)
{
// Loading Python file
var ipy = Python.CreateRuntime();
pythonFile = ipy.UseFile("test.py"); //throws error when this launches.
DisplayMain();
Console.ReadKey();
}
Python 代码(例如小片段):
import requests
def get_stock(stock_name, price_metric):
print(stock_name + " " + price_metric)
def get_industry_indicators(startTime, endTime):
print(startTime + " " + endTime)
def get_market_cap(industry, startTime, endTime):
print(industry + " " + startTime + " " + endTime)
def get_headers(link):
print(requests.get(link))
我希望能够调用我的 python 函数 (get_headers) 并传入 link,然后在我的 C# 控制台上打印它。请帮忙!
我发现了错误。必须包含所需模块的搜索路径。这是一个例子。
var engine = Python.CreateEngine();
var searchPaths = new List<string>();
searchPaths.Add(AppDomain.CurrentDomain.BaseDirectory + @"\Lib\site-packages");
searchPaths.Add(@"C:\...\projectName\Lib\");
engine.SetSearchPaths(searchPaths);
engine.ExecuteFile("test.py");