Home > OS >  How to simply call a PYD from a wheel installed package?
How to simply call a PYD from a wheel installed package?

Time:06-20

I'm building a pyd file via pybind11. There's no python-only code in the project (not even in the final wheel). I'm packing this prebuilt pyd into a wheel package. It's all working well so far, but I've got one small issue. Every time I want to use the functions from the pyd file (which is actually the primary "source" of code) I have to type the following: from python_lib_name.pyd_name import pyd_exported_class example: from crusher.crusher import Shard. This looks pretty lame, is there any way to prevent this and let the developer simply use the from crusher import Shard syntax?

Also, I'm not sure what information to provide, obviously, I wouldn't like to throw the whole project at you unnecessarily. I'm glad to provide any details by editing.

CodePudding user response:

how about this: put your pyd in a folder and include a __init__.py that import the stuff from the pyd

my_project 
 |---my_pyd_lib
 |   |---my_pyd_lib.pyd
 |   |---__init__.py
 |---#the other stuff

and in that init just put

#__init__.py
from .my_pyd_lib import *

the dot in the import make it a relative import useful to make sure that import the thing in the same folder as that file, it also make sure you import your thing if it happens to share a name with some other package/module you happens to have installed

CodePudding user response:

One way to make a pyd file simply importable is to use the Python interpreter's -m flag. For example, if your file is called foo.pyd and is located in the current directory, you can import it by running the following command:

python -m foo

This will cause the Python interpreter to load the module from foo.pyd and then execute its contents as if they were written in a normal .py file. You can also use this technique to import modules that are located in different directories; just specify the path to the module as an argument to -m.

Another way to make a pyd file simply importable is to convert it into a normal .py file.

  • Related