cythonrun: run a .pyx file

369 views
Skip to first unread message

Brent Pedersen

unread,
Nov 3, 2010, 2:42:45 PM11/3/10
to cython...@googlegroups.com
hi,
i cobbled together a script that uses distutils to run a .pyx file
from the command-line as simply as possible.
it's pretty hacky but it might be useful.

the full code is here:
https://gist.github.com/661480

simplest use is:

$ ./cythonrun some.pyx

which will use setup tools to build the .so, then import the module
and run a main() if it exists.
the rectangle example in cython docs looks like:

$ ./cythonrun --cpp -lstdc++ rectangle.pyx

which will find any .cpp files in the same dir in rectangle.pyx and
include them as source files. they can also be specified explicitly
as:

$ ./cythonrun --cpp -lstdc++ rectangle.pyx Rectangle.cpp

other options:

--time will time the import and running of main()
-v will show the output from distutils setup()
-i/-I can be used to specify includes.

it re-cythonizes and rebuilds every time even if it doesn't need to.
i'm glad to hear any feedback or ways to make it better.

-brent

Stefan Behnel

unread,
Nov 3, 2010, 2:50:02 PM11/3/10
to cython...@googlegroups.com
Brent Pedersen, 03.11.2010 19:42:

> i cobbled together a script that uses distutils to run a .pyx file
> from the command-line as simply as possible.

Nice.


> the full code is here:
> https://gist.github.com/661480
>
> simplest use is:
>
> $ ./cythonrun some.pyx
>
> which will use setup tools to build the .so, then import the module
> and run a main() if it exists.

Have you looked at the embedding support? If you run Cython with the
--embed option, it will generate a C file with a main() function that
initialises and executes the CPython interpreter and then runs the module
main code (i.e. the "if __name__ == '__main__'" trick works).

I think that's better than calling an explicit main() method.

Stefan

Brent Pedersen

unread,
Nov 3, 2010, 3:04:17 PM11/3/10
to cython...@googlegroups.com

ah, cool. how does one tell cython to --embed inside setup() ?


> Stefan
>

Brent Pedersen

unread,
Nov 3, 2010, 3:15:40 PM11/3/10
to cython...@googlegroups.com

i tried:
ext.pyrex_directives = {'embed': True}

but it doesn't seem to work.


>
>> Stefan
>>
>

Stefan Behnel

unread,
Nov 3, 2010, 4:33:22 PM11/3/10
to cython...@googlegroups.com
Brent Pedersen, 03.11.2010 20:04:

> On Wed, Nov 3, 2010 at 11:50 AM, Stefan Behnel wrote:
>> Brent Pedersen, 03.11.2010 19:42:
>>> i cobbled together a script that uses distutils to run a .pyx file
>>> from the command-line as simply as possible.
>> >
>>> the full code is here:
>>> https://gist.github.com/661480
>>>
>>> simplest use is:
>>>
>>> $ ./cythonrun some.pyx
>>>
>>> which will use setup tools to build the .so, then import the module
>>> and run a main() if it exists.
>>
>> Have you looked at the embedding support? If you run Cython with the --embed
>> option, it will generate a C file with a main() function that initialises
>> and executes the CPython interpreter and then runs the module main code
>> (i.e. the "if __name__ == '__main__'" trick works).
>>
>> I think that's better than calling an explicit main() method.
>
> ah, cool. how does one tell cython to --embed inside setup() ?

Hmm, I don't think that would work. distutils isn't made to build programs,
it's meant to build Python modules. There is an example under Demos/embed,
though.

Stefan

Reply all
Reply to author
Forward
0 new messages