Re: Using Visual C++
Lenard Lindstrom <[email protected]> Mon, 12 Jan 2009 11:47:55 -0800
| Newsgroups | gmane.comp.python.pyrex |
|---|---|
| Message-ID | <[email protected]> |
Navid Parvini wrote: > Dear All, > > I'm new in learning Pyrex, so I'm thankful for your help. > > I have written a code named first.pyx and then used "pyrex.py" to > created "first.c" file but now I want to know how can I create > "first.dll" file by using "Visual C++". > > Would you please help me? > > Thank you in advance. > > Regards, > Navid > > > Hi Navid, The easiest way to compile Pyrex extension modules is with Python's distutils package, a part of the standard library. On Windows, Visual C++ is used by default. To use distutils with Pyrex the Pyrex package should be installed. To install Pyrex run: python setup.py install from Pyrex's distribution directory. To build a project with distutils you need a setup.py file. I have attached a simple example Pyrex module and it's setup file. To compile and build the extension module do: python setup.py build_ext --inplace If all works well this should give you test.pyd, a Python extension module DLL. Here is a quick Python session to confirm it worked: >>> import test >>> dir(test) ['__builtins__', '__doc__', '__file__', '__name__', 'x'] >>> test.x() This is it. You can reuse the setup.py file by replacing "project = 'test'" with "project = 'first'". The Python documents give a more thorough explanation of setup.py and distutils in the top level document "Distributing Python Modules". Pyrex specifics are given in the Pyrex Language Overview under "Source Files and Compilation". Also: python setup.py build_ext --help will list command line options. A further note, matching compilers with Python versions is a bit involved on Windows. Python 2.3 used Visual C++ 6.0, Python's 2.4 and 2.5 use the 2003 toolkit, and Python 2.6 and 3.0 need Visual Studio 2008. Distutils refuses to compile a project if the wrong VC++ is installed. Lenard -- Lenard Lindstrom <[email protected]> _______________________________________________ Pyrex mailing list [email protected] http://lists.copyleft.no/mailman/listinfo/pyrex
test.pyx
(text/plain, 34 B)
def x():
print "This is it."
setup.py
(application/x-python, 278 B)
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext
project = 'test'
setup(
name = project,
ext_modules=[
Extension(project, [project + ".pyx"]),
],
cmdclass = {'build_ext': build_ext}
)