[PYTHON] How to set up and compile your Cython environment

Required environment

(Added on 2020.04.05) For Windows, the road to compiling is quite confusing. There are various hints on the Ichiou Cython official page, but ... it's tedious to read:

-Article to get a Python compilation environment: Recent versions seem to need something called the Windows SDK C / C ++ Compiler .. Basically you need a compiler for the version of the Windows SDK that Python was built on (see this Python wiki page (https://wiki.python.org/moin/WindowsCompilers) for more information). -Tips for installing with Anaconda etc.: There isn't much new information here, but just in case

Furthermore, in order to use the compiler from Cython, it is necessary to run Python (Cython) on a terminal emulator with the build environment set up correctly. The simplest is

  1. Make the python environment work with the standard Windows shell
  2. Start a command prompt of Visual Studio and run python setup.py there

But if you want to use Anaconda's virtual environment, it can be very confusing. During trial and error, where and what kind of environment variables should be set up so that they can be used on the anaconda prompt ...

Installation

Use pip and you're done right away. I think I didn't need administrator privileges ...

$ pip install cython

Check if it can be compiled

Copy the code from Cython: Basic tutorial as it is and check if it can be compiled:

  1. Create a development directory. Create files in this.
  2. Create a ".pyx" file. This is the source code.
  3. Create a "setup.py" file. This will be the build file and specify how it will be compiled.
  4. Open a terminal emulator in the development directory and run python setup.py build_ext --inplace. For Windows, you need to do it on the developer command prompt to use the required commands and include files.

When you run setup.py, you may not add the --inplace option, or it may be installed in the Python library (unconfirmed).

If the compilation is successful, setup.py will end with a "Finished" message. In that case, you can import the compiled file by doing `ʻimport `` on the python console.

On the other hand, if the compilation fails, a "Failed" message will appear. Go back a little before the output of the terminal emulator to see more details about the error. Most of the time, I think it's a mistake in the .pyx file or the include / link file is missing.

Add library header file

Excerpt from Stack Overflow Answer.

When using numpy from Cython, C header files of numpy library are required. It's easy to understand when compiling directly using gcc or the like, but the -I option cannot be used when compiling via setup.py.

In this case, use the `ʻExtensionobject of distutils``:

setup.py


from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=[
        Extension("my_module", ["my_module.c"],
                  include_dirs=[numpy.get_include()]),
    ],
)

The second argument in this case specifies a list of source files (absolute or relative path). List the include paths you want to add with the `ʻinclude_dirs`` keyword argument.

Or if you look at the Cython documentation (http://cython.readthedocs.io/en/latest/src/reference/compilation.html), the cythonize () itself has an option called `ʻinclude_dirs`` Apparently:

setup.py


from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = "My hello app",
    ext_modules = cythonize("src/*.pyx", include_path = [...]),
)

numpy provides a function called get_include (), so you can use it, and even native libraries can use pkg-config or whatever output.

Recommended Posts

How to set up and compile your Cython environment
How to set up a Python environment using pyenv
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to set up SVM using Optuna
How to set up and use OMC Log Analytics --Linux version -
How to set up the development environment of ev3dev [Windows version]
How to set up Random forest using Optuna
How to set up Random forest using Optuna
How to set up a local development server
How to set up public key authentication in ssh
How to set up a Google Colab environment with Coursera's advanced machine learning courses
How to set up a VPN gateway to establish a connection between Alibaba Cloud and AWS
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
How to use pyenv and pyenv-virtualenv in your own way
Overview of Python virtual environment and how to create it
How to set a shortcut to switch full-width and half-width with IBus
How to build a LAMP environment using Vagrant and VirtulBox Note
How to get and set the NTP server name by DHCP
How to install and use Tesseract-OCR
How to create your own Transform
How to use Serverless Framework & Python environment variables and manage stages
How to use jupyter notebook without polluting your environment with Docker
Cython Tutorial: How to Use shared_ptr
Set up Python environment on CentOS
How to use .bash_profile and .bashrc
How to install CUDA and nvidia-driver
How to install and use Graphviz
How to speed up Python calculations
How to build Python and Jupyter execution environment with VS Code
How to solve slide puzzles and 15 puzzles
How to set proxy, redirect and SSL authentication for Python Requests module
How to set up a jupyter notebook on ssh destination (AWS EC2)
Try to set up a Vim test environment quite seriously (for Python)
[Development environment] How to create a data set close to the production DB
[Blender] How to set shape_key with script
[Linux] How to subdivide files and folders
How to package and distribute Python scripts
How to split and save a DataFrame
How to speed up instantiation of BeautifulSoup
How to build a sphinx translation environment
How to set optuna (how to write search space)
How to install and use pandas_datareader [Python]
How to create an NVIDIA Docker environment
How to set the server time to Japanese time
How to prepare Python development environment [Mac]
How to set xg boost using Optuna
Set up Pipenv in Pycharm in Windows environment
Download CloudWatch Logs logs to your local environment
How to install your own (root) CA
python: How to use locals () and globals ()
How to use tensorflow under docker environment
Is your network stack set up correctly?
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
How to add python module to anaconda environment
How to run Cython on OSX Memo
How to use is and == in Python
How to use pandas Timestamp and date_range
How to install fabric and basic usage
How to write pydoc and multi-line comments