pyo3: ModuleNotFoundError: No module named 'supermodule.submodule'; 'supermodule' is not a package

šŸ› Bug Reports

I am working for writing Python extension, but facing import submodule error.

ModuleNotFoundError: No module named 'supermodule.submodule'; 'supermodule' is not a package

What do I expected

Import submodule like usual Python package.

šŸŒ Environment

$ uname -a
Linux xxxxxx 4.19.84-microsoft-standard #1 SMP Wed Nov 13 11:44:37 UTC 2019 x86_64 GNU/Linux
  • Your python version:
$ python -V
Python 3.8.1
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?:
$ pyenv virtualenv system xxxxxx
  • Your rust version (rustc --version):
rustc 1.43.0-nightly (c9290dcee 2020-02-04)
  • Are you using the latest pyo3 version? Have you tried using latest master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")? Yes.

šŸ’„ Reproducing

Please provide a minimal working example. This means both the rust code and the python.

Please also write what exact flags are required to reproduce your results. src/lib.rs

use pyo3::prelude::*;
use pyo3::{wrap_pyfunction, wrap_pymodule};
use pyo3::types::IntoPyDict;

#[pyfunction]
fn subfunction() -> String {
    "Subfunction".to_string()
}

#[pymodule]
fn submodule(_py: Python, module: &PyModule) -> PyResult<()> {
    module.add_wrapped(wrap_pyfunction!(subfunction))?;
    Ok(())
}

#[pymodule]
fn supermodule(_py: Python, module: &PyModule) -> PyResult<()> {
    module.add_wrapped(wrap_pymodule!(submodule))?;
    Ok(())
}

Cargo.toml

[package]
name = "supermodule"
version = "0.1.0"
authors = ["Tang Ziya <tcztzy@gmail.com>"]
edition = "2018"

[package.metadata.maturin]
classifier = [
    "Development Status :: 3 - Alpha",
    "Programming Language :: Python",
    "Programming Language :: Rust",
]

[lib]
name = "supermodule"
crate-type = ["cdylib", "rlib"]

[features]
default = []

[dependencies]
pyo3 = { git = "https://github.com/PyO3/pyo3", features = ["extension-module"] }

[dev-dependencies]
$ maturin develop
$ python -c "from supermodule.submodule import subfunction"
ModuleNotFoundError: No module named 'supermodule.submodule'; 'supermodule' is not a package

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 15
  • Comments: 23 (9 by maintainers)

Most upvoted comments

This is unfortunately a flaw in the way Python imports modules from native extensions. I think if you run import supermodule first, then from supermodule.submodule import subfunction will succeed.

For the record, an alternative to the py.run! solution is:

#[pymodule]
fn supermodule(py: Python, m: &PyModule) -> PyResult<()> {
    module.add_wrapped(wrap_pymodule!(submodule))?;

    py.import("sys")?
        .getattr("modules")?
        .set_item("supermodule.submodule", submodule)?;

    Ok(())
}

You can fix this in a somewhat hacky way by adding the module to sys.modules manually. From your Rust code, this can look like:

#[pymodule]
fn supermodule(py: Python, module: &PyModule) -> PyResult<()> {
    module.add_wrapped(wrap_pymodule!(submodule))?;

    py.run("\
import sys
sys.modules['supermodule.submodule'] = submodule
    ", None, Some(module.dict()))?;

    Ok(())
}

The documentation claims that sys.modules is writable, so this approach should not break anytime soon. The only caveat is that while this works when executed by a Python interpreter, it does not work with static analysis tools like pylint.

After a lot of trial and error, I am fairly convinced that the only ā€œfully robustā€ solution that will also make static analysis tools happy is to have your entire module hierarchy represented in the file system. Whether you do this with Python files or with separate extension modules is up to you. I agree with the comments in PyO3/maturin#266 that we should look into how numpy, pandas, etc. handle this and see if there is a better way.

It would be great if in the meantime PyO3 could emit code to modify sys.modules automatically so from supermodule.submodule import ... would work out of the box.

Here is my solution that correctly sets __name__ for both submodule and the function:

#[pymodule]
fn supermodule(py: Python, m: &PyModule) -> PyResult<()> {
    let child_module = PyModule::new(py, "supermodule.submodule")?;
    submodule(py, child_module)?;
    m.add("submodule", child_module)?;
    py.import("sys")?.getattr("modules")?.set_item("supermodule.submodule", child_module)?;
    Ok(())
}

I’ve run into this same issue with version 0.8.5 on Mac and no combination of imports that I’ve tried has worked, including the above suggestion.

FYI: The workaround posted in https://github.com/PyO3/pyo3/issues/759#issuecomment-977835119 also works for the experimental declarative API (which is awesome, btw!) ā˜ŗļø

#[pymodule]
mod my_module {
    use super::*;

    #[pymodule]
    mod my_submodule {
        use super::*;

        /// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
        #[pymodule_init]
        fn init(m: &Bound<'_, PyModule>) -> PyResult<()> {
            Python::with_gil(|py| {
                py.import_bound("sys")?
                    .getattr("modules")?
                    .set_item("my_module.my_submodule", m)
            })
        }
    }
}

For the record, an alternative to the py.run! solution is:

#[pymodule]
fn supermodule(py: Python, m: &PyModule) -> PyResult<()> {
    module.add_wrapped(wrap_pymodule!(submodule))?;

    py.import("sys")?
        .getattr("modules")?
        .set_item("supermodule.submodule", submodule)?;

    Ok(())
}

The modern incantation to do this is something like:

#[pymodule]
fn supermodule(py: Python, m: &PyModule) -> PyResult<()> {
    let submodule = pyo3::wrap_pymodule!(submodule);
    m.add_wrapped(submodule)?;
    py.import("sys")?
        .getattr("modules")?
        .set_item("supermodule.submodule", submodule(py))?;
    Ok(())
}

A solution for this came up in https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021

We could try to support this pattern internally in PyO3 (or at least add documentation).

I might have a cleaner approach to getting submodules outlined here. In short, for Python 3.13t, it seems sufficient to:

  • Create all modules via PyImport_AddModule to automatically register them in the ModuleDict.
  • Set the __path__ attribute for packages.
  • Set the __module__ attribute for all classes attached to attributes.

This setup appears to work well with Python’s import machinery (i.e., from a import b works), though I may be missing some edge cases. I also have unit tests for pickling/unpickling simple Python classes, which pass. I’ll continue testing and report any gaps, but feedback from anyone with more CPython experience would be greatly appreciated.

Thank you very much! I didn’t know that there is a whole chapter in the pyo3 manual that is concerned with ide typing hints 😃

I got confused by auto completion working though the ide’s typing hints did not. As far as I understood, as I used jupyter, the auto completion got the information from the live session using the __doc__ etc properties of the python objects while the typing hints are a static analysis which did not work cause of the missing pyi files.

Pylance cannot see inside of native modules to understand their structure, you will need to create .pyi files to represent the module hierarchy.

@davidhewitt Thank you for pointing me there, that seems to be the exact problem I was having. In the meantime, I got something working after reading this in the documentation:

As we now specify our own package content, we have to provide the init.py file, so the folder is treated as a package and we can import things from it. We can always use the same content that Maturin creates for us if we do not specify a Python source folder. For PyO3 bindings it would be:

from .my_project import *

It’s not perfect, and I am repeating myself a lot, but it works and is sufficient for now. I sincerely appreciate your work on this project! It’s become an indispensable tool in my workflow

I’m encountering this same issue and when using the solution suggested in https://github.com/PyO3/pyo3/issues/759#issuecomment-1208179322 (via copy/paste).

After maturin develop and attempting import supermodule.submodule in a REPL I’m now getting:

thread '<unnamed>' panicked at 'failed to wrap pymodule: PyErr { type: <class 'ImportError'>, value: ImportError('PyO3 modules may only be initialized once per interpreter process'), traceback: None }', supermodule/src/lib.rs:432:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python3.9/site-packages/supermodule/__init__.py", line 1, in <module>
    from .supermodule import *
pyo3_runtime.PanicException: failed to wrap pymodule: PyErr { type: <class 'ImportError'>, value: ImportError('PyO3 modules may only be initialized once per interpreter process'), traceback: None }

I don’t see any issues in this repo on this particular error (multiple initialization) - any idea where I’ve gone wrong?

(I see from https://github.com/PyO3/pyo3/blob/7bdc504252a2f972ba3490c44249b202a4ce6180/guide/src/migration.md#each-pymodule-can-now-only-be-initialized-once-per-process that this is new behavior as of the 28 August release of 0.17 but it’s not clear from that writeup what ā€˜the ability to initialize a #[pymodule] more than once in the same process’ means - i.e. I am just starting a REPL and importing the package one time to trigger the error).