Pyinstaller Import Module



Share some tips and experiences here.Keep the author's information and article links for sharing, reprinting or citing your hard work.

Author's environment:

On Module imports like for example 1 import PyQt5.QtCore the pyinstaller seems to find automatically the corresponding hooks during analysis and does not add anymore errors to the log. PyInstaller is a package in Python which bundles all the dependencies of Python applications in a single package. We don’t need to install different packages or modules for different applications. PyInstaller reads and analyzes our code and then discovers the modules that our program requires in order to execute. I tried to freeze my application, which uses importlibresources, with pyinstaller. However, I get the following problem: Traceback. In the bad case, when I have dynamic import, I add -hiddenimport flag, PyInstaller run detects all submodules (again, with the hook) but will not detect submodule's dependencies. You can see that there's a huge difference in the number of modules went into each one of the binaries.

win7+python3.5(anaconda3)

Theoretically, systems with win7 and above and any version of python are available.

1. Basic scripts

First, we build a simple script, such as output a series of numbers, text, and so on. Here we output a series of words and calculate the power of three times at the same time.

Save this script as mylib.py and run it:

Module

As you can see, the script is working correctly.

For demonstration purposes, we create a new script into the main.py script file and import the test function from mylib.

2. Package as exe using PyInstaller

Package it as a single exe using the following command (remove-F is not a single exe)

The packaging process is as follows:

Finally, build, disk folders are generated in the directory.Where exe file is in disk.It is working properly.As follows:

3. Decompiled Pyinstaller packaged exe

Such exe s can already be migrated to other computers for use, and this is the end if it is for the convenience of other computers.Sometimes, however, we may use this feature for many people, but if you don't want to open source or be cracked by others, this is not enough.

Because at this point exe can be decompiled by others.

Tools used here:

exe decompile tool: pyinstxtractor.py: Click here to download

pyc decompile tool: Easy Python Decompiler Or decompile pyc online.

Place pyinstxtractor.py in the same directory as the exe file and execute the following cmd command:

If executed successfully, a new decompiled folder, main.exe_extracted, will be generated in the same directory, as shown below:

At the same time, we can find the mylib module, which was introduced by exe at that time, in pyc format, in the path circled below.

pyc format file, decompile is very simple.Using the tools provided in the previous section or finding an online minute decompile on the web, let's look at the results of the decompilation:

As you can see, the source code is basically decompiled perfectly.Where Chinese is involved, coding problems will change.But the non-Chinese part is almost the same.

Ask you if you are afraid!!!

4. Compile scripts as pyd to prevent decompiling

Fear, hath.

Pyinstaller Import Module

How to solve this problem, consider compiling the module py file into a dynamic link library, which makes cracking more difficult.In python, the pyd format is a dynamic link library.Use cython to compile, and if anaconda comes with it, some pythons may not have cython, just install it:

We create a new py file in the folder where main.py is located. Here I name it build_pyd.py, which reads as follows:

Then execute the following cmd command:

The process and results are illustrated below, with the red-box PYD file compiled.Since I am a 64-bit system and python, an amd64 suffix is generated, so we can rename this deletion to mylib.pyd.

Note: When both mylib.pyd and mylib.py exist, the import priority is pyd>py, so PYD is the default import without removing the PY file.

At this point, we delete the build, disk folders, repeat Step 2, and compile to exe again.

Note: Compilation requires a relevant VC environment, as Python 3.5 is based on the VS14 version, so I installed it here as well.It cannot be compiled without installation.

You can verify that:

After decompiling main.exe again, mylib.pyc could not be found under the original path E:tdistmain.exe_extractedout00-PYZ.pyz_extracted.

Because he is no longer a file that can be decompiled directly.

Pyinstaller Import Error

Where is he? He exists in the upper directory as a pyd.As follows:

pyd's decompilation is quite difficult, so you're done!

Reference link: https://www.lizenghai.com/archives/898.html

https://blog.csdn.net/zy841958835/article/details/79446871

https://blog.csdn.net/HW140701/article/details/93494869 (pyinstaller-F--key=***encrypted pyc prevents decompilation) pycropto needs to be installed during this process, which is cumbersome to install: https://www.cnblogs.com/xiohao/p/11216271.html)

Pyinstaller Python Module

Added by friday_13 on Fri, 02 Aug 2019 03:46:14 +0300