问题
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?
回答1:
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')
来源:https://stackoverflow.com/questions/59743234/how-can-i-add-a-python-site-package-folder-thats-not-being-included-to-a-pyin