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.
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 have currently used the python Package Larch (https://pypi.python.org/pypi/larch) on Windows. It works nicely and I am really impressed with its performance. To use the package with some large models I want to install it on a Linux server (Ubuntu 14.04.4) where I do not have the root permissions. Though knowing that the OS requirements for the package are Window or MacOS and being a beginner with Linux, I still gave it a try.
I was able to install python locally using anaconda distribution but when I used pip to install larch, I got the following message
Could not find a version that satisfied the requirement larch (from version: )
No matching distribution found for larch
So it seems to me that I can not just deliberately try to install the package on linux with out any additional work. I then tried to find solutions for my issue but I could not find any. Therefore I would like to ask if there is any workaround for my problem?
Thank you so much for your help!
[Edit]
larch does not support python2.x, and only support Windows, MACOSX.
For installing in Linux, I suggest to use wine to run windows python program with larch.
For more information about wine in Ubuntu. You can search with keyword such as How To Run Windows Software on Ubuntu with Wine
For installing in macosx. Making sure your environment have python3. You can use which python3 to check that.
$ which python3
/usr/local/bin/python3
Then, without root permission, you can install package via virtualenv. (A workstation should provide this command)
$ virtualenv -p $(which python3) env
$ . env/bin/activate
(env) $ python -V
Python 3.x.x
And finally, you can install larch via pip.
(env) $ pip3 install larch
For more information about virtualenv
"Larch can be installed from source code. If not using Anaconda, this is necessary for Linux, and can be done for other systems as well.
The latest releases of the source code will be available from Larch releases (github.com). In addition, you can use git to grab the latest development version of the source code:git clone http://github.com/xraypy/xraylarch.git" After unpacking the source distribution kit, installation from source on any platform is:
python setup.py install
source:http://cars.uchicago.edu/xraylarch/installation/index.html
edit: See this for doing this without root access
How to install python modules without root access?
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.
We use Sphinx for documentation within our Linux/OSX python environment, but we have someone on Windows who would like to be able to get an environment in which they can edit and build the Sphinx documentation.
Is it possible to set up Sphinx on windows? I'm guessing the hard part is installing a python environment and all the dependencies, but I have 0 experience with python on windows so somebody might be able to point me to a straightforward way to proceed.
For installing python on windows download Python. Run the executable and you are all set.
If you have pip installed then you can open command prompt and just type
pip install -U Sphinx
If you don't have pip installed then you first need to install it by using the following at the command prompt
python -m pip install -U pip
You can use pip-win now to set up sphinx in a virtualenv on windows easily.
First, download and install Python. If you want to use the latexpdf builder you will also need to install either TeX Live or MiKTeX for windows.
Next, run the pip-win.exe. It will automatically pull the required setuptools required and install pip and virtualenv on your system. When loaded it will pull up the Python you just installed, but if you installed it in a location other than default directory you can easily enter the absolute path or browse for it.
Next, in the Command: line you can create a new virtualenv by inputing:
venv -c C:\foo\sphinx-venv
This will open a command prompt with you inside your virtualenv. Following this you can install sphinx with the pip command:
<sphinx-venv> C:\foo\> pip install sphinx
Now sphinx is installed on your windows machine in a virtual enviroment. You can use deactivate to escape your new environment.
When ever you wish to enter your virtualenv again to build more sphinx projects you can either open pip-win and run:
venv C:\foo\sphinx-venv
Or you can create a custom python file to activate your sphinx environment and build your sphinx file such as:
import os
os.system("cmd /c \"CALL C:\\foo\\sphinx-venv\\Scripts\\activate.bat && make html\"")
By far the simplest way to use Python on Windows is WinPython, which is a portable distribution that is as simple to use as any other portable app. It'll give you an icon for a WinPython command prompt from which you can use pip, etc., as if on any other (e.g. linux) system, or you can download packages and install them using WinPython's control panel.
I'm new to Python. According to the internets I was looking for the module pyserial after receiving this error:
ImportError: No module named serial
I first tried to install pywin32, it went well. But it seems not to contain pyserial. :-(
Then I found a single module installer for pyserial, I was not able to install it, it says it did not found the path to python in the registry. :-(
After that I found this module on python.org, but I don't know what to do, it does not come with an installer. :-(
How can I add pyserial to Python (64) 2.7 on Windows 7 64?
You could try it with pip. (Here a question/answer about installing it)
Then type in your shell:
pip install pyserial
Solution:
The installation of pip on Windows 7 64 is a little tricky:
I added my Python and Python/Scripts path to the PATH env.
Then I opened PythonWin as Administrator, ran ez_setup.py from http://pypi.python.org/pypi/setuptools#downloads with PythonWin. And then I downloaded pip unpacked it and as Administrator I ran python setup.py install on the windows shell and after that pip install pyserial.
From http://www.instructables.com/id/The-Arduino-Internet-Gizmo/step19/Installing-the-software/
Install pySerial as follows:
Download pySerial from http://pypi.python.org/pypi/pyserial - click on pyserial-2.6.tar.gz to download the library.
Run 7-Zip (Start -> All Programs -> 7-Zip -> 7-Zip File Manager).
Open the pyserial-2.6.tar.gz file with 7-Zip (File -> Open).
Double click the "dist" folder.
Single click on the "pyserial-2.6.tar" file and click the "Extract" button at the top of the window. When asked where to put the file, specify c:\Python27\Lib\site-packages\pyserial-2.6
Exit from 7-Zip
Now that you have downloaded pySerial, install it:
Open a command window (Start -> All Programs -> Accessories -> Command Prompt) and type into the command line: cd c:\Python27\Lib\site-packages\pyserial-2.6
Install pySerial by typing this command (including the path): c:\Python27\python.exe setup.py install
understood that this question is for Windows, but since it comes up first in a Google search let me add an alternative (to pip) for Debian:
sudo apt-get install python-serial
As a slight update there is still the problem that python 64bit for Windows is not compatible with the pyserial installer.
What is not mentioned in other answers but I found very useful to know is that Python 32 bit installs and works fine on Win7 64bit and that means the pyserial installer also works fine.
Obviously if you need 64bit python this is no use to you, but I suspect most of us do not.
please just go to http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyserial and download the 64 bit installer and that's it!
reviving an old thread but pyserial v3.4 is now available for windows/mac/linux via the conda package manager on the conda-forge channel
(the base conda channel has v2.7)
To install this package with conda run:
conda install -c conda-forge pyserial
pyserial comes now with a precomilied(?) Windows .exe installer: https://pypi.python.org/pypi/pyserial
pySerial can be installed from PyPI:
python -m pip install pyserial
NOTE : Using the python/python3 executable of the desired version (2.7/3.x).
I find the 2.7 installation files here
https://pypi.org/project/pyserial/2.7/#files