I have been trying for hours to figure out how to do this going through pyinstaller's docs, but I haven't had any luck.
I have a single .py file, and I need that made into a .exe file executable in windows 7, and a .app (or what ever works) executable in OS X Lion. The problem is that when ever I use
python pyinstaller.py my_code.py
it compiles into a linux executable.
Pyinstaller doesn't build executables for cross-platform targets, only for the platform on which Pyinstaller is run "natively". However, WINE allows running the native Windows Pyinstaller under Linux, so it can be used to build Python scripts developed on Linux into native Windows .exe executables using only the single Linux host - no separate Windows host necessary. These instructions don't target Mac.
I'm using Ubuntu 15.10 on a 64bit Pentium-type.
$ uname -op
x86_64 GNU/Linux
$ grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 15.10"
Ensure Python is installed.
$ apt-get install python2.7
[installed OK]
$ python --version
Python 2.7.10
(Upgrade and) use the Python package manager to install and
possibly upgrade Pyinstaller on Linux. Running pip without superuser
privileges might fail.
$ sudo -H pip install --upgrade pip
[upgraded OK]
$ sudo -H pip install PyInstaller
[installed OK]
$ sudo -H pip install --upgrade pyinstaller
[installed OK]
$ pyinstaller --version
3.0
You can install Python/Pyinstaller, install/configure WINE and write
Python code in any order (though running code requires Python is
installed). Use the Python package manager to install Python
packages pendent to your Python project.
$ pip install package1 [package2, ...]
[packages installed OK]
Test packaging the executable targeting Linux.
$ cd python-project
$ pyinstaller --onefile python-project.py
[built OK]
$ dist/python-project
[ran OK]
If it doesn't build or run OK, try building it as --onedir , the
Pyinstaller default, which doesn't include pendent files in the
single executable. That shouldn't build/run any differently than the
onefile version, but it can be easier to debug the onedir which
should then build OK as onefile too.
Ensure WINE is installed and configure it to use your selected
target Windows version (eg. Windows 7):
$ wine --version
wine-1.7.50
$ winecfg
[GUI: Applications tab > Windows Version: Windows 7]
Use WINE to install the Windows Python, pywin32 (Windows GUI
extensions); match their versions. You should probably go to each
installer's download page for the right versions and mirrors rather
than these current direct download links. Note that running WINE
dumps a lot of WINE bug notifications to the console; these are
practically all negligible in this procedure.
$ mkdir -p /opt/windows $ pushd /opt/windows
$ wget https://www.python.org/ftp/python/2.7.10/python-2.7.10.amd64.msi
$ wget http://iweb.dl.sourceforge.net/project/pywin32/pywin32/Build%20219/pywin32-219.win-amd64-py2.7.exe
$ wine msiexec -i python-2.7.10.amd64.msi $ wine msiexec -i pywin32-219.win-amd64-py2.7.exe
$ popd
I had problems with the Python 2.7.10 MSI wizard failing to "Next"
after selecting the install directory, so I cancelled it there and
ran it again adding the -qn option that suppresses the GUI. It
complained a little but it completed the installation. If you need
to find Windows Python in your Linux filesystem it's by default
installed in your Linux user's default WINE "C:" directory in your
home directory, ie. ~/.wine/drive_c/Python27 .
$ wine C:/Python27/python --version Python 2.7.10
$ find ~/.wine/drive_c -name python.exe ~/.wine/drive_c/Python27/python.exe
Upgrade Windows pip and install Pyinstaller using WINE Python/pip.
$ wine C:/Python27/Scripts/pip.exe install --upgrade pip
[upgraded OK]
$ wine C:/Python27/Scripts/pip.exe --version
pip 7.1.2 from C:\Python27\lib\site-packages (python 2.7)
$ wine C:/Python27/Scripts/pip.exe install pyinstaller
[installed OK]
$ wine c:/Python27/python.exe C:/Python27/Scripts/pyinstaller-script.py --version
3.0
Install your project's pendent packages with Windows pip
$ wine C:/Python27/Scripts/pip.exe install xlwt
[installed OK]
Now your Windows (WINE) Python environment is configured
equivalently to your Linux native environment. Running Windows
Pyinstaller under WINE generates a native Windows .exe executable.
Use the Windows Python script version of Windows Pyinstaller to
maintain parity with your tested OK Linux Pyinstaller procedure,
against your python project in your Linux filesystem (it doesn't
need to be copied to the WINE Windows filesystem). Keeping the
Windows build and dist directories separate from the tested OK
Linux one can help debugging the packaging procedure.
$ wine c:/Python27/python.exe C:/Python27/Scripts/pyinstaller-script.py --onefile --workpath=./win-wrk --distpath=/opt/windows python-project.py
[packaged OK]
$ ls -F /opt/windows/python-project.exe
python-project.exe*
$ wine /opt/windows/python-project.exe
[Windows app runs OK]
From the pyinstaller FAQ:
Can I package Windows binaries while running under Linux?
No, this is not supported. Please
use Wine for this, PyInstaller runs fine in Wine. You may also want
to have a look at this thread in the mailinglist. In version 1.4 we
had build in some support for this, but it showed to work only half.
It would require some Windows system on another partition and would
only work for pure Python programs. As soon as you want a decent GUI
(gtk, qt, wx), you would need to install Windows libraries anyhow. So
it's much easier to just use Wine.
In other words, you cannot simply run a command in Linux to build a Windows executable (or a Mac executable for that matter) like you are trying to do. The workaround they have provided for Windows (and only for Windows) is to install Wine.
Wine is a program that allows windows programs to run on Linux. It creates an environment that replicates a windows environment. The idea is that you would install Python (using a Windows Python installer), as well as any other libraries that you need (such as pyinstaller), into this environment. Then, still in that environment, you run the python pyinstaller.
I haven't done all of this, but the mailing list thread mentioned in the FAQ would be a good start. Here is an example that they use:
PYDIR="c:/Python27"
PYTHON="wine $PYDIR/python.exe"
WINPWD=$(winepath -w $(pwd))
cd pyinstaller
$PYTHON Configure.py
$PYTHON Makespec.py -p $WINPWD $WINPWD/diceroller.py
$PYTHON Build.py diceroller/diceroller.spec
It looks like this uses the old "Configure.py", "Makespec.py" and "Build.py", whereas nowadays the "pyinstaller.py" script tries to do all of this for you.
Related
I have a Python script that I'd like to compile into a Windows executable. Now, py2exe works fine from Windows, but I'd like to be able to run this from Linux. I do have Windows on my development machine, but Linux is my primary dev platform and I'm getting kind of sick of rebooting into Windows just to create the .exe. Nor do I want to have to buy a second Windows license to run in a virtual machine such as VirtualBox. Any ideas?
PS: I am aware that py2exe doesn't exactly compile the python file as much as package your script with the Python interpreter. But either way, the result is that you don't need Python installed to run the script.
As mentioned by other answerers, the cross-compilation feature is removed from PyInstaller since 1.5. Here, show how to package a Windows executable from Python scripts using PyInstaller under wine.
Step 1: Install wine and Python
sudo apt-get install wine
wine msiexec /i python-2.7.10.msi /L*v log.txt
PS:
Newer Python versions already include pip (is used to install pyinstaller). Download Python installation package from here (e.g., python-2.7.10.msi)
For macos users, use brew cask install xquartz wine-stable.
Step 2: Install PyInstaller on wine
$ cd ~/.wine/drive_c/Python27
$ wine python.exe Scripts/pip.exe install pyinstaller
Successfully installed pyinstaller-3.1.1 pypiwin32-219
Step 3: Package Python scripts
Package Python scripts (e.g., HelloWorld.py) with pyinstaller.
$ wine ~/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile HelloWorld.py
# filename: HelloWorld.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print('Hello World!')
The Windows executable file is located in dist/.
$ wine dist/HelloWorld.exe
Hello World!
fixme:msvcrt:__clean_type_info_names_internal (0x1e24e5b8) stub
Refer to here for the detailed description.
Did you look at PyInstaller?
It seems that versions through 1.4 support cross-compilation (support was removed in 1.5+). See this answer for how to do it with PyInstaller 1.5+ under Wine.
Documentation says:
Add support for cross-compilation: PyInstaller is now able to build Windows executables when running under Linux. See documentation for more details.
I didn't try it myself.
I hope it helps
You could run Windows in VirtualBox in order to run py2exe. VBox offers a powerful command-line client for automating tasks, so it something that you could likely integrate into your development process with ease.
Tested on Platform: Kubuntu 20.04, wine 6.0, python38
Download wine and python
Download windows version of python from https://www.python.org/downloads/release/python-3810/
Install wine
sudo apt install wine
Open your terminal and run
wine the-python-exe-you-downloaded
Run
find ~/.wine -name pip.exe
this will give you the pip path:
/home/yourusername/.wine/drive_c/users/yourusername/Local Settings/Application Data/Programs/Python/Python38/Scripts/pip.exe
Install pyinstaller
Run wine /home/yourusername/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pip.exe install pyinstaller
Package your file
Find installation path
find ~/.wine -name pyinstaller.exe
wine /home/yourusernmae/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pyinstaller.exe --onefile yourpythonfile
I wrote a blog post on how to do this with PyInstaller. Here's the summary:
How to create EXEs for Python on Linux, using PyInstaller and WINE
Download Python 3.8 Windows installer
wine python-3.8.9.exe, then see instructions below
wine C:/Python38/python.exe -m pip install --upgrade pip
wine C:/Python38/python.exe -m pip install -r requirements.txt, and requirements.txt should include PyInstaller itself
wine C:/Python38/Scripts/pyinstaller.exe ...
All done!
Installing Python 3.8 in Wine:
Check "Add Python 3.8 to PATH"
Click "Customize installation
Click "Next"
Click "Install for all users"
Set the install location as C:\\Python38
Click "Install"
Close the window.
Feel free to read the post to get more details.
I have tested py2exe inside of wine, and it does function. You'll need to install python in wine for it to work, or if you only use the standard libarary, you can bundle py2exe with py2exe from the windows machine and then use it in wine. Just keep in mind you need the same version of the ms visual C libraries in wine as were used to compile python or things won't work properly.
I am trying to install CyLP. I downloaded the cbc package.Then it says to run the following commands:
$ ./configure
$ make
$ make install
But these commands do not run in command prompt.So, how do I run them.Also, I tried the following command for pip install:
pip install -i https://pypi.anaconda.org/pypi/simple cylp
But, it gave me the error that cbc files were not found found.So , how do I install CyLP in windows.
I have recently worked with cylp package but on linux. So I am not sure that it is available for windows but there can be a quick fix to this problem on windows.
For working of make and make install on windowsHere are a few ports of GNU tools to Windows:
GnuWin32 - http://gnuwin32.sourceforge.net/summary.html
Gnu Tools for NT - http://www.devhood.com/Tools/tool_details.aspx?tool_id=3
GNU Utilities for Win32 - http://unxutils.sourceforge.net/
For working of ./configure on windows - I would install the MINGW/MSYS development tools and see if the configure script is happy in that environment.
EDIT
Okay So I finally came up with the solution on how to install CYLP on windows. I would recommend you to:
Try the following
Create a new environment for 32bit Python 2.7:
set CONDA_FORCE_32BIT=1
conda create -n py27_32 python=2.7
Activate it:
set CONDA_FORCE_32BIT=1
activate py27_32
install cylp:
conda install scipy
pip install cylp
I want to convert myscript.py to a exexutable file. i am using raspberry pi(raspbian) and python 2.7.
I am issuing the following command
sudo pip install PyInstaller
sudo pyinstaller myscript.py
after some processing it provides an error
Fatal error: PyInstaller does not include a pre-compiled bootloader for your
platform. See <http://pythonhosted.org/PyInstaller/#building-the-bootloader>
for more details and instructions how to build the bootloader.
i go online to build compiler but could not understand the process.
how could i solve this problem?
Full Tutorial
After many hours of searching, I got this working. The answer is straightforward, but it is spread across multiple StackExchange answers and GitHub Issues. I put it all together in this tutorial, so I save some hours for the next poor soul.
Key Takeaways
pip ships PyInstaller with the incorrect architectures. You need to build it yourself for ARM (Raspberry Pi).
Step by Step
1. Build the bootloader
git clone https://github.com/pyinstaller/pyinstaller
# Alternatively download the zip from https://github.com/pyinstaller/pyinstaller/releases
cd pyinstaller/bootloader
python ./waf distclean all # or python3
cd ../PyInstaller/bootloader/
ls
Here you should see Linux-32bit-arm and inside of it run and run_d
2. Check the bootloader
file Linux-32bit-arm/run
run: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=a01c65d74d7d8b483800154dcdd4a5d2aad95d5b, stripped
If you see the above, you are good so far. However, if you see something like ELF 32-bit LSB executable, Intel 80386, that is wrong.
3. Copy the bootloader
If you installed PyInstaller with pip inside a venv, then do this
# Replace ${CLONED_PYINSTALLER_PATH} with the path where you git cloned above
# Replace ${PATH_TO_YOUR_PROJECT} with the path to your project (where you have the venv)
cp -r ${CLONED_PYINSTALLER_PATH}/PyInstaller/bootloader/Linux-32bit-arm ${PATH_TO_YOUR_PROJECT}/venv/lib/python3.5/site-packages/PyInstaller/bootloader/
If you installed with apt-get, then do this
# !!! Replace python3.5 with your version
cp -r ${CLONED_PYINSTALLER_PATH}/PyInstaller/bootloader/Linux-32bit-arm /usr/local/lib/python3.5/dist-packages/PyInstaller/bootloader
Debugging
Issue
SystemError: objcopy Failure: objcopy: Unable to recognise the format of the input file `$FILE`
Check
`file dist/$FILE`
If it does not say ELF 32-bit LSB executable, ARM [...], but instead says Intel or x86, the PyInstaller tries to use the incorrect bootloader. If you executed all steps above, try renaming the Linux-32bit-arm to just Linux-32bit. That seems to have worked for this user
Issue
gcc not found
Solution
sudo apt-get install build-essential
Suspect path for the compilation of bootloader is wrong for your platform
may do this as mentioned in the forum here
cd /usr/local/lib/python2.7/dist-packages/PyInstaller/bootloader
sudo mv Linux-32bit Linux-32bit-arm
For RPI you need to get bootloader... You may cloned pyinstaller v3.1.1 into your rpi
Same thing, change the directory name for your arm platform after you have build the pyinstaller
cd /path/to/pyinstaller/PyInstaller/bootloader
cp -R Linux-32bit Linux-32bit-arm
For RPI, please clone the pyinstaller source code first and then follow my instructions.
Switch to pyinstaller/ bootloader subfolder.
Make the bootloader for your OS using the following command:
python3.5 ./waf distclean all
Make the Linux-32bit-arm sub-folder in /usr/local/lib/python3.5/dist-package/PyInstaller/bootloader
Copy the run and run_d to Linux-32bit-arm:
cp pyinstaller/bootloader/build/debug/run_d /usr/local/lib/python3.5/dist-package/PyInstaller/bootloader/Linux-32bit-arm/
cp pyinstaller/bootloader/build/release/run /usr/local/lib/python3.5/dist-package/PyInstaller/bootloader/Linux-32bit-arm/
I was able to install and create executables, on a Raspberry Pi running Raspbian (stretch 9.4), by running setup.py. Python version installed is 3.5.3.
git clone https://github.com/pyinstaller/pyinstaller
Inside pyinstaller source folder, run sudo python3 setup.py install
After, just execute pyinstaller <source>.py.
There does not seem to be any documentation on how to do it. I went into /sdk/installer and ran the vboxsetup.py script but it raised the following exception:
Exception: No VBOX_INSTALL_PATH defined, exiting
It is really annoying that they don't adequately explain how to install it and it's weird that the virtualbox installation does not create the VBOX_INSTALL_PATH variable by itself.
Anyways, This is what worked for me:
export VBOX_INSTALL_PATH=/usr/lib/virtualbox
sudo -E python vboxapisetup.py install
this is a minimal example for running the Python demo script vboxshell.py from the VirtualBox SDK via the Object Oriented Web Service for Python.
Tested with:
System: macOS 10.15.2
VirtualBox version: 6.0.14
Instructions:
install VirtualBox for macOS
download SDK from the VirtualBox website (i.e. https://download.virtualbox.org/virtualbox/6.1.0/VirtualBoxSDK-6.1.0-135406.zip) and extract to $HOME/vboxtest/sdk
mkdir $HOME/vboxtest
create a python2 venv:
cd $HOME/vboxtest
virtualenv venv_vbox -p python2
source venv_vbox/bin/activate
install dependencies into the venv:
wget https://netix.dl.sourceforge.net/project/pyxml/pyxml/0.8.4/PyXML-0.8.4.tar.gz
pip install PyXML-0.8.4.tar.gz
pip install ZSI
disable auth (for testing): VBoxManage setproperty websrvauthlibrary null
install SDK python module:
cd $HOME/vboxtest/sdk/installer
export VBOX_INSTALL_PATH=/usr/local/bin/virtualbox
export VBOX_SDK_PATH=$HOME/vboxtest/sdk
python vboxapisetup.py install
start API webserver: /Applications/VirtualBox.app/Contents/MacOS/vboxwebsrv -t 0
start the actual demo script:
cd $HOME/vboxtest
source venv_vbox/bin/activate
cd $HOME/vboxtest/sdk/bindings/glue/python/sample
export VBOX_INSTALL_PATH=/usr/local/bin/virtualbox
export VBOX_SDK_PATH=$HOME/vboxtest/sdk
python vboxshell.py -w
You should be getting a virtualbox shell, in which you can i.e. list all vms:
$ python vboxshell.py -w
Running VirtualBox version 6.0.14
vbox>
vbox>list
Machine 'tmp_default_1572559409192_87222' [32844f81-f971-42f8-99d8-f3edbf0bf637], machineState=PoweredOff, sessionState=Unlocked
Machine 'c4-2' [891a3509-fbc5-44be-816d-5045dd626157], machineState=PoweredOff, sessionState=Unlocked
vbox>
For more infos check the SDK docs: https://download.virtualbox.org/virtualbox/SDKRef.pdf
(IMHO should be used with caution as both Python2 and ZSI are EOL).
Five years later, things haven't progressed much it seems.
Under Mojave, I had brew installed python2 (2.7.15) and python3 (3.7.0), and I had to do the following:
cd /Applications/VirtualBox.app/Contents/MacOS/sdk/installer
sudo VBOX_INSTALL_PATH=/Applications/VirtualBox.app/Contents/MacOS /usr/local/bin/python2 vboxapisetup.py install
sudo VBOX_INSTALL_PATH=/Applications/VirtualBox.app/Contents/MacOS python3 vboxapisetup.py install
The built-in Mojave version of Python (/usr/bin/python version 2.7.10) was the only one that could import vboxapi after the VirtualBox install. I had to add the other Python versions manually.
The only instruction regarding this that I could find comes on page 40 of the SDK ref, where it suggests:
# cd VBOX_INSTALL_PATH/sdk/installer
# PYTHON vboxapisetup.py install
But, of course no mention of an environment variable. As Tal points out in the accepted answer, interesting that they can be bothered to write a 440 page SDK manual, and yet fail to mention this.
Simple approach using virtual environs
I try to only use python 3. For pyvbox API on Mac OS I'm using a second virtualenv which is python 2 Python2 venv
Base set up:
cd /Applications/VirtualBox.app/Contents/MacOS/sdk/installer
export VBOX_INSTALL_PATH=/Applications/VirtualBox.app/Contents/MacOS
sudo -E python3 vboxapisetup.py
cd $HOME
mkdir vbox
virtualenv -p /usr/bin/python venvpy2
source venvpy2/bin/activate
pip install virtualbox
I still pretty much only use python3, then use subprocess.Popen initialise venvpy2 and run python2 module. Take stdout and stderr back into main controlling python3 code. Having got this to work, I've concluded better approach for my use case is use command line vboxmanage Automate box server setup
I have a Python script that I'd like to compile into a Windows executable. Now, py2exe works fine from Windows, but I'd like to be able to run this from Linux. I do have Windows on my development machine, but Linux is my primary dev platform and I'm getting kind of sick of rebooting into Windows just to create the .exe. Nor do I want to have to buy a second Windows license to run in a virtual machine such as VirtualBox. Any ideas?
PS: I am aware that py2exe doesn't exactly compile the python file as much as package your script with the Python interpreter. But either way, the result is that you don't need Python installed to run the script.
As mentioned by other answerers, the cross-compilation feature is removed from PyInstaller since 1.5. Here, show how to package a Windows executable from Python scripts using PyInstaller under wine.
Step 1: Install wine and Python
sudo apt-get install wine
wine msiexec /i python-2.7.10.msi /L*v log.txt
PS:
Newer Python versions already include pip (is used to install pyinstaller). Download Python installation package from here (e.g., python-2.7.10.msi)
For macos users, use brew cask install xquartz wine-stable.
Step 2: Install PyInstaller on wine
$ cd ~/.wine/drive_c/Python27
$ wine python.exe Scripts/pip.exe install pyinstaller
Successfully installed pyinstaller-3.1.1 pypiwin32-219
Step 3: Package Python scripts
Package Python scripts (e.g., HelloWorld.py) with pyinstaller.
$ wine ~/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile HelloWorld.py
# filename: HelloWorld.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print('Hello World!')
The Windows executable file is located in dist/.
$ wine dist/HelloWorld.exe
Hello World!
fixme:msvcrt:__clean_type_info_names_internal (0x1e24e5b8) stub
Refer to here for the detailed description.
Did you look at PyInstaller?
It seems that versions through 1.4 support cross-compilation (support was removed in 1.5+). See this answer for how to do it with PyInstaller 1.5+ under Wine.
Documentation says:
Add support for cross-compilation: PyInstaller is now able to build Windows executables when running under Linux. See documentation for more details.
I didn't try it myself.
I hope it helps
You could run Windows in VirtualBox in order to run py2exe. VBox offers a powerful command-line client for automating tasks, so it something that you could likely integrate into your development process with ease.
Tested on Platform: Kubuntu 20.04, wine 6.0, python38
Download wine and python
Download windows version of python from https://www.python.org/downloads/release/python-3810/
Install wine
sudo apt install wine
Open your terminal and run
wine the-python-exe-you-downloaded
Run
find ~/.wine -name pip.exe
this will give you the pip path:
/home/yourusername/.wine/drive_c/users/yourusername/Local Settings/Application Data/Programs/Python/Python38/Scripts/pip.exe
Install pyinstaller
Run wine /home/yourusername/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pip.exe install pyinstaller
Package your file
Find installation path
find ~/.wine -name pyinstaller.exe
wine /home/yourusernmae/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pyinstaller.exe --onefile yourpythonfile
I wrote a blog post on how to do this with PyInstaller. Here's the summary:
How to create EXEs for Python on Linux, using PyInstaller and WINE
Download Python 3.8 Windows installer
wine python-3.8.9.exe, then see instructions below
wine C:/Python38/python.exe -m pip install --upgrade pip
wine C:/Python38/python.exe -m pip install -r requirements.txt, and requirements.txt should include PyInstaller itself
wine C:/Python38/Scripts/pyinstaller.exe ...
All done!
Installing Python 3.8 in Wine:
Check "Add Python 3.8 to PATH"
Click "Customize installation
Click "Next"
Click "Install for all users"
Set the install location as C:\\Python38
Click "Install"
Close the window.
Feel free to read the post to get more details.
I have tested py2exe inside of wine, and it does function. You'll need to install python in wine for it to work, or if you only use the standard libarary, you can bundle py2exe with py2exe from the windows machine and then use it in wine. Just keep in mind you need the same version of the ms visual C libraries in wine as were used to compile python or things won't work properly.