Auto Py to Exe is a convenient tool that allows you to convert your Python scripts into standalone executables. However, there may be situations where your script relies on external modules or packages that are not automatically detected by Auto Py to Exe. In such cases, you need to use “hidden imports” to explicitly include these dependencies in your executable. This guide will explain what hidden imports are, when to use them, and how to incorporate them into your Auto-Py-to-Exe workflow.
What are Hidden Imports?
Hidden imports play a crucial role in the functionality of Auto-Py-to-Exe. To grasp their significance, it’s important to understand how Python imports work and why certain dependencies might be considered “hidden.”
In Python, imports are used to bring in external modules or packages into your script, allowing you to utilize their functionality. When writing Python code, you typically import modules explicitly at the beginning of your script using statements like:
These explicit imports are easily recognized by Auto-Py-to-Exe, and the tool can include them automatically in the final executable.
However, there are cases where dependencies are not imported explicitly but are still required for the proper execution of your script. These dependencies may be indirectly imported through other modules or imported dynamically at runtime based on certain conditions. Since Auto-Py-to-Exe analyzes the code statically, it may not detect these indirect or dynamic imports by default. As a result, these dependencies become “hidden” from Auto-Py-to-Exe, and it does not include them automatically in the executable.
Hidden imports provide a solution to this problem. By manually specifying these hidden dependencies, you ensure that Auto-Py-to-Exe includes them in the final executable, allowing your script to function as intended on other machines.
Hidden imports help bridge the gap between the dependencies required by your script and the ability of Auto-Py-to-Exe to automatically detect them. By explicitly stating these hidden imports, you enable Auto-Py-to-Exe to identify and bundle the necessary modules and packages, ensuring that your executable runs smoothly on other systems.
It’s important to note that not all dependencies require hidden imports. Auto-Py-to-Exe employs various techniques to detect and include most dependencies automatically. Hidden imports are only necessary when certain dependencies are not recognized by Auto-Py-to-Exe during the static analysis of your code.
When to Use Hidden Imports in Auto-Py-to-Exe:
To summarize, you should consider using hidden imports in the following scenarios:
- Dynamic imports: If your script uses dynamic imports, where modules are imported based on user input or runtime conditions, these dependencies might not be detected automatically.
- Indirect imports: Sometimes, your script may rely on packages that are imported indirectly, through other modules. In such cases, Auto Py to Exe may not be able to identify them as direct dependencies.
- Custom modules: If you have custom modules or third-party libraries that are not recognized by Auto Py to Exe, hidden imports can be used to explicitly include the
How to use Hidden Imports with Auto-Py-to-Exe
To use hidden imports with Auto Py to Exe, follow these steps:
Step 1: Launch Auto Py to Exe:
Open Auto Py to Exe, either through the command line or the graphical user interface (GUI), depending on your preference. Use the following command for opening it through the command line:
Step 2: Import Your Script (Optional)
Use the “Add Folders” or “Add Files” option in Auto Py to Exe to import any additional Python files that you are.
This includes any type of files, including media, audio, scripts, custom modules, etc.
Step 3: Specify Hidden Imports:
In the Auto Py to Exe interface, look for the “Advanced” section. Here, you can enter the names of the modules or packages you want to include as hidden imports. Each hidden import should be on a separate line. You can specify modules using their import name or package name.
Usually you will not know which libraries to include here until you have run and executed the executable atleast once. Take not of any “module not found” errors that occurred, and repeat this process with the modules in question included as hidden imports.
If your executable does not work, even after including them as hidden imports, the issue lies elsewhere. Refer to our troubleshooting guide for pyinstaller/auto-py-to-exe for more information.
Step 4: Build Your Executable:
Once you have specified the hidden imports, proceed with the usual configuration options in Auto Py to Exe, such as setting the output directory, selecting an icon, and choosing other settings as required. Once you have configured everything, click on the “Convert .py to .exe” or similar button to initiate the build process.
Step 5: Test and Distribute:
After the conversion process completes successfully, you will have a standalone executable that includes the hidden imports. Test the executable thoroughly to ensure that all dependencies are functioning correctly. You can then distribute the executable to users without worrying about missing dependencies.
This marks the end of the “Guide to Hidden Imports in Auto-Py-to-EXE” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial may be asked in the comments section below.