简体   繁体   English

如何在F#中为Python.NET设置Python路径?

[英]How do I set the Python path for Python.NET in F#?

I'm trying to use Python.NET from F#. Before using Python.NET from F#, I used it from C#. In order to get my code to work from C#, I had to set the path to a local Python dll with: Runtime.PythonDLL = "C:/Users/name/miniconda3/python39.dll";我正在尝试使用来自 F# 的 Python.NET。在使用来自 F# 的 Python.NET 之前,我使用了来自 C# 的它。为了让我的代码从 C# 开始工作,我必须将路径设置为本地 Python 88103788845 with: Runtime.PythonDLL = "C:/Users/name/miniconda3/python39.dll"; . .

Now I'm trying Python.NET from F#, but it doesn't work.现在我正在尝试来自 F# 的 Python.NET,但它不起作用。 I used this example code :我使用了这个示例代码

open Python.Runtime // dotnet add package pythonnet --version 3.0.1
open FSharp.Interop.Dynamic // dotnet add package FSharp.Interop.Dynamic --version 5.0.1.268
open System.Collections.Generic

[<EntryPoint>]
let main argv = 
    //set up for garbage collection?
    use gil = Py.GIL()

    //-----
    //NUMPY
    //import numpy
    let np = Py.Import("numpy")

    //call a numpy function dynamically
    let sinResult = np?sin(5)

    // more code
 
    0

When I run this code I get当我运行这段代码时,我得到

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Delegates' threw an exception.
  Source=Python.Runtime
  StackTrace:
   at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure()
   at Python.Runtime.Runtime.PyGILState_Ensure()
   at Python.Runtime.Py.GIL()
   at Program.main(String[] argv) in D:\Repos\TcBlack\src\Fblark\Program.fs:line 20

Inner Exception 1:
BadPythonDllException: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net

Inner Exception 2:
MissingMethodException: Failed to load symbol Py_IncRef.

Inner Exception 3:
Win32Exception: The specified procedure could not be found

I figured this is because I didn't set the Python dll location.我认为这是因为我没有设置 Python dll 位置。 But since I can't do Runtime.PythonDLL = "C:/Users/name/miniconda3/python39.dll" in F#, I'm not sure how to continue.但是由于我不能在 F# 中执行Runtime.PythonDLL = "C:/Users/name/miniconda3/python39.dll" ,所以我不确定如何继续。 Any ideas?有任何想法吗?

You can set the Python paths with Environment.SetEnvironmentVariable as also shown in this C# example .您可以使用Environment.SetEnvironmentVariable设置 Python 路径,如C# 示例中所示。

open Python.Runtime
open FSharp.Interop.Dynamic
open System
open System.IO

[<EntryPoint>]
let main argv =
    let pythonBasePath = "C:/Users/user/miniconda3"
    Environment.SetEnvironmentVariable("PYTHONHOME", pythonBasePath)
    Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", Path.Combine(pythonBasePath, "python39.dll"));

    PythonEngine.Initialize();
    PythonEngine.BeginAllowThreads();

    use gil = Py.GIL()

    let np = Py.Import("numpy")

    //call a numpy function dynamically
    let sinResult = np?sin(5)

    printfn "%A" sinResult
    
    0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM