17

I'm a Matlab and C++ user, and have recently discovered python (spyder) as a possible replacement for both. One of the main benefits I thought python had was the ability to work in interpreter mode, and then seamlessly translate it into fast compiled code once I'm satisfied with the result. The interpreted environment is great for prototyping, analyzing data while stopped at a breakpoint, throwing plots and images all around, etc.

I started looking into Cython, and I don't fully understand the programming flow. Lets say you have a .py code you'd like to speed up - Do you have to write a .pyx file from scratch? Can you run a .pyx file in interpreted mode as if it were a regular .py file (before compiling)? How do you debug the code in a .pyx file?

1 Answer 1

2

I don't have too much experience with Cython, but judging from this entry in their documentation, the recommended workflow is to have a setup.py file with the following lines:

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

setup(name='Hello world app', ext_modules=cythonize("hello.pyx"))

Here hello.pyx is just an example file, you will have to replace the string to reference your Python script.

Afterwards you will be able to call

python setup.py build_ext --inplace

which will compile your code and leave you with a new file. Now, as long as that file is in the same directory, you can easily import what you defined in your file, just like with any other module. E.g., suppose you compiled a file hello.pyx with the function f, you could write:

from hello import f

and then proceed to use f.


Now, regarding your other questions. .pyx seems to just indicate, that this should be Cython code, there is no real difference. Using the method with a setup.py script as described above, you could also reference a file with the ending .py. However, Python won't allow you to import from .pyx files, only from the files created after compiling.

As to how you would debug code in a .pyx file, I don't have enough information on that, though you could probably just debug the non-compiled file like a .py file.

1
  • 2
    This is not an answer to the question, merely a summary of the introductory tutorial for Cython. The question asks, importantly, "Can you run a .pyx file in interpreted mode as if it were a regular .py file (before compiling)?". While what you say is true, that you can't directly import a pyx, the question is looking for a method of conditionally avoiding the compilation step for a pyx module -- not directly importing the non-Python text.
    – Wasabi Fan
    Apr 13, 2022 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.