Python Selenium GUI to an executable - python

I have never made an executable application before but from what I have read its pretty easy, using py2exe to generate the exe.
But I have a GUI that uses Selenium to scrape data from a backend (No I can not use API calls). How do I add chromedriver to the executable? Also, would all the imports go along when using a compiler?

When you compile a .py file into an .exe (from my personal experience) all of the imports are included.
I would personally suggest using pyinstaller. I had quite a few problems using py2exe and as a beginner I found pyinstaller much more user-friendly and easier to troubleshoot.
As compiling a file does not alter the .py file, I would suggest getting it to a fully working state and trying it. If it doesn't appear to work or if some of the imports are lost, we can troubleshoot with the error code.

You can also use cx_Freeze to create an executable from your python script.
You can install cx_Freeze by issuing the command
python -m pip install cx_Freeze --upgrade
in a cmd prompt / terminal.
As far as tkinter is concerned, you'll find a working example of how to freeze a tkinter-based application with the current version of cx_Freeze in this answer. In the setup.py script you find there, you need to replace the name of the Executable by the name of your main script. Place this setup.pyin the same directory than your main script and run
python setup.py build
from a cmd prompt / terminal.
As far as chromedriver is concerned, I've no experience, if you choose this approach and still have problems, please add the exact error message and a Minimal, Complete, and Verifiable example to your question.

Related

Packing python scripts into .exe file while keeping imported modules

I have a python script I want to pack into .exe file for the ease of use. The python script makes extensive use of Tkinter module for nice GUI. I've packed it first into .exe using pyinstaller and the guide outlied here - https://datatofish.com/executable-pyinstaller/
I have two problems. First is that my script makes use of FlowCal module, which doesn't come with Python's pyinstaller (I've made script using Spyder, and I'm using Python 3.8. to compile using pyinstaller) - so I installed FlowCal with pip install FlowCal so Python gets it too (I think that's how it works? Not too sure). But then FlowCal is dependent on various sklearn modules, and it would be a headache to install modules, compile to exe, then check if it works over and over. Is there a way that ALL modules script uses (and ALL modules that imported modules use) are compiled into the script?
Second problem is that alongside GUI I get another window. Picture included. How do I remove that window?
Another thing you can do without any hassle is using auto-py-to-exe. This will generate .exe from .py with writing command, just clicking some buttons in a GUI. For this, you have to give the command: pip install auto-py-to-exe in command prompt or PowerShell, whatever you like most. After successfully installing auto-py-to-exe, give the command auto-py-to-exe in your command prompt. Then give the necessary informations, and get your generated executable file!
Add the missing modules to the hiddenimports
hiddenimports=['sklearn.neighbors.typedefs','sklearn.neighbors.quad_tree','sklearn.tree._utils']
Or use it when you create the exe in cmd as
--hidden-import=modulename

How can I run a python script without python.exe

My goal is to have a python script that I can giving to someone to run on their windows machine that does not have python installed. I do want to package it up in an exe because I want the underlying code to be easily read.
I am updating an old VBscript and I want to mirror it. I am also using a few libraries.
use pyinstaller to package it up to an exe. You can still maintain your source code. Packaging it up wont remove your source code.

Python portable pyinstaller

I'm building an application where I'd like my users to be able to create executables with only a couple of clicks for some settings. It's a GUI application made with Tkinter where there is a page with some options. These options are then supposed to be written to a premade Python script. That will be no problem to make. The tricky part is compiling the script into an executable (.exe). I've always been using PyInstaller for making executables, but that's only on my own local computer. What if I want to pack my application and have PyInstaller bundled? I know PyInstaller requires pywin32, which means that has to be bundled too.
I'm completely lost on how to bundle PyInstaller with my application.
I've been doing a lot of googling on the matter, but can't seem to find any help at all. The closest I got was this post, which didn't help very much.
Try to use Nuitka. I tried some alternatives for Python (including PyInstaller) and like this one most. With Nuitka you can compile your script (and all modules it need) to standalone .exe file. Then you can create installer with many available options.
Here's user manual, here's example of command (I used for my project):
nuitka --standalone --recurse-all --recurse-stdlib --remove-output --windows-disable-console --recurse-directory=YOUR_PROJECT_DIR
Upd:
How to run nuitka inside script:
import os
import subprocess
project_dir = os.path.abspath('project')
project_main = os.path.abspath('project\\main.py')
subprocess.call([
'nuitka', '--standalone', '--recurse-all', '--recurse-stdlib', '--remove-output',
'--windows-disable-console',
# '--windows-icon={}'.format(icon_path),
'--recurse-directory={}'.format(project_dir),
project_main
], shell=True)

How to convert python .py file into an executable file for use cross platform?

I've been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don't understand much of the documentation, nor am I confident enough to experiment too much.
Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I'd appreciate knowing that too.
Running Vista 32bit, python 2.7
There are two distinct ways of freezing python scripts to create executables:
Packing the interpreter and *.pyc files into one exe file-container. Such an approach is used by tools like PyInstaller, Py2exe, cx_freeze.
Creating native code from Python source, usually using a middle step of converting Python-source to C or C++ code. This is done by such tools as Shed-skin and Nuitka. The problem of this aproach is that such tools do not always support all the functionality of Python (e.g. they can have some typing limitations and so on)
The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that's the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).
You can start with Py2exe tutorial and 'hello-world' to get acquainted with that how compilation is done. As far as I know it's a simplest way to get your goal.
And the last thing, you can't create cross-platform native executables as their file formats are strongly operating system and hardware dependent.
Download py2exe
Download this msvcp90.dll
Copy your FileCode.py AND msvcp90.dll to C:\Python27\
In C:\Python27\ create new text file, then enter this code inside it:
from distutils.core import setup
import py2exe
setup(console=['Avril.py'])
Replace Avril.py with YourFileName.py
Save the file as setup.txt
Open CMD and type this:
cd C:\Python27\
python setup.txt py2exe
Now go to C:\Python27\dist\ and there's your .exe program.
Source: Manvir Singh
Python scripts can be made directly executable, like shell scripts, by putting the python environment path in the top of the script file.
#!/usr/bin/env python3.5
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
Detailed description also for linux is here.
Install pyinstaller, a program that converts .py to .exe for python 2.7 to where python is located:
cd C:\python27\scripts
pip install pyinstaller
then move whatever python file you want to compile to C:\python27\scripts, compile from there by using:
pyinstaller --onefile yourfile.py
the --onefile is optional but it packages the whole thing(in this example yourfile.py) into one .exe. Once everything is done there will be 2 new folders along with a .spec file. From C:\python27\scripts open the folder dist. Your .exe will be located there in one file which you can double tap to execute and distribute to anyone who doesn't have python. Hope it helps.

Creating an executable file from python script

I have created a GUI program using python and wxpython. It is now ready to be turned into an executable file. I have tried for the last two days following various instructions from py2exe. All instructions for Command Prompt refer to older versions of windows and I'm using windows 7. I have never used Command Prompt before as I'm still new to programming. Is py2exe going to best way to create an executable file or is there a better option?
I have followed instructions on creating a setup.py file and it reads:
from distutils.core import setup
import py2exe
setup(windows = ["Core.py"])
When I enter the command in Command Prompt:
C:\Python27\Cylinderdrawer\python setup.py py2exe
I get the following:
'C:\Python27\Cylinderdrawer\python' is not recognized as an internal or eternal command, operable program or batch file
There are more than two options. You can use py2exe, PyInstaller, cx_freeze and bb_freeze. I enjoy using the GUI2Exe wrapper script that wraps all of these plus py2app (for Macs) as it makes tweaking the settings a breeze.
As far as I can tell, cx_freeze, PyInstaller and bb_freeze have the newest releases with py2exe's last release in 2008.
As for your issue, it sounds like you don't have Python on your system path. Try typing out the full path to your python instead. Something like this:
c:\python27\python.exe setup.py py2exe
I don't know what "Cylinderdrawer" is, but that is NOT a standard location for the Python executable on Windows.
See also:
a py2exe tutorial
a GUI2Exe tutorial
a cx_freeze tutorial
a bb_freeze tutorial
a PyInstaller tutorial
Your two options are py2exe and cx_freeze. py2exe is more widely used, and it can be tricky to get all the details right. If you provide details about what is going wrong, we might be able to help.
Thanks everyone for their help. As much as I have not worked it out completely yet, this problem has been solved. I'll ask seperatly about the subsequent problem if I cannot work it out. For anyone else having a similar problem the following is how I solved it.
C:\Python27\Cylinderdrawer\python setup.py py2exe
This was what I originally put into Command Prompt. Cylinderdrawer is my project folder. This is wrong it needs to be where python.exe is located. the next part, i.e., "setup.py" is the setup file. The only way I could get it to work was to explicitly state where the file was. Bellow is the command that worked.
C:\Python27\python "C:\Python27\Cylinderdrawer\setup.py" py2exe
Once again, thanks to all those who helped.

Categories

Resources