Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

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

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

Already on GitHub? # to your account

Module created automatically with submodule with the same name #3145

Closed
Cpyte-Engine-Developer opened this issue May 9, 2023 · 1 comment
Labels

Comments

@Cpyte-Engine-Developer
Copy link

Bug Description

I have cpyte_engine module. This submodule automatically created with cpyte_engine submodule, but I have one real submodule math_utils. This import work, but he should don't work:
>>> from cpyte_engine.cpyte_engine import math_utils
lib.rs:

mod entity;
mod game;
mod math;
mod rendering;
mod scene_graph;

use pyo3::prelude::*;

use entity::Entity;
use game::Game;
use math::{Quaternion, Vector2, Vector3};

/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
    Ok((a + b).to_string())
}

fn register_math_module(_py: Python<'_>, parent_module: &PyModule) -> PyResult<()> {
    let m = PyModule::new(_py, "math_utils")?;
    m.add_class::<Vector3>()?;
    m.add_class::<Vector2>()?;
    m.add_class::<Quaternion>()?;
    parent_module.add_submodule(m)?;
    Ok(())
}

/// A Python module implemented in Rust.
#[pymodule]
fn cpyte_engine(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    m.add_class::<Entity>()?;
    m.add_class::<Game>()?;
    register_math_module(_py, m)?;
    Ok(())
}

Steps to Reproduce

  1. git clone https://github.com/Cpyte-Engine-Developer/cpyte-engine.git
  2. cd cpyte-engine/
  3. python3 -m venv venv
  4. source venv/bin/activate
  5. pip3 install maturin
  6. maturin develop
  7. python3
  8. from cpyte_engine.cpyte_engine import math_utils

Backtrace

No response

Your operating system and version

LMDE 5

Your Python version (python --version)

Python 3.9

Your Rust version (rustc --version)

rustc 1.67.1 (d5a82bbd2 2023-02-07)

Your PyO3 version

0.18.3

How did you install python? Did you use a virtualenv?

Python was preinstalled. I used virtual env via venv library

Additional Info

No response

@mejrs
Copy link
Member

mejrs commented May 9, 2023

See the documentation of https://docs.rs/pyo3/latest/pyo3/types/struct.PyModule.html#method.add_submodule

Note that this doesn’t define a package, so this won’t allow Python code to directly import submodules by using from my_module import submodule. For more information, see #759 and #1517.

The tl;dr is that you need to mess with sys.modules. See the linked issues for more information.

@PyO3 PyO3 locked and limited conversation to collaborators May 9, 2023
@davidhewitt davidhewitt converted this issue into discussion #3147 May 9, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants