Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import error while bundling using py2exe

Tags:

python

I bundled a small script written in python using py2exe. The script uses many packages and one of them is reportlab. After bundling using py2exe I tried to run the executable file and it is returning following error:

C:\Python26\dist>DELchek.exe
Traceback (most recent call last):
File "DELchek.py", line 12, in <module>
File "reportlab\pdfgen\canvas.pyc", line 25, in <
File "reportlab\pdfbase\pdfdoc.pyc", line 22, in
File "reportlab\pdfbase\pdfmetrics.pyc", line 23,
File "reportlab\pdfbase\_fontdata.pyc", line 158,
ImportError: No module named _fontdata_enc_winansi

But I could see the '_fontdata_enc_winansi' module in reportlab folder. Could someone help me to fix this.

like image 523
Sankar Avatar asked Dec 07 '22 20:12

Sankar


1 Answers

I've had the same problem in the past bundling reportlab with py2exe. It imports of a bunch of modules dynamically, which py2exe does not recognize when assembling the dependencies. The brute-force fix is to import the required modules directly in your code:

from reportlab.pdfbase import _fontdata_enc_winansi
from reportlab.pdfbase import _fontdata_enc_macroman
from reportlab.pdfbase import _fontdata_enc_standard
from reportlab.pdfbase import _fontdata_enc_symbol
from reportlab.pdfbase import _fontdata_enc_zapfdingbats
from reportlab.pdfbase import _fontdata_enc_pdfdoc
from reportlab.pdfbase import _fontdata_enc_macexpert
from reportlab.pdfbase import _fontdata_widths_courier
from reportlab.pdfbase import _fontdata_widths_courierbold
from reportlab.pdfbase import _fontdata_widths_courieroblique
from reportlab.pdfbase import _fontdata_widths_courierboldoblique
from reportlab.pdfbase import _fontdata_widths_helvetica
from reportlab.pdfbase import _fontdata_widths_helveticabold
from reportlab.pdfbase import _fontdata_widths_helveticaoblique
from reportlab.pdfbase import _fontdata_widths_helveticaboldoblique
from reportlab.pdfbase import _fontdata_widths_timesroman
from reportlab.pdfbase import _fontdata_widths_timesbold
from reportlab.pdfbase import _fontdata_widths_timesitalic
from reportlab.pdfbase import _fontdata_widths_timesbolditalic
from reportlab.pdfbase import _fontdata_widths_symbol
from reportlab.pdfbase import _fontdata_widths_zapfdingbats

A search also shows that Sankar found another solution, which is including the following packages in the setup script:

packages=[
    'reportlab',
    'reportlab.graphics.charts',
    'reportlab.graphics.samples',
    'reportlab.graphics.widgets',
    'reportlab.graphics.barcode',
    'reportlab.graphics',
    'reportlab.lib',
    'reportlab.pdfbase',
    'reportlab.pdfgen',
    'reportlab.platypus',
], 
like image 57
robots.jpg Avatar answered Jan 04 '23 13:01

robots.jpg