I see that PyCharm supports Cython.
I could always compile and run in terminal, but I'm wondering if there is a way to do this in PyCharm. In the link it says: "Compilation is done using external tools. The preferred build systems (Makefile, setup.py, etc.) should be configured as external tools." I'm wondering how to do this configuration. A small Hello World example in PyCharm using Cython would be much appreciated.
Thanks
Answering my own question here:
Let's say we have the function fib.pyx:
There are two ways to compile and run this
Use a setup file. Make the file setup.py:
The
ext_options
is included here to generate the html file with annotations. To run this file you have to go to Tools --> Run setup.py Task. Then type inbuild_ext
as task name and when prompted for Command Line input type--inplace
. The files fib.c, fib.o and the executable file fib.so is generated. The annotation file fib.html is also created.Now, the following code should work in any python file, for example main.py:
The much easier way to go is to use pyximport. No setup file is needed. Note that this can only be used if "your module doesn’t require any extra C libraries or a special build setup." The file main.py should now look like:
As far as I understand the same compilation of code takes place even though the fib.c, fib.o and fib.so files don't end up in the project folder. The fib.html code is not generated either, but this can be fixed by adding two lines to the main file. With the new lines main.py is now: