Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyInstaller 2.1 import custom package

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.

like image 924
Nir Avatar asked Feb 04 '14 15:02

Nir


People also ask

Does PyInstaller include imports?

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.

How do I use PyInstaller hidden import?

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.

What is Pathex in PyInstaller?

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.

How do I create a spec file in PyInstaller?

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.


1 Answers

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

like image 144
Nir Avatar answered Nov 08 '22 08:11

Nir