Nested Python C Extensions/Modules?

2024/10/11 4:26:53

How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so that it imported via "import mymodule.bar"?

(Sorry if this is poorly phrased, I wasn't sure what the proper term for it was.)

I tried the following in setup.py, but it doesn't seem to work:

from distutils.core import setup, Extensionsetup(name='mymodule',version='1.0',author='Me',ext_modules=[Extension('mymodule', ['mymodule-module.c']),Extension('bar', ['bar-module.c'])])

Edit

Thanks Alex. So this is what I ended up using:

from distutils.core import setup, ExtensionPACKAGE_NAME = 'mymodule'setup(name=PACKAGE_NAME,version='1.0',author='Me',packages=[PACKAGE_NAME],ext_package=PACKAGE_NAMEext_modules=[Extension('foo', ['mymodule-foo-module.c']),Extension('bar', ['mymodule-bar-module.c'])])

with of course a folder named "mymodule" containing __init__.py.

Answer

The instructions are here:

Extension('foo', ['src/foo1.c', 'src/foo2.c'])

describes an extension that lives in the root package, while

Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c'])

describes the same extension in the pkg package. The source files and resulting object code are identical in both cases; the only difference is where in the filesystem (and therefore where in Python’s namespace hierarchy) the resulting extension lives.

Remember, a package is always a directory (or zipfile) containing a module __init__. To create a module that's a package body, that module will be called __init__ and live under the package's directory (or zipfile). I've never done that in C; if it doesn't work to do it directly, name the module e.g. _init instead, and in __init__.py do from _init import * (one of the very few legitimate uses of from ... import *;-).

https://en.xdnf.cn/q/69814.html

Related Q&A

ImportError: No module named sysconfig--cant get pip working

Im really struggling with pip on a RedHat 6.9 system. Every time I tried to use pip, I got ImportError: No module named sysconfigI tried Googling for solutions. I dont have apt-get and cant seem to get…

Convert Dataframe to a Dictionary with List Values

Suppose I have a Dataframe df :Label1 Label2 Label3 key1 col1value1 col2value1 key2 col1value2 col2value2 key3 col1value3 col2value3dict1 = df.set_index(Label1).to_dic…

Efficiently count all the combinations of numbers having a sum close to 0

I have following pandas dataframe df column1 column2 list_numbers sublist_column x y [10,-6,1,-4] a b [1,3,7,-2] p q [6,2,-3,-3.…

What is the equivalent to iloc for dask dataframe?

I have a situation where I need to index a dask dataframe by location. I see that there is not an .iloc method available. Is there an alternative? Or am I required to use label-based indexing?For …

How to deal with limitations of inspect.getsource - or how to get ONLY the source of a function?

I have been playing with the inspect module from Pythons standard library. The following examples work just fine (assuming that inspect has been imported):def foo(x, y):return x - y print(inspect.getso…

Checking whether a function is decorated

I am trying to build a control structure in a class method that takes a function as input and has different behaviors if a function is decorated or not. Any ideas on how you would go about building a f…

How to keep the script run after plt.show() [duplicate]

This question already has answers here:Is there a way to detach matplotlib plots so that the computation can continue?(21 answers)Closed 6 years ago.After the plt.show() , I just want to continue. How…

python - Simulating else in dictionary switch statements

Im working on a project which used a load of If, Elif, Elif, ...Else structures, which I later changed for switch-like statements, as shown here and here.How would I go about adding a general "Hey…

Allow dynamic choice in Django ChoiceField

Im using Select2 in my application for creating tags-like select dropdowns. Users can select number of predefined tags or create a new tag.Relevant forms class part:all_tags = Tag.objects.values_list(i…

Row-wise unions in pandas groupby

I have a large data frame that looks like so (and is copy-pasteable with df=pd.read_clipboard(sep=\s\s+):user_nm month unique_ips shifted_ips halves quarters mo_pairs100118231 2 set(…