firefoxmetzger / mini-extension Goto Github PK
View Code? Open in Web Editor NEWDemos of how to use Poetry to build various C/C++ extensions for Python.
Demos of how to use Poetry to build various C/C++ extensions for Python.
Hi @FirefoxMetzger , thanks for this example repo, I'm approaching building with poetry and it's very useful!
I'm focusing only on shared_library example, but probably those things I'm reporting can be found also on other examples.
Just some thing to improve:
print(hello_world().decode())
because it returns c_char_p
hello_world.py
a little more os-independent, the following implementation run in windows and in linuximport platform
import os
import re
from ctypes import cdll, c_char_p
from pathlib import Path
# load the library using ctypes
_extension = ''
if platform.system() == ' Windows':
_extension = 'dll'
if platform.system() == 'Linux':
_extension = 'so'
_base_path = Path(__file__).parent.absolute().joinpath('lib')
# linux do put 'lib' as name prefix and can put the version after the extension (e.g. libhello_library.so.0.1.0 )
res = [f for f in os.listdir(_base_path) if re.search(rf'[a-z]*hello_library[a_z]*.{_extension}[.0-9]*', f)]
# just one library should be into the folder, otherwise exception is raised
lib_shared_hello = cdll.LoadLibrary(str(_base_path.joinpath(res[0])))
# expose the hello function as hello_world in python
# Note: returns a bytes object, not a python string
hello_world = lib_shared_hello.hello
hello_world.restype = c_char_p
Thank you to have spotted the way to build C-project into poetry-managed project!
Hey there @FirefoxMetzger !
Thanks a lot for you example of how to compile c-extensions when working with poetry
!
I managed to compile my C-extension locally. By locally I mean while working on the package itself.
Both poetry build
and poetry install
are compiling the objects to the desired place.
However when I try to use said package as a dependency to another package nothing gets compiled.
I tried with your shared_library_with_ctypes
that I listed as one of my dependency in my pyproject..toml
:
[tool.poetry.dependencies]
python = "3.10.*"
extension = { git = "[email protected]:FirefoxMetzger/mini-extension.git", subdirectory="shared_library_with_ctypes"}
After doing poetry install
I get the error: FileNotFoundError
for the extension/lib
directory.
Am I missing something ? Any help would really be welcome!
Thanks!
This is the warning currently in place
https://github.com/pypa/setuptools/blob/v69.5.1/setuptools/dep_util.py#L5-L16.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.