Search code examples
pythonrustanacondacondapyo3

PyO3 can't find Python interpreter when using Anaconda


Consider the following Rust code:

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(())
}

With a "standard" Python installation, this runs fine, and prints it works. I tried running this code with a fresh installation of Anaconda on Windows, and it outputted the following:

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)

It seems all of the sys executables are pointing to a wrong location.

Rust environment info:

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

and this is the output of 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>

Can someone point out what I'm doing wrong? I've tried researching, but there's not much resources out there.


Solution

  • I won't pretend it's a good solution, but I found a workaround. Anaconda sets up the CONDA_PREFIX environment variable, and in my case it was suitable as PYTHONHOME. However, I'm not sure it will work for anyone, and I'd rather want a universal solution to this, so I'll definitely submit an issue to PyO3. I also don't think end users should call unsafe functions themselves.

    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(())
    }
    

    Related issue: https://github.com/PyO3/pyo3/issues/1554