I have a script I'm trying to compile with PyInstaller (2.1) using Python 2.7
The script uses a custom package I've written called 'auto_common'
In the script I import it by using
sys.path.append(path_to_package)
The project folders look like this:
Automation/ Top level project
Proj1/
script1.py This is the script I want to compile
myspec.spec Spec file for the script
Packages/
auto_common/
__init__.py Init module of the package (empty)
... More modules here
In the PyInstaller log file I get the following warning:
W: no module named auto_common (top-level import by __main__)
How do I create a hook which will include the package (using sys.path.append for example)?
I tried adding the path of the package to 'pathex' in the spec file but it didn't work.
Analysis: Finding the Files Your Program Needs To find out, PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.
The simpler solution is to use --hidden-import=modulename along with the PyInstaller script. It will add modulename as import statement silently. Hooks are better if you want to specify which import needs what additional modules. --hidden-import is simpler as a one-shot or for debugging.
pathex : a list of paths to search for imports (like using PYTHONPATH ), including paths given by the --paths option. binaries : non-python modules needed by the scripts, including names given by the --add-binary option; datas : non-binary files included in the app, including names given by the --add-data option.
By default, pyi-makespec generates a spec file that tells PyInstaller to create a distribution directory contains the main executable and the dynamic libraries. The option --onefile specifies that you want PyInstaller to build a single file with everything inside.
Using "-p" when compiling (or when building a spec file) will add additional paths to python's path.
pyinstaller -p any_path/Automation/Packages script1.py
This mimics the behavior of sys.path.append().
Thanks to the guys at PyInstaller for the solution:
sys.path.append does not work when compiling with PyInstaller 2.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With