banner



How To Add Chromedriver In Build Pyinsatler

Make a program created with Python & Selenium into an executable format (.exe).
In particular, this fourth dimension nosotros will make one exe file.
Information technology is easy to use PyInstaller, just as it is, WebDriver (in this article, chromedriver.exe) is non included in the exe. The fact that it is not included in the exe means that when yous distribute the executable file, you must too distribute the WebDriver. I didn't like this, so I investigated how to put it in an exe.

Execution environment

  • Windows 10
  • ChromeDriver 76.0.3809.68
  • Python 3.six.2
  • selenium iii.141.0
  • PyInstaller 3.5

Make information technology executable with PyInstaller

The basic usage of PyInstaller is explained on the post-obit page:

  • [PyInstaller]Brand Python an executable file

There are ii things to keep in mind when using PyInstaller:

  1. Specifying PyInstaller options
  2. Specifying the path in the source

Specifying PyInstaller options

The following folder structure is causeless:

            MyProj/  ├ commuter/  │ └ chromedriver.exe  └ master.py          

Information technology is fabricated executable by PyInstaller, simply with --add-binary as an choice. This is specified when you lot want to include a binary file other than Python (this time chromedriver.exe) in the exe.

Use the following command (replace each path):

            pyinstaller ./master.py --onefile --noconsole --add-binary              "./driver/chromedriver.exe;./driver"                      
  • --onefile  It outputs as one exe.
  • --noconsole  The console is not displayed when the created exe is executed.
  • --add-binary  Specify the binary file you want to add to the exe. The specification method is "source_file_path; destination_file_path".

The method of specifying the --add-binary option is a little confusing, simply beginning it is easier to understand if you build without the --onefile option. If you build without the --onefile selection, it volition be output in multiple files.
For example:

            dist/  └ main/    ├ driver/    │ └ chromedriver.exe    └ primary.exe (* Really in that location are many other folders and files.)          

Y'all tin can exercise the same thing by writing information technology in the spec file without specifying it as a command line choice.

                          a              =              Analysis              (              ...              binaries              =              [              (              './commuter/chromedriver.exe'              ,              './driver'              )              ],              ...                      

The spec file is created automatically when PyInstaller is executed, and then information technology is recommended to modify the created 1 and use it.
If there are multiple files yous want to add, the spec file is easier to employ.
When building with a spec file:

The to a higher place settings are fine for PyInstaller settings, just you may need to modify the source.
Described in the next section.

Specifying the path in the source

When using the --onefile option, the binary file specified with--add-binary is included in the exe. They are expanded to a temporary folder at runtime. Therefore, using relative paths in the Python source may not work.

For example, information technology is necessary to rewrite the office using the relative path equally follows:

                          driver              =              webdriver              .              Chrome              (              './commuter/chromedriver.exe'              )                      

Rewrite equally follows (import the necessary modules):

                          def              resource_path              (              relative_path              ):              try              :              base_path              =              sys              .              _MEIPASS              except              Exception              :              base_path              =              os              .              path              .              dirname              (              __file__              )              return              os              .              path              .              bring together              (              base_path              ,              relative_path              )              driver              =              webdriver              .              Chrome              (              resource_path              (              './driver/chromedriver.exe'              ))                      

Add the function resource_path to use it.
resource_path gets the path with the following logic:

  1. When executing from exe, get the relative path from sys._MEIPASS (when executing from exe, the temporary folder path is entered).
  2. When executing from Python, get the relative path from this file (__file__).

At present information technology works properly whether y'all run it from Python or an exe file.

At the stop

This is the end of the explanation.
Although it was an example of Python & Selenium, I think that it is the same even if you want to include paradigm files etc. in exe.


Related Posts


[Python] Replace text in Excel cells

How to convert Excel file to PDF with Python

How To Add Chromedriver In Build Pyinsatler,

Source: https://www.zacoding.com/en/post/python-selenium-to-exe/

Posted by: finleysionuirt.blogspot.com

0 Response to "How To Add Chromedriver In Build Pyinsatler"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel