pythonwindows-subsystem-for-linuxcythonize

Cython on WSL-2: Getting "Operation not permitted" error from setup, but the .so works


I am working on WSL-2 on Win 11 with the latest versions of Python and Cython. I have a very small program m2_mod.pyx, which I would like to convert into an .so file to be included in the main Python program:

from libc.math cimport pow
cdef float hypotenuse(float x, y):
    return pow(x*x + y*y, 0.5)

cpdef print_hypotenuse(float x, y):
    print("%4.3f, %4.3f, %4.3f" %  (x, y, hypotenuse(x, y)) )

The setup file, m1_setup.py, is:

from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["m2_mod.pyx"]))

I run python3 m1_setup.py build_ext --inplace to create an extension module. The output is shown below. I have inserted new-lines to make it easy to read.

Compiling m1_mod.pyx because it changed.

[1/1] Cythonizing m1_mod.pyx

/usr/local/lib/python3.10/dist-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /mnt/c/USR/Programming/Python/WI - Lic/wsl/use_cython/m1_mod.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)

running build_ext
building 'm1_mod' extension
creating build
creating build/temp.linux-x86_64-3.10

x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.10 -c m1_mod.c -o build/temp.linux-x86_64-3.10/m1_mod.o

creating build/lib.linux-x86_64-3.10

x86_64-linux-gnu-gcc -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -g -fwrapv -O2 -Wl,-Bsymbolic-functions -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.10/m1_mod.o -o build/lib.linux-x86_64-3.10/m1_mod.cpython-310-x86_64-linux-gnu.so

copying build/lib.linux-x86_64-3.10/m1_mod.cpython-310-x86_64-linux-gnu.so ->
error: [Errno 1] Operation not permitted

Even though the process ends in an error message, the .c and .so files have been created. The .so file can be "included" in a Python program, and it works fine. My question is what does the error message mean and how to eliminate it? I have done extensive web search to no avail and would greatly appreciate any guidance. I am new to WSL, though.


Solution

  • I finally found the answer as to why the error message is arising and would like to post it in case others run into the same conundrum.

    In WSL-2 (Dec 2024), wsl command on the Command Line starts WSL as the root user, and all files are owned by root. I had changed the user using su <myname> and received the error message. If do not change user to <myname> the problem goes away.

    One can unmount /mnt/c, and remount it with <myname>. That will also resolve the problem.