Compiling multiple files to exe - python-3.x

I got multiple files to compile to a standalone exe using cython.
compile.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
Extension("TheMainFile", ["TheMainFile.py"]),
Extension("HelperClass", ["HelperClass.py"]),
Extension("HelperClass2", ["HelperClass2.py"]),
]
setup(
name = 'Main',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
The main file that should be run by the exe is TheMainFile
running the following command
python .\compiler.py build_ext --inplace
creates 3 .c files, in the build dir however is no .exe, just .lib,.exp and .obj files.
Manually adding these files to a VS Projects to build, results in the following error
"Python.h": No such file or directory
Adding the following files to the project from C:\Python\include lastly results in "structmember.h": No such file or directory
My question is:
How do I compile these files to a binary where TheMainFile is executed.

Related

Convert .py file into .pyd file

Well after a lot of search unable to find a proper solution, I have my python file 'Main.py'.I want to have its .pyd file i.e Main.pyd. I have tried the way of using Cpython i.e first I have converted 'Main.py' file into 'Main.c'but then unable to convert the 'Main.c' into 'Main.pyd' and its quite tough way.Can I have a simple way to convert 'Main.py' into 'Main.pyd'?
I think you just need to create a library from your script. Create a new setup.py besides your sample_code.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
Extension("sample_code", ["sample_code.py"]),
]
setup(
name = 'My Program',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
Then use python setup.py build_ext --inplace to generate your library.
You can find more information here.

error after compiling .py to .exe

when I compile a program from .py into .exe and open .exe, this error appears
when I compile a program from .py into .exe and open .exe, this error appears
from cx_Freeze import setup, Executable
setup(
name = "Diophantine equantion",
version = "1.0",
description = "Diophantine equantion",
executables = [Executable("Diofant.py", base='Win32GUI')]
)
Just add options = {'build_exe': {'includes': ['numpy.core._methods']}} to your setup.

cython compiles but no pyd files are generated

I tried to run a python code, say myfile.py (also tried to rename it as myfile.pyx) as follows:
import pyximport
pyximport.install(setup_args={"script_args":["--compiler=mingw32"]},
reload_support=True)
import myfile
myfile.mycode()
I am using PyCharm. The code seems to have run fine without any error and even gave me correct results on the Python Console within PyCharm.
However no pyd (or pxd) files were generated. How can I know if my code (myfile.mycode()) ran via Cython or via regular Python?
I am using Python 3.4, Cython 0.21.2.
Thanks
pyximport generates a temporary pyd file that is not in the working directory. You probably want to build a setup.py that looks something like:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension('myfile',
sources=['myfile.pyx'],
language='c++',
)]
setup(
name = 'myfile',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
which you can compile using:
python setup.py build_ext -i clean

Building minimal cython file with python 3.3 (Anaconda) under windows 7

When I try to build a minimal Cython file test.pyx with Python 3.3 (Anaconda 3) under windows 7, I obtain a strange error:
C:\Users\myname\Test_cython>python setup.py build
running build
running build_ext
error: [WinError 2] The system cannot find the file specified
Of course test.pyx is in the working directory. It works fine under windows with Python 2.7 (Anaconda) and under Linux with Python 2 and 3.
What could be the problem here with Python 3.3 (Anaconda 3)?
Thanks
The file setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
name = 'test',
cmdclass = {"build_ext": build_ext},
ext_modules = [Extension('test', ['test.pyx'])]
)
Solution:
I found that the line 404 of the file cygwinccompiler.py of the package disutils
out_string = check_output(['gcc', '-dumpmachine'])
has to be changed as
out_string = check_output(['gcc', '-dumpmachine'], shell=True)
Then, it compiles normally.
The line 404 of the file cygwinccompiler.py of the package disutils
out_string = check_output(['gcc', '-dumpmachine'])
has to be changed as
out_string = check_output(['gcc', '-dumpmachine'], shell=True)
Then, it compiles normally.

How to create .EXE file in python using cx_freeze

I have one application developed in python 3.2, which has inbuilt modules(ex: Tkinter, matplotlib, openpyxl), user defined modules & classes(ex: draw_graph, generate_report), icon files, log file, .csv, .docx etc. I am running this application from script(ex: testapplication.py)
I have setup file as
import sys
from cx_Freeze import setup, Executable
exe = Executable(
script=r"C:\Python32\testapplication.py",
base="Win32GUI",
)
setup(
name = "TESTApp",
version = "0.1",
description = "An example",
executables = [exe]
)
Now I want to create a exe file of this application. can anyone please suggest me a way to do this?
So this is what you need to do. For starters, change script=r"C:\Python32\testapplication.py" to script=r"testapplication.py"
Then, put ALL the files to need to convert into C/python32 including the setup file. Then what you wan to do is get your command line up, and type the following commands: (assuming that you're cx_freeze file is named setup.py):
cd
cd python32
python setup.py build
And then you should have a build folder in that directory containing the exe file.

Resources