Skip to content

RUSTPYTHONPATH is not set when using Settings::default() without clap #4988

Closed

Description

@euanmacinnes

Summary

I've manually set the RUSTPYTHONPATH in the system environment variable, Rust sees the correct folder location prior to calling the
vm::Interpreter::without_stdlib(Default::default()).enter(|vm| { ..

and yet, the following error appears:

encodings initialization failed. Only utf-8 encoding will be supported.
ModuleNotFoundError: No module named 'encodings'

The above exception was the direct cause of the following exception:

RuntimeError: Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have access to a consistent external environment (e.g. if you're embedding rustpython in another application), try enabling the freeze-stdlib feature

Expected

No message

Actual

encodings initialization failed. Only utf-8 encoding will be supported.
ModuleNotFoundError: No module named 'encodings'

The above exception was the direct cause of the following exception:

RuntimeError: Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have access to a consistent external environment (e.g. if you're embedding rustpython in another application), try enabling the freeze-stdlib feature

Python Documentation

Activity

  1. fanninpm commented on May 23, 2023

    @fanninpm
    Contributor

    Does the problem persist after running cargo clean?

  2. euanmacinnes commented on May 23, 2023

    @euanmacinnes
    Author

    Yes, it does

  3. euanmacinnes commented on May 23, 2023

    @euanmacinnes
    Author

    If it's helpful, the dependencies tree is:

    rustpython = { git="https://github.com/RustPython/RustPython", features = ['jit']}

  4. fanninpm commented on May 23, 2023

    @fanninpm
    Contributor

    You probably need to depend on the rustpython-vm crate.

  5. euanmacinnes commented on May 23, 2023

    @euanmacinnes
    Author

    Aren't the crates yanked? 3+ years old...

  6. fanninpm commented on May 23, 2023

    @fanninpm
    Contributor

    Try this:

    rustpython-vm = { git = "https://github.com/RustPython/RustPython", features = ['jit'] }
  7. fanninpm commented on May 23, 2023

    @fanninpm
    Contributor

    @youknowone Does this have anything to do with the parser migration?

  8. euanmacinnes commented on May 24, 2023

    @euanmacinnes
    Author
  9. fanninpm commented on May 24, 2023

    @fanninpm
    Contributor

    One last troubleshooting step: does the problem persist after deleting Cargo.lock?

  10. youknowone commented on May 24, 2023

    @youknowone
    Member

    @euanmacinnes Are you using rustpython as dependency of your project? or directly using RustPython repository?

  11. youknowone commented on May 24, 2023

    @youknowone
    Member

    The -vm is in the same git repo,

    I think using RustPython repository by that part. Then Cargo.lock will not help.

  12. euanmacinnes commented on May 24, 2023

    @euanmacinnes
    Author
  13. youknowone commented on May 24, 2023

    @youknowone
    Member

    Oh, wow. It was totally broken for Setting::default().
    RUSTPYTOHNPATH is only loaded when opts_with_clap is used.

  14. changed the title [-]RUSTPYTHONPATH is set in WINDOWS to the correct path, but there's still an error about missing encodings.[/-] [+]RUSTPYTHONPATH is not set when using `Settings::default()` without clap[/+] on May 24, 2023
  15. youknowone commented on May 24, 2023

    @youknowone
    Member

    Hmm.. the behavior seems reasonable. It doesn't automatically load PYTHONPATH when user doesn't want.
    But the the message seems very confusing.

    @euanmacinnes try this:

    let mut settings = Settings::default();
    settings.path_list.push(<the path  you want to put>);
    vm::Interpreter::without_stdlib(settings).enter(|vm| { ..
    
  16. euanmacinnes commented on May 29, 2023

    @euanmacinnes
    Author

    Ok nice! That works! Thanks!

  17. youknowone commented on Jun 1, 2023

    @youknowone
    Member

    2 tasks are expected to resolve this problem.

    • Fix error message to add path to settings.path_list and suggest RUSTPYTHONPATH as one of the way to set it when running interpreter binary.
      • It can be even better by checking settings.path_list contains PYTHONPATH or RUSTPYTHONPATH and generate different message. (optional)
    • Add fn with_path(self, path: ...) -> Self to Settings for convenience method.

    Both tasks will be easy tasks.

  18. hbina commented on Feb 15, 2025

    @hbina
    Contributor

    Is this still a problem?

  19. added a commit that references this issue on Oct 1, 2026
    c12cd7f
  20. youknowone commented on Oct 6, 2026

    @youknowone
    Member

    Settings::default() still leaves path_list empty on purpose, so a process-level RUSTPYTHONPATH is not applied to an embedded Interpreter::without_stdlib. That is the intended embed API.

    The two follow-ups listed on this issue are in tree:

    • Settings::with_path (crates/vm/src/vm/setting.rs)
    • encodings-import failure text now says when the env var is set but was not copied into Settings / PyConfig::paths::module_search_paths, and tells embedders to build a custom Settings

    Closing as resolved.

    — commented by Grok 4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-vmArea: virtual machineC-bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions