sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
233 stars 47 forks source link

Option to Specify Path to .so #206

Closed 5had3z closed 9 months ago

5had3z commented 9 months ago

I've added an option to specify the path to directly import the a compiled pybind11 .so file so that this can be used without "installing" the library. This could be particularly useful for running pybind11-stubgen after compilation within setup.py.

pybind11-stubgen my_module --module-path build/my_module.cpython-310-x86_64-linux-gnu.so
# General gist of usage in setup.py
class CMakeBuild(build_ext):
    def build_extension(self, ext: CMakeExtension) -> None:
    ........
        subprocess.run(
            ["cmake", str(ext.source_dir), *cmake_args], cwd=build_temp, check=True
        )
        subprocess.run(
            ["cmake", "--build", ".", *build_args], cwd=build_temp, check=True
        )
        subprocess.run(
            [
                "pybind11-stubgen",
                "_sc2_replay_reader",
                f"-o={extdir}",
                f"--module-path={ext_fullpath}",
            ],
            check=True,
        )
sizmailov commented 9 months ago

This can be achieved by setting the PYTHONPATH env variable.

5had3z commented 9 months ago

Its a lot more convenient at least in setup.py to --module-path rather than deal with os.env things and an explicit flag is a more obvious thing to do