Executable file out of a Python script - python

Sometimes when you write programs in Python, the need for using special libraries may arise.
For example, when writing a script X.py you might need to handle operations between matrices and so you would go for the Numpy library, or you may need to read and write raster files and you would go for GDAL library which support raster manipulation.
In the case of Numpy you would simply have to install the library, but for GDAL you would first have to install GDAL utility on your operating system and then install the associated
Python bindings.
Now, to successfully run the script X.py you would need a computer that has Python installed and the associated libraries.
What about making an executable X.exe out of this script? Could you run it on any Windows OS or you would need Numpy and GDAL installed on that OS to be able to successfully execute it?

You can use something call: PyInstaller, this takes care of all the libraries while packing/building your exe file. All you need is just this .exe and it will work seamlessly on all windows/linux machine.
In a similar post on stack overflow, there was detailed discussion on pyinstaller. Referred here.

Related

Using pyinstaller on c compiled python code

I am trying to convert one single c file into an executable with pyinstaller. Now the reason why I want to compile with pyinstaller is because of the fact, that the exe file is supposed to be run on both a mac as well as a windows machine.
Now, how can this be done?
You can include Cython/C modules in a Pyinstaller executable.
However, Pyinstaller is not suitable for your goal of making a single executable that works on Mac and Windows. From the first question of the Pyinstaller FAQs:
Can I use PyInstaller as a cross-compiler?
Can I package Windows binaries while running under OS X?
No, this is not supported. [...]
It seems like you've fundamentally misunderstood what Pyinstaller does: it packages a Python script with Python and its libraries to allow you to use the Python script without having to install Python separately. To do this though it needs to package a version of Python specific to the platform, and so the executable will only work on the same operating system that it was created on.
In addition, it deals with compiled libraries (like Cython modules) by zipping them up and the extracting them into a temporary folder when run. Therefore, even if Pyinstaller somehow managed to bundle two versions of Python to work on both Windows and Mac your C compiled module would still only be compiled for a single platform, so by doing it they way you describe you've actually made your code less portable.
I don't believe there are any obvious tools to do what you're asking.

Using pyWin32 without installing it

Is it possible to use pyWin32 without installing it?
I would like to include it into python's interpreter folder.
I have managed to use Python interpreter without installing it (simply installing it and copy&pasting installed directory into my product).
I need also the same with pyWin32.
My objective is:
Client receives a folder with a script (batch) which will run python program in a console. The client does not have to install python, pywin32, nothing. What he needs is to copy and paste my product and run the console-based program.
Is it possible?
My objective is: Client receives a folder with a script (batch) which
will run python program in a console. The client does not have to
install python, pywin32, nothing. What he needs is to copy and paste
my product and run the console-based program.
I would suggest something along the lines of using pyinstaller or any other python code to executable converter.
http://www.pyinstaller.org/
PyInstaller is a program that freezes (packages) Python programs into
stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD,
Solaris and AIX. Its main advantages over similar tools are that
PyInstaller works with Python 2.7 and 3.3—3.5, it builds smaller
executables thanks to transparent compression, it is fully
multi-platform, and use the OS support to load the dynamic libraries,
thus ensuring full compatibility.
To me it seems this would suit your/your clients needs quite well. Furthermore pyinstaller is easy to use.

Cython, how to include 'external' modules and compile a single library?

I have created a plugin for a software, this plugin is written in Python and I wish to distribute it in the form of a shared library (.so) for Mac OS (only). In my code I have a number of imports to packages that may not be installed in the 'target' Macs and I want to avoid having the user to download those packages as they many not have pip on their Mac and so on...
The simplest solution would be to 'build' a shared library with all the dependencies/modules to ensure that the user just downloads it and activates the plugin in the software and everything automatically works.
Is that possible?
I have been doing a lot of searching and reading on the web, I've look at pyInstaller, but I don' want to create a stand-alone executable but a single shared library with imports from packages such as Crypto, zerconf, twisted, etc.
If not what will be the alternatives?
I have tried Cython but I only get my plugin code compiled into a library so when I try to use it in a computer without the necessary packages I get an import error.
Is there a way to 'tell' Cython to compile all the dependencies of the code into a single shared library?
As I mentioned above pyInstaller could work for a stand-alone executable but in my case it needs to be a shared library...
Many thanks!

Run a python script in an environment where a library may not be present

I have a python script where I am including a third party library:
from docx import Document.
Now, I need to run this script in an environment where bare-bones python is present but not this library.
Installing this library in the target environment is beyond my scope and I tried using distutils, but couldn't go far with it. The target environment just need to run the script, not install a package.
I am from Java background and in Java I would have just exported and created a jar file which would have included all the libraries I needed. I need to do similar with python.
Edit: With distutils, I tried creating a setup.py:
from distutils.core import setup
import docx
setup(name='mymodule',
version='1.0',
py_modules=['mymodule', docx]
)
But I am not sure this works.
PyInstaller won't work if you can't make a pyc file and you cannot make pyc file unless your code runs without fatal errors.
You could have the import in a try block that excepts ImportError 's but that will result in NameError 's where the package is referenced. Long story short if the package is integral to the script no amount of avoiding it will fix your problem. You need the dependencies.
You said installing the package is beyond your scope, well then, it is time to expand your scope. Docx is an open source package you can find on github here
You can download that and run setup.py
You can include the modules for docx in your application. Just distribute them together.
But docx depends on the lmxl operating system package and needs to run setup on that. You can't just copy it to the target machine.
I'm not sure PyInstaller supports docx, especially add it has the non python dependency.
Really using pip or easy_install is the way to go.
PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, Solaris and AIX.

OpenCV with standalone python executable (py2exe/pyinstaller)

I have a python program that uses OpenCV to get frames from a video file for processing. I then create a standalone executable using py2exe (also tried pyinstaller and got same error). My computer and the target computer are both Windows 7, but the target computer does not have python installed. I use OpenCV to read the frame rate and individual images from a video file.
Problem: When I run the executable on the target computer the frame rate is returned as 0.0 and I cannot read frames.
If python is installed on the target machine then the executable runs as expected, otherwise it produces this error. So it seems that something is missing in the executable, but I get no errors when creating the executable to indicate what might be missing.
Others who have reported similar issues usually have not included the numpy dependency (and get an error indicating this), but I have included numpy. I have also tried including the entire PyQt4 module since this is listed as a dependency on the python xy site for OpenCV (I already have parts of PyQt4 for other parts of the code) and this does not solve the problem either.
I guess I will go ahead and post an answer for this, but solution was provided by #otterb in the comments to the question. I am pasting the text here:
"py2exe is not perfect so will often miss some libraries or dll, pyd etc needed. Most likely you are missing opencv_highgui249.dl‌​l and opencv_ffmpeg249.dll etc. I would use py2exe with no single executable option enabled. And, start manually copying files that might be needed for your app. After identifying them, modify setup.py for py2exe to include them automatically."
I will note however that I use pyinstaller rather than py2exe, since I get fewer problems while building. I still have to manually copy the opencv dll files though.On Windows 7 they are located here: "C:\Python27\DLLs" and they need to be copied into the distribution folder so that they are on the same path as the other dll files that go with the distribution.
Try using pyinstaller, download it using pip :
pip install pyinstaller
if you don't know how to install pip , try downloading python 2.7.9 or above, which has inbuilt pip, but don forget to add python path to environment varibles, this procedure is mentioned in this post:
How to run Pip commands from CMD
After installing pyinstaller, select the main file of your project and run this command
pyinstaller yourprogram.py
it will create folder with application file naming your file name, and finally make sure that numpy and opencv are in lib->site-packages folder of your python27 in your C Folder while running that command
Hope it Helps!

Categories