Building Python extensions with Canopy

To build Python extension modules, you will need to have the same (or functionally identical) C compiler that was used to compile Python for your platform (OS).

Windows

For users of Canopy 1.5.3 (released June 10, 2015), and above:

Canopy is built on a standard Python 2.7 environment. On Windows, standard Python 2.7 supports only specific C++ compilers (https://docs.python.org/2/using/windows.html#compiling-python-on-windows).

Microsoft has released the Visual C++ Compiler for Python 2.7. Starting with Canopy 1.5.3, Canopy users can use this compiler to build extensions following the instructions found on the Cython wiki:

Here is how to compile C extensions (including 64-bit extensions) using Microsoft Visual C++ Compiler for Python:

    1. Install Microsoft Visual C++ Compiler for Python

If you are using setuptools (v18+) in your setup.py file (recommended):

    1. Open a Canopy Command Prompt from Canopy's Tools menu or from the Windows Start menu
    2. You can then "cd X:yourpath" to navigate to the Python code which you are building, and build your C extensions by entering:

      python.exe setup.py build_ext --inplace --compiler=msvc

If you are using distutils in your setup.py file:

    1. Launch MSVC for Python command prompt

      (Start menu > Microsoft Visual C++ Compiler Package for Python 2.7 > Visual C++ 2008 [32- or 64-]bit Command Prompt)
    2. Enter the following commands:

      SET DISTUTILS_USE_SDK=1
      SET MSSdk=1
    3. If Canopy or EPD is not already your default Python, make it so by running the batch file activate.bat, which is located by default in this directory:

      • In Canopy User Python (standard Canopy installation):
           C:\Users\<username>\Appdata\Local\Enthought\Canopy\User\Scripts\
      • EPD:
          C:\Python27\Scripts\ 
    4. You can then "cd X:yourpath" to navigate to the Python code which you are building, then build your C extensions by entering:

      python.exe setup.py build_ext --inplace --compiler=msvc

 

For users of older versions of Canopy:

(Note: While these instructions can be made to work, we recommend updating Canopy and using the above method, which seems to be less error-prone)

You will need either the full version of Microsoft Visual Studio 2008 or the free Microsoft Windows SDK

You can get the Windows SDK for Python 2.7 as either a web installer or a DVD ISO (download `GRMSDKX_EN_DVD.iso`).

If you use the DVD .iso, the SDK is provided as a DVD .iso image. To install it, you'll need software to mount this as a virtual DVD.  Windows 8 provides this, but on Windows 7 you will need something like the following: 

http://www.microsoft.com/en-us/download/details.aspx?id=38780

Once you have installed the SDK, you need to start the SDK Command Prompt and set two environment variables before compiling your extensions.

By default the shell starts in at the installation path of the Windows SDK (C:\Program Files\Microsoft SDKs\Windows\v7.0. There, we have to to two things:

  1. Tell distutils / setuptools to use the Microsoft SDK compiler
  2. Tell the compiler to compile a 32 or 64 bit release and whether it should be a debug or a release build

Thus, we have to enter two commands

set DISTUTILS_USE_SDK=1
setenv /x64 /release

Assuming you haven't changed the Windows SDK installation path, this looks something like this

C:\Program Files\Microsoft SDKs\Windows\v7.0>set DISTUTILS_USE_SDK=1
C:\Program Files\Microsoft SDKs\Windows\v7.0>setenv /x64 /release

An important note, you may need to change x64 to x86 or /release to /debug and vice versa, depending on your system and project requirements.

Unsupported environments:

Any other version of Visual Studio post-2008 is not supported. Python 2 is built with the CRT9.0 which is included in Visual Studio 2008. Newer versions of the compiler use a new CRT (10 and higher), which should not be used with Python 2. For more information, please refer to: 

https://msdn.microsoft.com/en-us/library/ms235460.aspx

http://siomsystems.com/mixing-visual-studio-versions/

The MinGW stack is another solution that is very often used on Windows. On 32-bit, it tends to work pretty nicely for some trivial cases, but has the very same drawbacks as using a different version of Visual Studio, mentioned above. On 64-bit, MinGW is not entirely stable yet.

Mac OS X

Please check (using the Terminal application) whether you already have gcc installed, by typing "gcc" at the command prompt.  If you do not have gcc, you will need to download and install XCode from Apple: 

https://developer.apple.com/xcode/

and then install the "Command Line Tools" from within XCode (accessible from the XCode > Preferences window in the "Downloads" tab under "Components").

Note: If you are a user of OS X 10.9 or higher, please see the following article for important considerations regarding "gcc" provided by Xcode: 

https://support.enthought.com/entries/26184115-OS-X-GCC-Clang-and-Cython-in-10-9-Mavericks

Linux

You will need to use your package manager (for example "apt" or "yum") to install gcc if it is not already available.  Consult the documentation for your Linux distribution for details.

 

GLOSSARY:

SDK: Software Development Kit

CRT: C Runtime (Library)

Have more questions? Submit a request

Comments

Powered by Zendesk