way to eliminate Cython numpy compilation warnings? - python

I ran into the problem described here (What is this import_umath function?) and wanted to know if there is a fix for it? I have the exact same case where compiling Cython code that uses numpy with the following code:
import numpy as np
cimport numpy as np
np.import_array()
generates many warnings about _import_umath not being used:
/usr/local/lib/python2.7/dist-packages/numpy-1.6.2-py2.7-linux-x86_64.egg/numpy/core/include/numpy/__ufunc_api.h:226:1: warning: ‘_import_umath’ defined but not used [-Wunused-function]
removing np.import_array() does not change the result. Like one of the posters suggested in the above thread, I tried adding this in my .pxd/.pyx file:
cdef extern from *:
import_umath()
this also made no difference. How can this warnings be eliminated?

You can pass arguments to the C compiler using the keyword extra_compile_args in your setup.py. For example, this will not generate warnings:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
import numpy
extensions=[
Extension("abc",
["abc.pyx"],
include_dirs=[numpy.get_include()],
extra_compile_args=["-w"]
)
]
setup(
ext_modules=cythonize(extensions),
)

In Cython Tricks and Tips they explain that you need:
cdef extern from *:
pass
when you import extern packages. I already needed this trick to write a wrapper, maybe it will work for you too...

Related

ray in Cython error: The #ray.remote decorator must be applied to either a function or to a class

I apply ray with Cython. It shows an error:
TypeError: The #ray.remote decorator must be applied to either a function or to a class.
How to solve this problem? Thanks.
The setup.py:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
ext_modules=cythonize("ray_cython.pyx"),
include_dirs=[numpy.get_include()]
)
setup(
ext_modules=[
Extension("ray_cython", ["ray_cython.c"],
include_dirs=[numpy.get_include()]),
],
)
# compile by python setup.py build_ext --inplace
# Then run compare.py
The ray_cython.pyx file:
from numpy import linalg as LA
import time
#Import package
import ray
cimport numpy as np
np.import_array()
DTYPE = np.int
ctypedef np.int_t DTYPE_t
#ray.remote
def myfunction():
...
https://docs.ray.io/en/latest/ray-core/advanced.html#cython-code-in-ray
You must include the following two lines at the top of any *.pyx file:
#!python
# cython: embedsignature=True, binding=True
You cannot decorate Cython functions within a *.pyx file (there are ways around this, but creates a leaky abstraction between Cython and Python that would be very challenging to support generally). Instead, prefer the following in your Python code:
some_cython_func = ray.remote(some_cython_module.some_cython_func)

How do I include the numpy includedir for a setuptools extension?

I have a setup.py that looks like this, minus the irrelevant bits.
import numpy
my_module = Extension(
'my_module',
......
include_dirs=[numpy.get_include()],
)
setup(
...
ext_modules=[my_module],
install_requires=['numpy'],
)
I cannot figure out how to rewrite this so that numpy is automatically installed if it is not already in the environment. In its current form, I get an error from the import numpy about numpy not being defined.
I need the extension to know where the numpy include files are. But there seems no way to delay the definition of the extension (or at least of its include_dirs) until after the installation of numpy from the install_requires has occurred.
Is there a workaround?

I am getting a "DLL load failed: The specified module could not be found." when using Cython with a Numpy program

I am trying to build a library using Numpy and Cython. While compiling the .pyx file went smoothly, I can't test out the files in a test file.
It just says ", line 1, in
import blank_cy #The name of the .pyd
ImportError: DLL load failed: The specified module could not be found.
I have tried looking at other similar problems but I still can't figure it out. Also, I am not sure what information I need on here so please ask. I'll just list off some things.
The .pyx file imports numpy as np and math and cimports numpy as np.
The compiling process does not produce any errors.
I renamed the file to match my import
Without imports it works fine.
Thank you so much.
Here's an example.
This would be the test.pyx
import numpy
cimport numpy
print("Hello World");
The setup.py:
from setuptools import setup
from Cython.Build import cythonize
import numpy
setup(ext_modules = cythonize("test.pyx"),include_dirs=[numpy.get_include()])
The test file to import test.pyd
import test
I renamed the file to match my import
Don't do this! This is your problem.
When it imports an extension named my_module Python looks for a function called PyInit_my_module (the function name is slightly different for Python 2, or if the module name has non-ascii characters, but the same basic idea applies) as the module initialisation function.
Since you've renamed your module the name of the initialisation function that Cython has created no longer matches and thus the whole thing breaks.
Just ensure that your pyx files have the module name that you ultimately want to use.

Having problem with a cython library in python

I'm working on a python code. This code uses a library that is written in cython.
when I run, I face this error:
I use python3.7. I've searched a lot but I couldn't find a useful way to solve it. I think that visual cpp has not been matched with python. First I had visual cpp 2017 but didn't work and I uninstalled it. After that I installed only visualcppbuildtools but the result didn't change. Can anyone help me?
(Also I added the below part to my code but the result didn't change.)
This is setup.py :
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy
ext_modules=[
Extension("lib2",
["lib2.pyx"],
language="c++",
libraries=["m"],
extra_compile_args = ["-std=c++11", "-O3", "-ffast-math", "-
march=native", "-fopenmp" ],
extra_link_args=['-fopenmp'],
include_dirs=[numpy.get_include()]
)
]
setup(
name = "lib2",
cmdclass = {"build_ext": build_ext},
ext_modules = ext_modules,
include_dirs=[numpy.get_include()]
)
this the main.py:
import sys
sys.path.append('E:\EDU\Hipp\septo-hippocampal-model-master\septo-
hippocampal-model-master\cython_code')
import pyximport
pyximport.install()
import lib2 as lib
and the last one is lib2.pyx:
from libc.math cimport exp, cos
from libcpp.map cimport map
from libcpp.pair cimport pair
from libcpp.string cimport string
from libcpp.vector cimport vector
from libcpp cimport bool
from cython.operator cimport dereference, preincrement
import numpy as np
cimport numpy as np
from libcpp.queue cimport queue
from cython.parallel cimport parallel, prange
cimport cython
By those simple codes I still receive the above error.

Cython ImportError: No module named parallel

I am trying to access the new parallel features of Cython 0.15 (using
Cython 0.15.1). However, if I try this minimal example (testp.py), taken from http://docs.cython.org/src/userguide/parallelism.html:
from cython.parallel import prange, parallel, threadid
cdef int i
cdef int sum = 0
for i in prange(n, nogil=True):
sum += i
print sum
with this setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy
ext = Extension("testp", ["testp.pyx"], include_dirs=[numpy.get_include()],
extra_compile_args=['-fopenmp'], extra_link_args ['-fopenmp'])
setup(ext_modules=[ext], cmdclass={'build_ext': build_ext})
when I import testp, Python tells me: ImportError: No module named
parallel. And in fact, if I browse the Cython package in the
site-packages, I cannot find any file or directory that is called
parallel. But I thought it should be included somewhere in the
release? Could someone please clarify for a confused user?
I'm using Cython 0.15+
cython.parallel exists in Shadow.py:
import sys
sys.modules['cython.parallel'] = CythonDotParallel()
And the Shadow.py can be located in your Python's dist-packages directory like /usr/local/lib/python2.6/dist-packages/ in Linux
You can check all of your python modules in python command-line using:
>>> help('modules')
And then try to install/reinstall cython using easy_install or pip.

Categories

Resources