Selenium automation script packaging exe file

Recently, I wrote a selenium automation script due to work needs, but I have to open Pycharm every time I run it, so I directly use the Python third-party packaging library PyInstaller to package the py file into an executable exe file, and there is no need to install Python when using it You can also click Run.

PyInstaller is a relatively simple and convenient packaging kit. It only needs a few lines of commands to generate an exe file. It should be noted that different parameters will cause the size of the exe file generated after packaging to vary.

The following is a description of its functions in the official PyInstaller documentation.

PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.

PyInstaller reads Python scripts written by you. It analyzes the code to discover any additional modules and libraries required for script execution. Then gather a copy of all these files - including the active Python interpreter! and put them in a single folder with your script, or optionally a single executable.

Let's show how to use PyInstaller.

1. Python does not include the PyInstaller module by default, so you need to install the PyInstaller module yourself. Use the pip tool to install pyinstaller, open the cmd command line and enter

pip install pyinstaller

2. Execute pyinstaller and package it into exe, cmd command execution:

The first step: the file path where the cd /d py file is located

Step 2: Execute the following command

pyinstaller -Fw --icon=ico.ico weijian.py

After entering the command and pressing Enter to execute it, you need to wait for a few minutes to package dependencies and other processes. After seeing the following text prompt, the packaging is successfully completed.

Like the pip command to install a third-party library, it is generally successful when you see successfully.

Explanation: Add -F to pack all dependent packages such as selenium into run.exe, –icon is to set the icon of the packaged exe file, and the ico image can be generated using an online format conversion website, or can be searched through network resources, or Made by myself through ps. For more parameter settings during packaging, you can refer to the official documentation for proper selection, generally in the options section of the official documentation Using PyInstaller — PyInstaller 5.3+gc36f03d2 documentation .

 After the above steps are completed, we observe the folder and find the following generated files to confirm that the generation is successful.

The files in the build can be deleted after the build is successful, and there is no need to keep them, and the usage will not be affected. 

The following files need to be prepared before generation:

You may find that your .exe is very large, and the execution is very slow. This means that a lot of things we don’t need are included in the packaging. At this time, you need to combine the virtual environment to package. The specific operation is not here. I will repeat it, if any netizens need it, please comment in the comment area and I will make it up.

It is also necessary to pay attention here because selenium needs the browser driver to run correctly, and the driver cannot be packaged together during the packaging process, so we need to put the browser driver and the exe file in the same file before running the program folder.

 Click to test the execution effect of the exe file:

I'm sorry that the first execution failed. There are related dependencies that were not successfully packaged during the packaging process. It may be the reason why I use PyCharm, so the following will show you how to use PyCharm to complete the packaging process.

First find the path of the third-party library Pyinstaller used for packaging.

Pyinstaller support configuration in pycharm

Step 1: Settings——>Tools——>External Tools, click the plus sign in the upper left corner

Step 2: Attribute configuration

name: Pyinstaller EXE (customizable)

Program: The path where the pyinstaller.exe file is located, you can refer to the following path

( C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\Scripts\pyinstaller.exe)我的为:D:\Anaconda\Scripts\pyinstaller.exe

arfuments:-F --icon=ico.ico $FileNameWithoutExtension$.py

working:$FileDir$

Step 3: Save the settings and exit

After clicking the program that needs to be packaged, in Tools——>External Tools——>pyinstaller

The execution process is as follows

When you see the last word is successful, you will know that the package is complete, and the file manager enters the folder where your package program is located

open dist folder

run the program

ok, you're done, a simple Python application installation package is packaged successfully.

This article mainly explains the installation of the Python packaging tool PyInstaller and the detailed method of pycharm configuration to support PyInstaller. For more knowledge about the Python packaging tool PyInstaller, please go to the official documentation to find the answer.

Guess you like

Origin blog.csdn.net/anmin8888/article/details/126150038