5

I am having trouble including a python package while using PyInstaller, particularly docxcompose . This is a package that needs to import its site-package folder within the PyInstaller directory. I have pip installed docxcompose and it is in my site-packages library, with the folder labeled as docxcompose. import docxcompose is explicitly listed in the python file I am referencing in PyInstaller.

I am debugging using a spec file and the --onedir method, as I want to eventually install using --onefile. I have added these to the analysis section of spec file so far, with no luck:

hiddenimports=['docxcompose']
pathex=['C:\\Users\\myusername\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\site-packages']

Is there a reason docxcompose is not being added in my PyInstall? Is there I way I can force that folder to be copied in during install otherwise?

JcoOnline
  • 243
  • 2
  • 13
  • Why is it that you think it's not being included in the bundle? If it's imported explicitly, PyInstaller will include it automatically. (if not, you can use the hidden-import option, which you have done anyway). Can you update your question with a specific error message? (Hint: use the debug option when building the exe) – sytech Jan 14 '20 at 23:44
  • 1
    @sytech My executable opens, but when I run a certain process that uses docxcompose, the exe crashes, and it says it cannot find the folder docxcompose, which holds some xml files. I cannot find docxcompose anywhere in my directory after my installs. – JcoOnline Jan 15 '20 at 13:41

2 Answers2

6

To "manually" add the docxcompose folder instead of relying on the PyInstaller search, I found that you have to add the site-package folder destination for docxcompose in "datas" within the analysis section of the spec file. See text/spec file:

sample.spec

# -*- mode: python ; coding: utf-8 -*-

import sys
from os import path
site_packages = next(p for p in sys.path if 'site-packages' in p)
block_cipher = None


a = Analysis(['ape_proposal_generator.py'],
             pathex=['C:\\Users\\myusername\\AppData\\Local\\Programs\\Python\\Python37\\Lib', 'C:\\MyPythonFileDest'],
             binaries=[],
             datas=[(path.join(site_packages,"docxcompose"),
"docxcompose")],
             hiddenimports=['docxcompose'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='mypythonfilename',
          debug=True,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='mypythonfilename')
JcoOnline
  • 243
  • 2
  • 13
5

Alternatively, you can add --collect-data "docxcompose" to your command line argument for pyinstaller.

pyinstaller --collect-data "docxcompose"
machaniv
  • 91
  • 1
  • 3