0

I have a python3 distribution that has one package and also a C extension. I would like to be able to specify the version of the C extension during imports in the tell.py script, but cannot seem to figure it out.

My directory structure looks like:

funnyjoke/
├── tell.py
├── funnyjoke
    └── __init__.py
└── src
    └── fastjoke.c

I can import the specific version of the funnyjoke module perfectly fine:

__requires__= 'funnyjoke==0.3'
import pkg_resources
pkg_resources.require("funnyjoke==0.3")
import funnyjoke
funnyjoke.tell()

But the fastjoke extension does not get the version from the setup.py script

__requires__= 'fastjoke==0.3'
import pkg_resources
pkg_resources.require("fastjoke==0.3")
import fastjoke
fastjoke.tell()

and gives the error:

pkg_resources.DistributionNotFound: The 'fastjoke==0.3' distribution was not found and is required by the application

Is it possible to set/attach a version to the C extension?


setup.py

from setuptools import setup, Extension, find_packages

setup(name='funnyjoke',
      version='0.3',
      description='The funniest joke in the world',
      author='Flying Circus',
      license='MIT',
      #packages=['funnyjoke'],
      packages=find_packages(),
      scripts=['tell.py'],
      ext_modules = [Extension('fastjoke', ['src/fastjoke.c'])],
      zip_safe=False)

All files can also be retrieved via pip3 install funnyjoke

deprekate
  • 446
  • 3
  • 9
  • 1
    hi, not sure, perhaps append it to the shared library prefix? – IronMan Dec 05 '20 at 01:35
  • I had the same idea, and tried all sorts of attempts/hacks at adding a version to the shared library. There were all sorts of pitfalls, like `distutils` treating periods as [folder separators](https://github.com/python/cpython/blob/06afac6c5740bb81d2b7ab9639d2b08cccf77d33/Lib/distutils/command/build_ext.py#L646-L648), and it didn't like underscores or dashes either. I even tried post install renaming the shared library, but it only gave more rabbit holes of errors. – deprekate Dec 08 '20 at 01:47

1 Answers1

0

I found out that I needed to add the root package name (funnyjoke) to the Extension name (fastjoke) in the setup.py via:

...
ext_modules = [Extension('funnyjoke.fastjoke', ['src/fastjoke.c'])],
...

I could then import/require the root package name and then import the associated C extension:

>>> __requires__= 'funnyjoke==0.4' ; import pkg_resources ; pkg_resources.require("funnyjoke==0.4")
>>> import funnyjoke
>>> funnyjoke.tell()
Wenn ist das Nunstück git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput.
>>> import funnyjoke.fastjoke
>>> funnyjoke.fastjoke.tell()
Wenn ist das Nunstück git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput.

And likewise (I had to quit the IDE between, since I cannot unload the 0.4 version):

>>> __requires__= 'funnyjoke==0.5' ; import pkg_resources ; pkg_resources.require("funnyjoke==0.5")
>>> import funnyjoke
>>> funnyjoke.tell()
A man walks into a bar
>>> import funnyjoke.fastjoke
>>> funnyjoke.fastjoke.tell()
A man walks into a bar

deprekate
  • 446
  • 3
  • 9