Cython compilation appends text to filename, how to get rid of it?
Asked Answered
A

2

6

I'm working with cython on the Ubuntu platform. Everything works fine, except there is one thing that annoys me. When compiling a cython project to a .so file, the filename of the .pyx file is appended with "cpython-36m-x86_64-linux-gnu". For example, if I build "helloworld.pyx" the resulting .so file is called: "helloworld.cpython-36m-x86_64-linux-gnu.so". I however would just want it to be called "helloworld.so".

I thought the answer would be quite trivial, so I started googling, even after 30 minutes I could find nothing on the subject. Does anyone have any idea?

Here is my .pyx file:

print('hello world')

setup.py file:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("helloworld.pyx")
)

building the file:

python setup.py build_ext --inplace
Compiling helloworld.pyx because it changed.
[1/1] Cythonizing helloworld.pyx
running build_ext
building 'helloworld' extension
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/**/anaconda3/include/python3.6m -c helloworld.c -o build/temp.linux-x86_64-3.6/helloworld.o
gcc -pthread -shared -L/home/**/anaconda3/lib -Wl,-rpath=/home/ed/anaconda3/lib,--no-as-needed build/temp.linux-x86_64-3.6/helloworld.o -L/home/**/anaconda3/lib -lpython3.6m -o /home/**/new_project/helloworld.cpython-36m-x86_64-linux-gnu.so
Ambrosial answered 15/3, 2017 at 17:53 Comment(3)
Even with that extra text import helloworld works just fine. You could try editing the file name after creation, or make a copy. But I just let it be. It doesn't get in the way for me.Logorrhea
Changing the end of the gcc command from -o /home/paul/mypy/cython3/try_numpy/iterate.cpython-35m-i386-linux-gnu.so to -o ./iterate.so, changes the resulting .so name. But I can't find of way of specifying that -o option in the setup file.Logorrhea
Possible duplicate of Change Cython's naming rules for .so filesRanunculus
F
4

You cannot get rid of it, at least not automatically. PEP 3149 defines the tags to include in the file name of compiled modules: https://www.python.org/dev/peps/pep-3149/

The tag includes the Python implementation (here cpython), version (here 36 for 3.6), a flag for the compile-time options (m for using Python's memory allocator, this is by default). The platform tag x86_64-linux-gnu is not part of PEP 3149, I don't know where it is defined.

These changes are implemented in distutils and cython is not "to blame" :-)

The import name of the package is not affected by this file name.

Do you have any specific reason for not adhering to PEP 3149? You can replace the build process of the setup file by issuing manually the linker command, but this is less convenient.

Fanestil answered 16/3, 2017 at 10:2 Comment(0)
L
3

just change one line in /usr/lib/python3.6/disutils/command/build_ext.py at

    def get_ext_filename(self, ext_name):

        from distutils.sysconfig import get_config_var
        ext_path = ext_name.split('.')
        ext_suffix = get_config_var('EXT_SUFFIX') 
        return os.path.join(*ext_path) + ext_suffix

change ext_suffix = get_config_var('EXT_SUFFIX') to ext_suffix = ".so" or to ".pyd" on Windows

that's it, you don't have to worry about the output name ever again

Leggat answered 24/6, 2019 at 16:27 Comment(1)
And if you upgrade Python, you do that again? And if you build on multiple Python versions, you do that on each one? Patching a Python package is simply very bad practice. I am downvoting very rarely, but this time I did. How about simply setting the EXT_SUFFIX config variable and leaving the code unchanged?Venditti

© 2022 - 2024 — McMap. All rights reserved.