Python script slower as .exe than as .py - python

I'm writing an app in Python that uses among others PySide for the GUI and also modbus_tk for Modbus communications between my PC and an embedded controller.
I've noticed that when I byte-compile my script to a Windows application (.exe), using py2exe, the requests are sent to the controller slower than with the Python script (.py).
Have you ever experienced something like this ? Is it normal ? What should I do to speed up my program ? Any help would be highly valued.

From https://en.wikipedia.org/wiki/Py2exe
Although this program transforms a .py file to an .exe, it does not make it run faster as py2exe just bundles the Python bytecode rather than converting it to machine-code. It may even run slower than using the Python interpreter directly because of startup overhead.
You can't compile (standard) Python code.

Related

Password protect a Python Script that is Scheduled to run daily

I have a python script that is scheduled to run at a fixed time daily
If I am not around my colleague will be able to access my computer to run the script if there is any error with the windows task scheduler
I like to allow him to run my windows task scheduler but also to protect my source code in the script... is there any good way to do this, please?
(I have read methods to use C code to hide it but I am only familiar with Python)
Thank you
Compile the source to the .pyc bytecode, and then move the source somewhere inaccessible.
Open a terminal window in the directory containing your script
Run python -m py-compile <yourfile.py> (you should get a yourfile.pyc file)
Move <yourfile.py> somewhere secure
your script can now be run as python <yourfile.pyc>
Note that is is not necessarily secure as such - there are ways to decompile the bytecode - but it does obfuscate it, if that is your requirement.

How to eliminate two instances of .exe compiled by PyInstaller

I compiled a python program using PyInstaller. But when I execute the .exe file on a client, two instances of the .exe is running on the task manager.
How can i eliminate the other .exe?
This question didn't have any clear answer.
I'm using python 2.7 and I'm running the .exe on Windows XP.
I recommend reading this section:
https://pythonhosted.org/PyInstaller/#id74
Bootloader
The bootloader prepares everything for running Python code. It begins the setup and then returns itself in another process. This approach of using two processes allows a lot of flexibility and is used in all bundles except one-folder mode in Windows. So do not be surprised if you will see your bundled app as two processes in your system task manager.

Does converting an interpreted script to an executable increase speed?

In general I'm curious, does using a utility that converts a non-exe to an exe increase speed? It's my understanding that they just package the interpreter inside the exe.
In specific, if you have a python script and use py2exe on it, does the resulting executable run faster than the .py? My boss seems to have the assumption it does but I'm not so sure.
Especially when dealing with multiple modules. For example say you have modules first.py and second.py. You compile them all to executables. When they were .py they second.py could be called as
from second import main
main()
Now that they're executables you have to start a new process, which surely is slower?
subproccess.call(["second.exe"], shell=True)
Do I understand this correctly? Or does importing from another python module actually start a new instance of the python interpreter or something?
In our case the target platform is always Windows.
Your boss is misinformed. All py2exe does is package your program into a self-contained package capable of running without dependencies. It is still the same bytecode running on the same interpreter (well, whatever one is packaged).
See this other answer for about all of the "optimization" you can get out of using -o flags.
Also, yes, definitely run some benchmarks to confirm for yourself.

Running C Program on Windows 8.1 using Python (or not Python)

The original goal was to write a software executable on Windows platform that could read numbers from 7-segment displays and record it at a constant time interval. I tried out a couple of OCR libraries in Python (since I was planning to write the program in Python) but learned that they are more suitable for handwritten letters and soon redirected my attention to another open source program written in C, specifically designed for reading numbers from 7-segment displays: http://www.unix-ag.uni-kl.de/~auerswal/ssocr/
So I compiled the source code in Linux and planned to move the compiled file, along with its linked library files, to the Windows machine, naively believing that I could simply "exec" from a Python script to execute the program. It turns out it couldn't - just trying to run the program (after manually adding an .exe extension at the end and double clicking) generates a response "the app cannot run on this pc. to find a version for this computer, check with the software publisher".
So my question is, whether there is a way to simply execute the C program from a script on Windows platform, and if not, what you might suggest me do to complete the aforementioned task?
You can execute any program using Python (like other programming languages). But what happens (for all languages) is that they do a system call and ask the underlying operating system to execute the program, because that is one of the responsibilities of the OS.
Open a Python REPL (or write a Python script) and try this (This is only one of the ways to execute a program from python)
import subprocess
# if you're on windows call this
subprocess.call("notepad", shell=True)
# If on Linux call this
subprocess.call("ls", shell=True)
What happens is that Python asks OS to execute the specified program (notepad or ls), weather OS can run the requested application is another story.
Your C program is compiled on Linux so it is an executable in a format that Linux can run (probably ELF format). Windows requires executable files to be in a different file format. File extensions just help the environment to treat files appropriately. If you change the extension of your Linux executable to '.exe', it does not change its content, so Windows still would not be able to run it.
You need to recompile your C program on Windows so the compiled program is executable on Windows. If the program requires a POSIX environment (for example if it uses fork() syscalls or etc.), then you could use Cygwin on Windows (that provides system calls and libraries available in Linux for Windows) to compile it.

Python script packed with cx_Freeze: how do I force windows to use an alternative console?

I'm a very unexperienced programmer who has just began studying Python. I managed to do some basic stuff and now I tried packing my .py files to .exe files that can be shared with friends who don't have Python installed. I'm using python 3.4 so I was forced to use cx_Freeze which is pretty easy to understand, but I have a question: is there a way to "embed" a different console than cmd.exe in those .exe's?
I wrote some scripts that work only with command line, but window's console is pretty ugly and frustrating. I want my packed .exe to be opened with a more friendly console (even something like IDLE), is that possible? (everything must be packed in the final .exe, because this have to work even on an external pc that doesn't have python installed or a specific console installed).
Sorry for my bad english!
May be you should look to "py2exe" which is only build for windows.
I'm not sure at all you can embed "idle" in you "compiled" python project. If you need a more windows friendly presentation, I suggest you use wx python library which can be easily embed in your py2exe project.
see http://sourceforge.net/projects/py2exe/ and http://wiki.wxpython.org/py2exe

Categories