In this post, we are going to explore how to set up the development environment for Cython on your system.
First we will see the various ways to compile Cython code so that it can be run by Python. There are several options to compile Cython.
Cython code can be compiled and run interactively from an IPython interpreter.
- It can also be compiled automatically at import time.
- It can be separately compiled by build tools like Python’s distutils.
- It can be integrated into standard build systems such as make, CMake, or SCons.
But in all cases, each method passes Cython code through two compilation stages to generate a compiled module that Python can import and use. The first stage is handled by the cython compiler, which transforms Cython source into optimized and platform- independent C or C++ code. The second stage compiles the generated C or C++ source into a shared library with a standard C or C++ compiler. It is a shared-object file with a .so extension on Linux or Mac OS X, and is a dynamic library with a .pyd extension on Windows.
Now lets understand how we can install cython on various platforms. Many scientific Python distributions, such as Anaconda, Enthought Canopy, and Sage, bundle Cython, and no setup is needed. However if your distribution ships a version of Cython which is too old you may need to install an updated version. Unlike most Python software, Cython requires a C compiler to be present on the system. We can install a C compiler on different operating systems in different ways.
The GNU C Compiler (GCC) is usually present, or easily available through the package system. On Ubuntu or Debian, for instance, the command
sudo apt-get install build-essential
will fetch everything you need.
On Mac OS X
To retrieve GCC, one option is Apple’s XCode, which can be retrieved from the Mac OS X’s developer site
One another way to install that is by using Homebrew using the command brew install gcc
Go to MinGW official site and click on 'Downloads' on the left panel. You'll be redirected to the site which hosts the files. Look for mingw-get-setup.exe for downloading. Download it and launch the installer. Accept the terms and move on. You'll now see that the installer is connecting to the Internet and downloading a lot of tiny and small files. Wait till it ends. Right when it ends (which won't take long), you'll be presented with a window with the title MinGW Installation Manager. You should be in the 'Basic Setup' tab by default when it launches. If not, click on Basic Setup. Out of the numerous checkboxes presented to you on the right side,tick "mingw32-gcc-g++-bin". If you are prompted with a menu, click on Mark for Install.Then on the top left corner click on Installation > Apply Changes. And wait while it downloads a billion files and installs them.
MinGW Installation Manager
- Now you gotta edit your "Environment Variables" as well, so that gcc works in cmd no matter the file location.
- For that go to Windows Explorer > Right click on This PC > Properties > Advanced system settings > Environment Variables or you could just search for "Environment Variables" in Windows Search
- At the bottom "System Variables" panel, look for a Variable named "Path" and double click on it. Some systems show a good UI for adding a New Path easily (by clicking New), else you just need to add ; at the end and add the following path C:\MinGW\bin (This is assuming you didn't manually change any installation paths and went with just clicking 'Next' during installation)
- Click on OK, and OK and close the other windows. Open a Command Prompt Terminal and try typing gcc --version and press Enter.
If you get something like
gcc (MinGW.org GCC Build-2) 9.2.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE
gcc has been successfully installed in your PC. Enjoy!Now you have a C compiler GCC installed on your system whatever OS you are using, next thing is the installation of Cython.
The simplest way of installing Cython is by using pip, so type the command as:
pip install Cython
One antoher option to install the newest Cython release, it can be downloaded from site. .Unpack the tarball or zip file, enter the directory, and then run:
python setup.py install
For one-time builds, e.g. for CI/testing, on platforms that are not covered by one of the wheel packages provide d on PyPI, it is substantially faster than a full source build to install an uncompiled (slower) version of Cython with
pip install Cython --install-option="--no-cython-compile"
Once we have a C compiler and the cython in place, we are ready to follow along with the distutils and pyximport.
Additionally, we will need to have IPython installed to use Cython from within Python. The packaged distributions include IPython, or we can use pip to install it.