Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys.argv support: Consider call PySys_SetArgv on python initialization. #1241

Open
XUWeijiang opened this issue Oct 17, 2020 · 5 comments
Open
Assignees

Comments

@XUWeijiang
Copy link

XUWeijiang commented Oct 17, 2020

Currently if I use sys.argv in python code, it would complain: 'module' object has no attribute 'argv'
This is because python's argv is not initialized.

I hope PyO3 can provide an option to initialize argv in its initialization.

Repro Example

use pyo3::prelude::*;


fn main_(py: Python) -> PyResult<()> {
    let sys = py.import("sys")?;
    let argv: Vec<String> = sys.get("argv")?.extract()?;
    println!("{:?}", argv);
    Ok(())
}

fn main() -> Result<(), ()> {
    Python::with_gil(|py| {
        main_(py).map_err(|e| {
            e.print_and_set_sys_last_vars(py);
        })
    })
}

Output:

AttributeError: module 'sys' has no attribute 'argv'
Error: ()
@davidhewitt
Copy link
Member

I wonder if we should be using the python initialization configuration from Python 3.8 onwards? I'll have a play with this this morning.

@davidhewitt
Copy link
Member

@XUWeijiang I took a look this morning and I can't reproduce this issue. On my system the snippet you provide above works fine, and sys.argv exists.

Can you please provide detail on your OS and Python version?

@XUWeijiang
Copy link
Author

@davidhewitt I am using ubuntu 18.04 on wsl, and python3.6.

@davidhewitt
Copy link
Member

So I can confirm that on Python 3.6 this issue reproduces on my system.

On Python 3.7 and up sys.argv exists but is empty, so the original reported issue no longer applies.

However, I'm unsure whether pyo3 should be setting sys.argv unconditionally. Perhaps we can add an API to do this.

@davidhewitt
Copy link
Member

Note for the future - the folks in pybind11 look like they might merge something similar to this: pybind/pybind11#2341

The design of their implementation would take arguments passed from c++ main, which isn't quite how Rust does it. There's also a question of whether we would ever want something like this for the auto-initialize feature.

Additionally, it's clearly less of a problem once we move away from supporting Python 3.6, because after that point accessing sys.argv stops being an AttributeError.

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

No branches or pull requests

2 participants