1

考虑以下 Rust 代码:

use pyo3::prelude::*;

fn main() -> PyResult<()>{
    let gil = Python::acquire_gil();
    let py = gil.python();

    let result = py.run("print('it works')", None, None);

    if let Err(ref err) = result {
        println!("{:?}", err);
    }
    Ok(())
}

使用“标准”Python 安装,它运行良好,并打印it works. 我尝试在 Windows 上使用全新安装的 Anaconda 运行此代码,它输出以下内容:

Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\bug\\target\\debug\\bug.exe'
  sys.base_prefix = 'C:\\Users\\balog\\anaconda3'
  sys.base_exec_prefix = 'C:\\Users\\balog\\anaconda3'
  sys.executable = 'C:\\bug\\target\\debug\\bug.exe'
  sys.prefix = 'C:\\Users\\balog\\anaconda3'
  sys.exec_prefix = 'C:\\Users\\balog\\anaconda3'
  sys.path = [
    'C:\\Users\\balog\\anaconda3\\python38.zip',
    '.\\DLLs',
    '.\\lib',
    'C:\\bug\\target\\debug',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00000e6c (most recent call first):
<no Python frame>
error: process didn't exit successfully: `target\debug\bug.exe` (exit code: 1)

似乎所有 sys 可执行文件都指向错误的位置。

Rust 环境信息:

cargo 1.50.0 (f04e7fab7 2021-02-04)
release: 1.50.0
commit-hash: f04e7fab73128592a4063983c302da788bdfaba5
commit-date: 2021-02-04

rustc 1.53.0-nightly (f82664191 2021-03-21)
binary: rustc
commit-hash: f82664191d0e8764b7435b9d72eb0e366b8b1464
commit-date: 2021-03-21
host: x86_64-pc-windows-msvc
release: 1.53.0-nightly
LLVM version: 12.0.0

PyO3 version: 0.13.2

这是输出conda info -a


     active environment : base
    active env location : C:\Users\balog\anaconda3
            shell level : 1
       user config file : C:\Users\balog\.condarc
 populated config files : 
          conda version : 4.9.2
    conda-build version : 3.20.5
         python version : 3.8.5.final.0
       virtual packages : __cuda=9.1=0
                          __win=0=0
                          __archspec=1=x86_64
       base environment : C:\Users\balog\anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\balog\anaconda3\pkgs
                          C:\Users\balog\.conda\pkgs
                          C:\Users\balog\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\balog\anaconda3\envs
                          C:\Users\balog\.conda\envs
                          C:\Users\balog\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.19041
          administrator : False
             netrc file : None
           offline mode : False

# conda environments:
#
base                  *  C:\Users\balog\anaconda3

sys.version: 3.8.5 (default, Sep  3 2020, 21:29:08) [...
sys.prefix: C:\Users\balog\anaconda3
sys.executable: C:\Users\balog\anaconda3\python.exe
conda location: C:\Users\balog\anaconda3\lib\site-packages\conda
conda-build: C:\Users\balog\anaconda3\Scripts\conda-build.exe
conda-convert: C:\Users\balog\anaconda3\Scripts\conda-convert.exe
conda-debug: C:\Users\balog\anaconda3\Scripts\conda-debug.exe
conda-develop: C:\Users\balog\anaconda3\Scripts\conda-develop.exe
conda-env: C:\Users\balog\anaconda3\Scripts\conda-env.exe
conda-index: C:\Users\balog\anaconda3\Scripts\conda-index.exe
conda-inspect: C:\Users\balog\anaconda3\Scripts\conda-inspect.exe
conda-metapackage: C:\Users\balog\anaconda3\Scripts\conda-metapackage.exe
conda-render: C:\Users\balog\anaconda3\Scripts\conda-render.exe
conda-server: C:\Users\balog\anaconda3\Scripts\conda-server.exe
conda-skeleton: C:\Users\balog\anaconda3\Scripts\conda-skeleton.exe
conda-verify: C:\Users\balog\anaconda3\Scripts\conda-verify.exe
user site dirs: 

CIO_TEST: <not set>
CONDA_DEFAULT_ENV: base
CONDA_EXE: C:\Users\balog\anaconda3\condabin\..\Scripts\conda.exe
CONDA_EXES: "C:\Users\balog\anaconda3\condabin\..\Scripts\conda.exe"  
CONDA_PREFIX: C:\Users\balog\anaconda3
CONDA_PROMPT_MODIFIER: (base) 
CONDA_PYTHON_EXE: C:\Users\balog\anaconda3\python.exe
CONDA_ROOT: C:\Users\balog\anaconda3
CONDA_SHLVL: 1
CURL_CA_BUNDLE: <not set>
HOMEPATH: \Users\balog
PATH: C:\Users\balog\anaconda3;C:\Users\balog\anaconda3\Library\mingw-w64\bin;C:\Users\balog\anaconda3\Library\usr\bin;C:\Users\balog\anaconda3\Library\bin;C:\Users\balog\anaconda3\Scripts;C:\Users\balog\anaconda3\bin;C:\Users\balog\anaconda3;C:\Users\balog\anaconda3\Library\mingw-w64\bin;C:\Users\balog\anaconda3\Library\usr\bin;C:\Users\balog\anaconda3\Library\bin;C:\Users\balog\anaconda3\Scripts;C:\Users\balog\anaconda3\bin;C:\Users\balog\anaconda3\condabin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\Program Files\pysprint-cli\bin;C:\Users\balog\.cargo\bin;C:\Users\balog\AppData\Local\Microsoft\WindowsApps;C:\Users\balog\anaconda3\python.exe;.
PSMODULEPATH: C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
REQUESTS_CA_BUNDLE: <not set>
SSL_CERT_FILE: <not set>

有人可以指出我做错了什么吗?我已经尝试过研究,但那里没有太多资源。

4

2 回答 2

1

我在我们的一个内部应用程序中遇到了与 Miniconda 类似的问题。看起来他们确实在启动时修改了一些东西,导致我们的应用程序在启动时失败,但仅针对 Miniconda。

长话短说,在启动 Python 时,您需要在调用Py_SetPath之前调用。Py_Initialize

https://github.com/python/cpython/blob/master/PC/getpathp.c

于 2021-04-09T19:48:50.613 回答
0

我不会假装这是一个好的解决方案,但我找到了解决方法。Anaconda 设置CONDA_PREFIX环境变量,在我的情况下它适合PYTHONHOME. 但是,我不确定它是否适用于任何人,我更希望有一个通用的解决方案,所以我肯定会向 PyO3 提交问题。我也不认为最终用户应该自己调用不安全的函数。

use pyo3::ffi;
use pyo3::prelude::*;
use widestring::WideCString;

fn main() -> PyResult<()> {
    if let Some(PYTHONHOME) = std::env::var_os("CONDA_PREFIX") {
        unsafe {
            ffi::Py_SetPythonHome(
                WideCString::from_str(PYTHONHOME.to_str().unwrap())
                    .unwrap()
                    .as_ptr(),
            );
        }
    }

    let gil = Python::acquire_gil();
    let py = gil.python();

    let result = py.run("print('it works')", None, None);

    if let Err(ref err) = result {
        println!("{:?}", err);
    }
    Ok(())
}

相关问题:https ://github.com/PyO3/pyo3/issues/1554

于 2021-04-10T10:39:27.900 回答