does importing a module more then once affects runtime? #3566
-
i am rewriting some of my modules in rust, and while creating them, i noticed i have a specific function that gets imported only for that function, since it is necessary for that function only, something like def function():
from foo import bar
# the rest of the code this isn't an issue in python, since runtime doesn't get affected if you import a module that has already been imported, but since pyo3 isn't exactly normal python, i am wondering if the same thing is true here, if i were to import a module inside a function, for example #[pyfunction]
fn function(){
Python::with_gil(|py|{
let foo = py.import("foo");
// the rest of the code
})
} would this affect the runtime speed by any way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Since our |
Beta Was this translation helpful? Give feedback.
Maybe I misunderstood your question: Are you asking about the effect of importing some module
foo
from your Rust code? Or are you asking about repeatedly importing your Rust-written extensionfoo
?If you are talking about repeatedly import a Rust extension than we prevented this until recently where we relaxed the check to prevent only usage in multiple sub-interpreters but made multiple imports a no-op, c.f. #3446
Hence, it will also not have any effect performance effect besides the cost of running the import itself.