Re: [cython-users] How do you run Cython in Windows?

1,018 views
Skip to first unread message

Warren Weckesser

unread,
Aug 16, 2012, 9:09:25 PM8/16/12
to cython...@googlegroups.com


On Thu, Aug 16, 2012 at 3:31 PM, Canon <mpark...@gmail.com> wrote:
How do I run or compile Cython? So far I have been unable to do either.

Some background:
Windows XP, 32-bit machine
downloaded Python (x,y) 2.7.2.3  (http://code.google.com/p/pythonxy/wiki/Downloads)
I checked the boxes in the python package that would included Cython 0.16 and the MinGW compiler in the installation

I try to follow the steps in this Cython tutorial (http://docs.cython.org/src/userguide/tutorial.html) but get errors. The tutorial does not specify where these steps should be performed, maybe it doesn't matter?

1) I open up IDLE and write:
             print "hello world"
2) I save this program as helloworld.pyx, and then close IDLE
3) I reopen IDLE and type the following lines:

>>> from distutils.core import setup
>>> from distutils.extension import Extension
>>> from Cython.Distutils import build_ext
>>> setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("helloworld",["helloworld.pyx"])]
)

4) after pressing enter I get the following error:

Traceback (most recent call last):
  File "<pyshell#6>", line 3, in <module>
    ext_modules = [Extension("helloworld",["helloworld.pyx"])]
  File "C:\Python27\lib\distutils\core.py", line 140, in setup
    raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg
SystemExit: usage:  [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or:  --help [cmd1 cmd2 ...]
   or:  --help-commands
   or:  cmd --help

error: no commands supplied



The tutorial tells you to create a file called setup.py containing:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("helloworld", ["helloworld.pyx"])]
)

and then, in a terminal (i.e. a DOS prompt, in the directory where setup.py is saved), run the command

> python setup.py build_ext --inplace


Try that, and if you don't get any errors, you can continue with the tutorial.

Warren



5) If I go onto the next step and type:

$ python setup.py build_ext --inplace

Then I get a syntax error on the dollar sign



What is going on? I get the same errors if I do steps 3-5 in the cmd.exe Shell.


An alternative method to skip steps 3-5 is to type:
>>> import pyximport; pyximport.install()
>>> import helloworld

(I do this in IDLE)

and the result is a long list of errors.


What is going on???




Canon

unread,
Aug 17, 2012, 2:49:03 AM8/17/12
to cython...@googlegroups.com
I tried it, and it works. YES!!! lol. Thanks for highlighting those crucial details, it was a big help.


If anyone is curious or ran into problems later on, here's 2 points that may be helpful:

1) If you try to run your Cython program, and get an error that says: Unable to find vcvarsall.bat

then you need to set minigw as the compiler, the following code should work:

python setup.py build_ext --inplace --compiler=mingw32

2) The Cython tutorial I referenced earlier said you should see a .dll file after the Cython program compiles in Windows. But chances are you will only see a .pyd file in your directory, and no .dll file. That is fine, and the .pyd file can serve as the .dll file. Just make sure the .pyd file is accessible if your trying to import the cython program into IDLE or the python interpreter. Otherwise you will get the error: no module named whatever

#NoobProblems 

Warren Weckesser

unread,
Aug 17, 2012, 1:33:20 PM8/17/12
to cython...@googlegroups.com
On Fri, Aug 17, 2012 at 1:49 AM, Canon <mpark...@gmail.com> wrote:
I tried it, and it works. YES!!! lol. Thanks for highlighting those crucial details, it was a big help.



Great.

And thank *you* for following up with the additional details; those could help the next person getting started with Cython in Windows.

Warren


 
Reply all
Reply to author
Forward
0 new messages