9
#setup.py    
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext}, 
    ext_modules = [Extension("module_name", ["xxxx.pyx", './yyyyy.c'],
                    language='c',
                    extra_compile_args = ['-std=gnu11'],
                    extra_link_args=['-std=gnu11']                 
                    )]
)


$ python --version
Python 3.5.1
$ python -c "import cython; print(cython.__version__);"
0.25.2
$ python setup.py build_ext -i

Question> Why Cython generates the module name as module_name.cpython-35m-x86_64-linux-gnu.so instead of module_name.so?

Thank you

q0987
  • 34,938
  • 69
  • 242
  • 387
  • 1
    Earlier question about the name rules: http://stackoverflow.com/questions/38523941/change-cythons-naming-rules-for-so-files. You don't need to use the full name when importing. The full name identifies the compilation context, which may help when debugging or porting code. – hpaulj May 15 '17 at 19:08
  • @hpaulj, thank you for your message and I have confirmed that it works without the suffix. – q0987 May 15 '17 at 19:15
  • 1
    This might also be worth reading https://www.python.org/dev/peps/pep-3149/ to understand why Python chooses to do this (it isn't specifically a Cython thing) – DavidW May 15 '17 at 20:40

1 Answers1

6

A full description is available in PEP 3149. In short: this is a change that applies from Python 3.2 onwards. A version identifier is deliberately added to the filename; filenames with this version identifier are used in preference to those without (but a plain .so or .pyd file without the version identifier will be used if no other option is available to import).

There are two main advantages to adding information to the .so or .pyd file about what version of Python it was compiled against:

  1. It is possible to have .so/.pyd files compatible with multiple versions of Python all compiled to the same directory, making it easier to use or distribute libraries for multiple versions of Python.
  2. It protects you from accidentally importing a module compiled for the wrong version of Python. This is a good thing, since the ABI isn't 100% compatible between Python versions (but is likely to be mostly compatible, which will give you occasional and confusing bugs)

The change to the file name affects all compiled Python modules, not just Cython.

This question describes how to change the identifier that's added - there is rarely a good reason for doing so, however.

DavidW
  • 29,336
  • 6
  • 55
  • 86