Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Cython compiler generates a so with suffix 'cpython-35m-x86_64-linux-gnu.so'

Tags:

python

cython

#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

like image 379
q0987 Avatar asked May 15 '17 18:05

q0987


1 Answers

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.

like image 145
3 revs Avatar answered Oct 22 '22 16:10

3 revs