I have created a small web scraping app using Selenium & chromedriver for a project that outputs the content into an excel file. The people I did this app for unfortunately aren't the most tech-savvy.
So my question is how can I share this app with these people?
I looked into py2exe.org, but it doesn't take the chromedriver into account when creating the executable. Any better ways of doing this, without these people having to add the files manually to their "usr/bin"?
You can do this with the help of pyinstaller : Below is the solution which work on Windows but pyinstaller says its capable of working on Mac OS also.
Steps are:
Open Command prompt
Go to the project path in cmd where the script is present
type pyinstaller Scriptname.spec Scriptname.py (Enter y/yes if prompt on screen)
The build will be at 'path to project'\dist\Scriptname
Note you need to provide the details of chromedriver in Scriptname.spec when passing the
Sample content of spec file:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['Scriptname.py'],
pathex=['Pathofproject'],
binaries=[('C:\\Python27\\chromedriver.exe', '**.\\selenium\\webdriver**')],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='createEVIPOrg_Automation_new',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='**scriptname**')
You need to update the Scriptname, project path where you script lies, path of chromedriver in spec file
Related
Mac Big Sur, python 3.9, pyinstaller 4.3.
I've seen this question posted elsewhere, e.g. PyInstaller OS X app runs from command line, but not Finder window, but can't quite understand the proposed solutions. I have a Mac .app created using tkinter and pyinstaller that functions fine at the terminal when I type
./dist/MyApplication.app/Contents/MacOS/MyApplication
However, when I double click on the .app in the Finder, the program icon appears briefly on my computer's dock before disappearing. No error message at all.
Here is my .spec file:
block_cipher = None
a = Analysis(['MyApplication.py'],
pathex=
['/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages',
'/Users/fishbacp/Desktop'],
binaries=[],
datas=[('/Users/fishbacp/Desktop/background.png','.')],
hiddenimports=['_tkinter','PIL'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='MyApplication',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='MyApplication')
app =
BUNDLE(coll,name='MyApplication.app',
icon='/Users/fishbacp/Desktop/spectrum.ico',
bundle_identifier=None,
info_plist={'LSEnvironment': {'LANG': 'de_DE.UTF-8',
'LC_CTYPE': 'de_DE.UTF-8'}})
I had same problem (but used one folder mode instead of packaging as an .app), when double click in finder, it will open console and immediately close it.
And my case was due to uncaught exception in my program, which failed to load a file from the folder, and correct the path fixed the problem.
from pathlib import Path
# The file path should be relative to the packaged folder path
file_path = Path(__file__).resolve().with_name("some/path/to/the/file")
It might not be your case but my guess is your program exists due to unexpected condition and that's why it disappears soon, and file path is quite often not handled properly when using PyInstaller.
Hope it can help someone.
I writed a program with python which using selenium library. Basically, I am opening a page using Chromedriver.exe and login to a site. I want to use this program in another computer without use python. I want to convert it to a stand-alone exe type.
I tried pyinstaller --onefile filename.py
I got a dist folder which include .exe file. Also I have an .spec file. When I try to run exe file, nothing happen. Just a command panel is opening. How can I run this exe file?
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['borsaBot_Zpro.py'],
pathex=
['C:\\Users\\90544\\OneDrive\\Masaüstü\\Yusuf\\ödevler\\borsaBot'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='borsaBot_Zpro',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
I am using Kivy for GUI and developed an desktop exe successfully. The code runs fine which starts camera and even it records video and saves it. But when I create exe, recording is not working. And recorded video needs to be saved but it just saves the file which is actually empty.
I am using windows 10, Anaconda 3.5.5.
this is .spec file
# -*- mode: python -*-
from kivy.deps import sdl2, glew, gstreamer
block_cipher = None
a = Analysis(['ninth.py'],
pathex=['D:\\Somu_Sir\\new_project'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='ninth',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )
coll = COLLECT(exe, Tree('D:\\Somu_Sir\\new_project\\required_files\\'),
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins + gstreamer.dep_bins)],
strip=False,
upx=True,
name='ninth')
Application is starting but only record function is not working. Expected output should be a video stored in same folder
Yes you have to use binaries=[] where you need to pass the .dll files which are required for your application to run. Your code works fine that means your app should also work. only when creating .exe file using PyInstaller you need to give all required files as hiddenimports as well if you want only one single file. You can try binaries=[], where you can add number of .dll dependencies or even through cmd for example:
pyinstaller --add-binary <PATH_TO_PYTHON>\Lib\site-packages\cv2\opencv_ffmpeg320_64.dll
opencv_ffmpeg320_64.dll can help you to record camera videos
I have programmed a python script using tkinter and want to turn it into an exe file. I have tried running it as a .pyw file and it only runs the GUI window without the command line which is what I want. However, when using Pyinstaller to convert my script to a .exe file it opens and runs fine but the command line always opens as well. This has happened when converting both .py and .pyw files and it also occurs when I specify in the command prompt --windowed or --noconsole. I have even tried to edit the spec file of my .exe to see if the console is set to true but I find in the code "console=False". I cannot find any other fixes on previous questions or on the pyinstaller issus page and so I have had to ask myself.
This is the spec file
# -*- mode: python -*-
block_cipher = None
a = Analysis(['D:\\zebsu\\Documents\\simple_calculator_3.py'],
pathex=['C:\\Users\\zebsu'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='simple_calculator_3',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='simple_calculator_3')
I'm fairly new to Pyinstaller myself but the following command has worked for me:
pyinstaller -w YourPythonFile.py.
Hope this helps.
After updating the Pyinstaller spec file as suggested in the answer here (How to include chromedriver with pyinstaller?), chromedriver is still not being accessed from the generated app file. Could the issue be with .\\selenium\\webdriver? That was copied from the answer and I'm not sure it's specific to a Windows OS.
Running the UNIX executable in terminal works, accessing chromedriver.
The full spec file is:
# -*- mode: python -*-
block_cipher = None
a = Analysis([‘scriptname.py'],
pathex=['/Users/Name/Desktop'],
binaries=[('/usr/local/bin/chromedriver', '.\\selenium\\webdriver')],
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name=‘app name’,
debug=False,
strip=False,
upx=True,
console=False )
app = BUNDLE(exe,
name=‘appname.app',
icon=None,
bundle_identifier=None)
The line pyinstaller appname.spec scriptname.py --windowed --onefile is used in terminal to generate the app.
Yes, that was Windows path. In Unix, you need to use ./selenium/webdriver instead. It tells where to place the chromedriver binary in the bundle, so after pyinstall, chromedriver will be in /path/to/bundle/dist/selenium/webdriver.
Then in the code you should use something like this to reach it (it's a remote example):
dir = os.path.dirname(__file__)
chrome_path = os.path.join(dir, 'selenium','webdriver','chromedriver.exe')
service = service.Service(chrome_path) ...