Technical background
In many cases, programmers do their programming tasks in a Linux environment, but more users are in a Windows environment, let's say, as mentioned in the article at reference link 1:
Then we have to consider a question of environmental transformation.
python, as a general-purpose programming language, can itself run in different environments, but executables are not.
For now, the most convenient way is to compile the executable with pyinstaller on the respective platform.
Writing python scripts
When we compile the executable, we first need a python task script that can be executed, which can be a series of complex operations, displaying an image, or an entire complete piece of software for the PC.
One of the tasks we have written here is to create an image of a discrete sine function using numpy and then present it using matplotlib:
# import numpy as np import as plt x = (0,10,1000) y = (x) () (x,y,'o',color='green') ()
It is important to note that
If you are creating a python script that depends on some printout function, you need to specify a popup terminal window when compiling the executable, otherwise the program won't execute correctly.
Installing pyinstaller and compiling executables
pyinstaller is also a cross-platform python tool that can be installed and managed using pip on both windows and linux:
python3 -m pip install pyinstaller
Take linux platform as an example, after installing pyinstaller successfully, you can directly execute the following commands to compile python scripts:
$ pyinstaller -F ... $ ll total 24 drwxr-xr-x 4 dechin dechin 4096 Apr 20 14:26 ./ drwxr-xr-x 4 dechin dechin 4096 Apr 19 22:31 ../ drwxr-xr-x 3 dechin dechin 4096 Apr 20 14:26 build/ drwxr-xr-x 2 dechin dechin 4096 Apr 20 14:28 dist/ -rw-r--r-- 1 dechin dechin 160 Apr 20 14:21 -rw-r--r-- 1 dechin dechin 808 Apr 20 14:26 $ ll dist/ total 264376 drwxr-xr-x 2 dechin dechin 4096 Apr 20 14:28 ./ drwxr-xr-x 4 dechin dechin 4096 Apr 20 14:26 ../ -rwxr-xr-x 1 dechin dechin 270710928 Apr 20 14:28 plot*
We can see that after successful compilation, the build and dist folders are generated under the current path, where the executable is placed under the dist path.
You can run the executable directly under linux using the . /plot command to run the executable file, with the following results:
Commonly used commands about pyinstaller include commands that specify pop-up terminal windows (for outputting program printouts) and commands for programs to display logos (typically ico files):
pyinstaller -F -w # Black dialog box pops up pyinstaller -F -i ico_file.ico # Specify the display icon
After finishing the compilation in linux environment, we can see that the generated executable file can only run in linux environment as well.
In order to generate an exe executable file that can be run in a windows environment, we switch to windows to run it:
Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2022/4/20 14:28 dist d----- 2022/4/20 14:26 build ------ 2022/4/20 14:26 808 ------ 2022/4/20 14:21 160 PS > pyinstaller -F .\ ... 47314 INFO: Appending PKG archive to EXE 62696 INFO: Building EXE from completed successfully. PS > ls .\dist\ catalogs: \\\Ubuntu-18.04\home\dechin\projects\project0331\dist Mode LastWriteTime Length Name ---- ------------- ------ ---- ------ 2022/4/20 14:28 270710928 plot ------ 2022/4/20 14:40 36811075
The same method of running, after the completion of the compilation in the dist folder to generate a new executable file , the execution results are as follows:
Running software in a windows environment often pops up some windows, we just let it go:
As you can see, although there is a slight visual difference from the results of running the executable under linux, it's still basically the same.
Summary outline
After completing the implementation of a software or program, the last step is to release, the purpose of the release is to allow more people to use the project, and most people do not have the appropriate programming environment, so we must consider compiling the code into the various platforms of the executable file, and then released to the user to use. This article mainly introduces the python script through pyinstaller in linux and windows were packaged as executable files, want to cross-platform compiler software on the realization of the current is still relatively difficult, the blogger's local use of Win11 + WSL2 Ubuntu Linux environment, to a certain extent, to solve the problem of cross-platform publishing! The blogger is using Win11+WSL2 Ubuntu Linux environment, which can solve the problem of cross-platform distribution to some extent.
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.