I'm trying to cythonize some python modules (on windows) and want the resulting .c files stored in a different directory.
I tried something like this:
from Cython.Build import cythonize
module_list = cythonize(r'C:\myproject\module.py',
force=True,
build_dir=r'C:\myproject\compiled_modules')
The compilation is done but the file module.c is created in C:\myproject and not in C:\myproject\compiled_modules as expected. What am I doing wrong?
I'm not sure the cythonize function can be coerced into doing that. I would just use the command line:
/path/to/cython yourmod.py -o compiled_modules/yourmod.c
Edit:
This command can be called from a python script using the subprocess module:
import subprocess
subprocess.check_call(["/path/to/cython", "yourmod.py", "-o", "compiled_modules/yourmod.c"])
Edit 2:
According to this Sage developer, relocating the c/cpp files is not supported by cython, so it looks like either compiling from the command line or using a normal configuration and then moving the c/cpp files are your only options.
Related
I have read How to enable `--embed` with cythonize? and Cython --embed flag in setup.py but this method does not work on Windows. Let's say we have:
# app.py
print("hello")
# build.py
import setuptools # allows us to avoid calling vcvarsall.bat, see https://stackoverflow.com/a/53172602/
from distutils.core import setup
from Cython.Build import cythonize
from Cython.Compiler import Options
Options.embed = "main"
setup(ext_modules=cythonize(r'app.py', build_dir="release"), script_args=['build'], options={'build': {'build_lib': 'release'}})
Running this build.py script on Python 3.8 for Windows does not produce an app.exe file like it would with the command line command:
cython app.py --embed
Instead, it produces a .pyd file.
How to use cythonize + embed from a Python script, producing a .exe, on Windows?
Solved: in fact the problem did not come from cythonize itself, but from the fact distutils.core.setup(...) is configured to compile+link into a .pyd instead of a .exe.
Here is the solution:
from distutils._msvccompiler import MSVCCompiler # "from distutils.msvccompiler" did not work for me!
from Cython.Compiler import Options
Options.embed = "main"
cythonize(r'src\app.py', build_dir="build")
compiler = MSVCCompiler()
compiler.compile([r"build\src\app.c"], include_dirs=["C:/Python38/include"])
compiler.link_executable([r"build\src\app.obj"], "app", libraries=["python38"], library_dirs=["C:/Python38/libs"], output_dir="release", extra_preargs=["/NOIMPLIB", "/NOEXP"])
The .exe will be in the release folder.
(Note: I also upgraded Cython to the latest version 0.29.30, it might have helped as well)
I have a compile.py script:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("module1.pyx"))
that compiles my Cython code. The drawback is that I have to call it with a command-line parameter build:
python compile.py build
Instead, I would like to be able to call this compile.py directly from Sublime Text, as usual, with CTRL+B. To do that, it should work from:
python compile.py
Question: how to modify the above script so that it can be run with python compile.py?
Method #1:
Use script_args like this:
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build'])
or
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build_ext'])
(both work).
If you want the output files to be in the same directory, you can use:
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build'],
options={'build':{'build_lib':'.'}})
or
setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build_ext'],
options={'build_ext':{'inplace':True}})
Method #2:
Add this on top:
import sys; sys.argv = ["", "build"]
It's a bit hack-ish but it works fine, and avoids to have to create a new build-system, like with Build and run with arguments in Sublime Text 2 (link kindly provided by #Melvin).
I have a Python code that uses two command line arguments. I am using linux terminal for all command line tasks. Now I am trying to use Cython to speed up my Python code.
For that I have compiled the Python code to C using build_ext module by creating this setup.py file:
setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup (
cmdclass = {'build_ext': build_ext } ,
ext_modules = [
Extension ("myCode", ["myCode.py"]) ,
])
And then compiling my Python code into C using:
python setup.py build_ext -i
The following were generated:
[file]myCode.c
[file]myCode.so
[folder]build
--[folder]temp.linux-x86_64-2.7
----[file]myCode.o
I want to run the generated file with command line arguments.
Till now in Python I was using the usual command
>> python myCode.py arg1 arg2
I am very new to Cython, infact I started using it to address the inherent speed issue of Python after code level algorithm optimization. I need inputs on which files to run, and how to run the converted C code and with command line arguments. Thanks in advance.
As mentioned you compiled a Python module. So to call from Linux you have to write a .py script that imports your compiled module and does any calculations needed. Then you can run it with your typical Linux command.
I am trying to link to my own C library from Cython, following the directions I've found on the web, including this answer:
Using Cython To Link Python To A Shared Library
I am running IPython through Spyder.
My setup.py looks like this:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy as np
setup(
ext_modules = cythonize(
[Extension("*",["*.pyx"],
libraries =["MyLib"],
extra_compile_args = ["-fopenmp","-O3"],
extra_link_args=["-L/path/to/lib"])
]),
include_dirs = [np.get_include()],
)
The file libMyLib.so is in /path/to/lib and it compiles fine.
I have a Python script in my IPython profile startup folder that does this
try:
os.environ["LD_LIBRARY_PATH"] += ":/path/to/lib"
except KeyError:
os.environ["LD_LIBRARY_PATH"] = "/path/to/lib"
I can confirm that this is running, because if I type os.environ["LD_LIBRARY_PATH"] into the IPython interpreter, it returns /path/to/lib
But when I try to load the Cython module (i.e. import mycythonmodule) I get:
ImportError: libMyLib.so: cannot open shared object file: No such file or directory
I've also tried putting libMyLib.so in other places to see if cython would find it:
In the directory where Python is running
On the Python path
In the same folder as the cython module
But it still doesn't find the shared library. The only way I can get it to find the library is by dropping it in /usr/lib, but I don't want it there, I want to be able to set the library path.
Am I missing something?
I'm self-answering, in case anyone else runs into the same problem. Looks like the answers are here:
Set LD_LIBRARY_PATH before importing in python
Changing LD_LIBRARY_PATH at runtime for ctypes
According to these answers (and my experience), the linker reads LD_LIBRARY_PATH when python is launched, so changing it from within python doesn't have any useful effect, at least not the effect I was hoping for. The only solution is to either wrap python in a shell script that sets LD_LIBRARY_PATH, or else drop the shared object somewhere on the linker search path.
Kind of a pain, but it is what it is.
I have fixed it by change setup.py.
I have a C++ dynamic shared library called "libtmsmdreader.so". and a header file named "TmsMdReader.hpp"
I wrapper C++ shared library to cython library called "tmsmdreader-pythonxxxxxx.so"
from setuptools import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(
name="tmsmdreader",
ext_modules=cythonize([
Extension(
name="tmsmdreader",
language="c++",
sources=["TmsMdReaderApi.pyx"],
libraries=["tmsmdreader"],
library_dirs=["."],
include_dirs=["."],
extra_compile_args=["-std=c++14"],
compiler_directives={'language_level': 3},
runtime_library_dirs=["."])
]))
library_dirs=["."] and runtime_library_dirs=["."] can fixed LD_LIBRARY_PATH if libtmsmdreader.so in python scripy directory
I coded a python application with a GUI in Tkinter with pictures. Now that I have finished, I am trying to convert it to a .exe with py2exe. All my pictures are in the same folder as my python file and setup file, but when I try to convert my python file via the Command Prompt, I get error messages saying that my .ico file is not defined and that it can not copy it. I think the problem is due to my setup.py file. How do I allow my images to be copied into the new .exe executable file without getting errors I have never used py2exe before.
Setup file:
from distutils.core import setup
import py2exe
setup(console=['Gui.py'])
Error:
How do I fix this?
You may have just forgotten to define your icon:
cfg = {
'py2exe.icon':'icon.ico',
...
}
Try to change your setup.py like this:
from distutils.core import setup
import py2exe
data_files = [('', [r'standardicon.ico'])]
setup(
windows =['Gui.py'],
data_files = data_files
)