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

print function does not work within rust #660

Closed
lorencarvalho opened this issue Nov 5, 2019 · 5 comments
Closed

print function does not work within rust #660

lorencarvalho opened this issue Nov 5, 2019 · 5 comments

Comments

@lorencarvalho
Copy link

🌍 Environment

  • Your operating system and version: macOS Mojave 10.14.6 (18G95)
  • Your python version: 3.7.4
  • How did you install python (e.g. apt or pyenv)? macOS installer via python.org
  • Did you use a virtualenv?: No, n/a as I'm calling Python from Rust.
  • Your rust version (rustc --version): rustc 1.39.0-nightly (17e73e801 2019-08-28)
  • Are you using the latest pyo3 version? Yes
  • Have you tried using latest master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")? yes

💥 Reproducing

use pyo3::prelude::*;
use pyo3::types::PyDict;

fn main() {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let locals = PyDict::new(py);
    py.run("foo = 'one'; print(foo)", None, Some(locals));
    py.run("print(\"hello python\"", None, None);
    println!("hello {}", locals.get_item("foo").unwrap());
}

Hello! It seems that calling print() in a Python snippet from within Rust does not work. I thought that perhaps Pyo3 did something with stdout/stderr file descriptors, but calling import code; code.interact() via a py_run! macro (surprisingly) does work, and everything seems mostly normal (save for sys.argv being an empty list and a few other expected oddities).

Obviously this isn't super high priority, but I figured I'd mention it :)

Thanks for such a cool project! Really excited to work with it.

@programmerjake
Copy link
Contributor

    py.run("print(\"hello python\"", None, None);

you're missing the closing parenthesis ) in the print function call.

didn't test if that fixes it.

@lorencarvalho
Copy link
Author

@programmerjake it doesn't, I just bungled copying my example code

@ehiggs
Copy link

ehiggs commented Nov 8, 2019

It works here.
Ubuntu 19.10, python3 installed using apt. No venv. Rust 1.40 nightly 2019-11-7. pyo3 0.8.2.

$ python3 --version
Python 3.7.5rc1
$ cat src/main.rs 
use pyo3::prelude::*;
use pyo3::types::PyDict;

fn main() {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let locals = PyDict::new(py);
    py.run("foo = 'one'; print(foo)", None, Some(locals)).expect("smash");
    py.run("print(\"hello python\")", None, None).expect("bang");
    println!("hello {}", locals.get_item("foo").unwrap());
}
$ ./target/debug/pyo3-test 
one
hello python
hello one

@programmerjake
Copy link
Contributor

maybe it would work by using print(..., flush=True)?

@davidhewitt
Copy link
Member

I think this was probably resolved by #943, so I'm going to close. If anyone encounters this and can provide another repro let's reopen.

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

No branches or pull requests

4 participants