Python3.4 Py2exe, error - name 'WinDLL' is not defined - python

I use Ubuntu, Python 3.4 and try to compile my py scripts to exe using py2exe.
Unfortunately When I try to use command 'build_exe', it generates error!
Could anybody help me to solve it or advise how to compile py project to exe in another way?
Error code:
NameError: name 'WinDLL' is not define.

py2exe is for windows...
On linux you can try PyInstaller:
http://www.pyinstaller.org/
PyInstaller is now able to build Windows executables when running
under Linux. See documentation for more details.

The py2exe module needs to be run from within a command prompt shell (aka Windows terminal) to make a Windows executable, since just like pyinstaller, "they use the OS support to load the dynamic libraries, thus ensuring full compatibility". That's why you get the ''WinDLL' is not defined' error when using the py2exe module in a bash shell.
py2exe works until python-3.4 and pyinstaller works until python-3.7. The latest version at time of writing is 3.8. Use the python module virtualenv from command prompt to get the adeqaute python version running (without replacing your current python set-up), and make your executable file. Here is my answer with code. --> Make sure to add python to your Windows path on install, so you can use the pip and python commands in command prompt. Otherwise you have to replace these commands in my answer for their fullpath, e.g. C:\Users\jakethefinn\python37\pip.exe and C:\Users\jakethefinn\python37\python.exe respectively.
If you install python from Microsoft Store, the files pip.exe and python.exe are automatically added to path.

Related

How does the Windows Python Launcher (py.exe) find the python executable(s)?

I'm trying to use the Windows Python Launcher (py.exe) for the first time. I run python in a command shell but the launcher, running in the same shell, can't find any version of python.exe to run:
where python
C:\Python38\python.exe
py --list
Installed Pythons found by py Launcher for Windows
No Installed Pythons Found!
Obviously there is something missing in my configuration but I'm at a loss as to what. I've been digging into the registery and have located keys in:
HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/Python/PyLauncher
HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/Python/PythonCore
but there isn't anything that references any executables.
Any help appreciated!
You was on the right path. In your case, you would need to add the the following entries in the registry:
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.8]
[HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.8\InstallPath]
#="C:\\python38\\"
"ExecutablePath"="C:\\Python38\\python.exe"
"WindowedExecutablePath"="C:\\Python38\\pythonw.exe"
Also answered in py launcher does not find my Python 2.7

Pyinstaller exe not working on other computer(with other windows ver.)

My platform is Windows 10 and Python 3.9. There is another computer(Windows server 2008R2) without Python. So I'd like to use pyinstaller in my computer and use .exe on the other computer.
I tried simple script print("hello") and used pyinstaller -F myscript.py
.exe works on my computer, but failed on the other computer.
Error
error loading python dll ~ python39.dll
Should I use Python 3.8? Or what should I do?
The problem is that Pyinstaller does not create fully standalone executables, it creates dependencies (E.g. this python39.dll), so this python39.dll should be on the computer which is running this executable. Because python is already installed on your computer, python39.dll is already there and everything works fine. The problem is that machine that you're running this program on probably won't have it.
To fix this there are several solutions:
Install python 3.9 on targets' machine (But in this case you don't need to create an executable)
Include python39.dll with your program
For second solution just create a folder and move your executable into it as well as this python39.dll library. Windows will find it because it's in the same directory where this executable is. You can get this library from c:\Windows\System32 folder (Or where all DLL's are stored on your system) and then just copy it into folder with your executable. After that ship not just executable but this folder with library included.
#Stepan wrote in comments that you can also include this library right in your executable by adding --add-binary "path\to\python39.dll" to your command when compiling. The final command will look like this:
pyinstaller -F --add-binary "c:\Windows\System32\python39.dll" myscript.py
Check if the Python version is compatible with the windows version you are trying to use. I was having this problem with an exe I did using Python 3.10. Did it again with Python 3.7 and it worked.
In such cases it could be a solution to use something like auto-py-to-exe wrap for pyistaller: it knows better which option to set for py converting :)
Also, from my exp, in some cases you should modify yout already normally working from terminal Py code before pyinstaller: for example replace exit() with sys.exit() and so on.

Packaging a Python script that can be run on a computer that doesn't have Python installed?

I have a Python script (that manipulates files on a shared drive). I would like to turn that script into a file (maybe .exe?) that perhaps can be double-clicked and run on a machine that doesn't have Python installed. Is that possible/easy to do?
You can try py2exe.
Tutorials can be found at http://www.py2exe.org/index.cgi/Tutorial
I use and recommend pyinstaller. If you need only one executable file, you need to install pyinstaller and call executable passing these arguments:
pyinstaller.exe your_script.py --onefile --windowed
This options can be used when you generate the executable file: https://pythonhosted.org/PyInstaller/usage.html#options
To install: http://www.pyinstaller.org/
Documentation: https://pythonhosted.org/PyInstaller/

Windows: run python command from clickable icon

I have a python script I run using Cygwin and I'd like to create a clickable icon on the windows desktop that could run this script without opening Cygwin and entering in the commands by hand. how can I do this?
The simplest solution will be creating batch file containing command like:
c:\python27\python.exe c:\somescript.py
With this solution you will have to have installed python interpreter. If you need more portable solution you can try for e.g. py2exe and bundle python scripts into executable file, able to run without requiring a Python installation.
This comes straight from Python Docs (https://docs.python.org/3.3/using/windows.html):
3.3.5. Executing scripts without the Python launcher
Without the Python launcher installed, Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.
You can also make all .py scripts execute with pythonw.exe, setting this through the usual facilities, for example (might require administrative rights):
Launch a command prompt.
Associate the correct file group with .py scripts:
assoc .py=Python.File
Redirect all Python files to the new executable:
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
This tutorial shows how to create a batch file that runs a python script.
Note that many of the other answers are out of date - py2exe is out of support past python 3.4. More info here.
As an alternative to py2exe you could use the pyinstaller package with the onefile flag. This is a solution which works for python 3.x.
install pyinstaller via pip
Package your file to a single exe with the onefile flag
pyinstaller --onefile your_file.py
A better way (in my opinion):
Create a shortcut:
Set the target to
%systemroot%\System32\cmd.exe /c "python C:\Users\MyUsername\Documents\MyScript.py"
Start In:
C:\Users\MyUsername\Documents\
Obviously change the path to the location of your script. May need to add escaped quotes if there is a space in it.
The solution that worked like a charm for me >
From https://www.tutorialexample.com/convert-python-script-to-exe-using-auto-py-to-exe-library-python-tutorial/
pip install auto-py-to-exe
The GUI is available just by typing:
auto-py-to-exe
Then, I used this command to generate the desired output:
pyinstaller --noconfirm --onedir --windowed --icon "path/favicon.ico" "path/your_python_script"
Now I have my script as executable on taskbar

Configuring Pycharm to run Pyinstaller

Yes I want to create a run configuration in PyCharm to run Pyinstaller and get my executable. According to the Pyinstaller documentation you should be able to locate an python script called pyinstaller-folder/pyinstaller.py after the installation, but it wasn't there. Then I look carefully and found this other one named pyinstaller-folder/__main__.py which should be the same <--(me wild guessing), so I set up my running configuration like this:
After running it, is giving me this error:
/usr/local/Cellar/python3/3.4.3/bin/python3.4 /usr/local/lib/python3.4/sit
e-packages/PyInstaller/__main__.py --onefile --nowindow --osx-bundle-identifier=jg.optimizer -F --name=genoptimizer optimizer/manage.py
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/PyInstaller/__main__.py", line 26, in <module>
from . import __version__
SystemError: Parent module '' not loaded, cannot perform relative import
Process finished with exit code 1
It seems to require a parent module to run but, how would that look like?
After more than two years, perhaps there is a better option.
In the PyCharm menu go to File -> Settings.
In the Settings dialog find Tools -> External tools and use the green + to add a new external tool.
For example:
Then, the IDE will allow you to run it on any python script. Right click on the file and the context menu will show External tools -> PyInstaller.
The PyInstaller package is a runnable module and can be run using python -m PyInstaller. To configure it as a run target in PyCharm, leave the "Script" field blank, write -m PyInstaller in the "Interpreter Options" field, and put the PyInstaller parameters into the "Script Parameters" field.
For example:
PyCharm will complain that the "Script" field is empty, but it will let you run the configuration anyway.
The run command has changed a bit since the accepted answer. You can now select a module to launch rather than editing the interpreter options.
As of writing this answer, here is how it looks like:
Notes:
This solution requires to install PyInstaller in the virtual environment of the project.
I am using PyCharm pro 2020.1
Old solution should still work
Using external tool is still a possibility. Which solution you choose is a matter of personal preference.
Install pyinstaller in pycharm, then open the pycharm terminal and write python -m PyInstaller.
So as Pycharm has newly updates, my case was a bit different as I installed pyinstaller from the interpreter settings as the following picture shows:
For Linux Users:
You could install it in both Python 2.7 or Python 3.7+. Make sure to get the path of where pyinstaller was stored.Then in the Settings option, try to find Tools -> External tools and add a new external tool as the following picture shows:
For Windows users:
If you are using Pycharm or any virtual environment. Unfortunatelly Pycharm creates its local vertual environment in venvpath once you indicate the interpreter. So, you should set the external tool (pyinstaller) to the real path of your python 3.7 .exe as the picture shows
For those of us on Windows with Anaconda trying to figure this out, I found it easiest to just set up a Bash Configuration (I believe you need the BashSupport plugin for this), and set:
Script: pyinstaller (assuming pyinstaller is in your path, if not, the full path)
Interpreter path: C:\Windows\system32\cmd.exe (yes, a bash configuration can just use the standard command program)
Interpreter options: /C
Program arguments: script_name.py (along with any other pyinstaller arguments)
Working Directory: C:\Path\To\Script

Categories

Resources