indygreg / PyOxidizer

A modern Python application packaging and distribution tool
Mozilla Public License 2.0
5.32k stars 227 forks source link

pyembed macos compile error init_fs_encoding #702

Open Omicronlawful opened 1 year ago

Omicronlawful commented 1 year ago

I am using macos and venv. I am trying to run python code with pyembed. I tried setting filesystem_encoding and the python path to the python from the venv

I took this from the bench and it works. I think the default should be updated to work out of the box.

 config.interpreter_config.parse_argv = Some(false);
    config.set_missing_path_configuration = false;
    config.argv = Some(vec!["python".into()]);
    config.interpreter_config.executable = Some("python".into());
Python path configuration:
  PYTHONHOME = '/Users/user/code/rust/test/target/debug'
  PYTHONPATH = (not set)
  program name = '/Users/user/code/rust/test/target/debug/test'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/Users/user/code/rust/test target/debug/test'
  sys.base_prefix = '/Users/frederik/code/rust/manga-image-translator/target/debug'
  sys.base_exec_prefix = '/Users/user/code/rust/test/target/debug'
  sys.platlibdir = 'lib'
  sys.executable = '/Users/user/code/rust/test/target/debug/test'
  sys.prefix = '/Users/user/code/rust/test/target/debug'
  sys.exec_prefix = '/Users/user/code/rust/test/target/debug'
  sys.path = [
    '/Users/user/code/rust/test/target/debug/lib/python310.zip',
    '/Users/user/code/rust/test/target/debug/lib/python3.10',
    '/Users/user/code/rust/test/target/debug/lib/python3.10/lib-dynload',
  ]
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Dynamic("during initializing Python main: init_fs_encoding: failed to get the Python codec of the filesystem encoding")', src/main.rs:11:59
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fn main() {
    let mut config = OxidizedPythonInterpreterConfig::default();
    config.interpreter_config.filesystem_encoding = Some("utf-8".to_string());
   #config.interpreter_config.python_path_env = Some("/Users/user/code/rust/test/test/venv/bin/python".to_string());

    // Initialize the Python interpreter
    let gil = pyembed::MainPythonInterpreter::new(config).unwrap();
    gil.with_gil(|py| {
        let result = py.run("print('Hello, Python!')", None, None);
    });
}
TheMagicNacho commented 9 months ago

Can confirm that the following configs remove the "encoding" error.

OxidizedPythonInterpreterConfig::default();
            config.interpreter_config.isolated = Some(true);
            config.interpreter_config.filesystem_encoding = Some("utf-8".to_string());
            config.set_missing_path_configuration = false;
            config.interpreter_config.parse_argv = Some(false);
            config.argv = Some(vec!["python".into()]);
            config.interpreter_config.executable = Some("python".into());
indygreg commented 9 months ago

Failures in init_fs_encoding() are almost always a red herring: the actual failure is the Python standard library .py files cannot be located/loaded. This materializes as a failure to locate the module providing the encoding used by the filesystem and that causes Python's interpreter initialization to barf. (There is room to handle this error better in pyembed.)