OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Error when Generating Barcodes Using barcode Library in PyInstaller Executable

  • Thread starter Thread starter amarinelloa
  • Start date Start date
A

amarinelloa

Guest
I have a Python application that uses the barcode library to generate barcodes and save them as PNG images. The application works perfectly when run from the Python interpreter, but I encounter an error when I create an executable using PyInstaller and try to run it.

The error I'm encountering is as follows:

Code:
File "tkinter/__init__.py", line 1948, in __call__
File "Bachmaier.py", line 668, in save_selection
File "Barcode_generator.py", line 13, in generate_barcodes
  barcode_image = code128.render(writer_options={"font_size": 1, "text_distance": 1})
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "barcode/codex.py", line 255, in render
File "barcode/base.py", line 100, in render
File "barcode/writer.py", line 269, in render
File "barcode/writer.py", line 440, in _paint_text
File "PIL/ImageFont.py", line 791, in truetype
File "PIL/ImageFont.py", line 788, in freetype
File "PIL/ImageFont.py", line 226, in __init__
OSError: cannot open resource

Here's the relevant code snippet where the error occurs:

Code:
import os
from barcode import Code128
from barcode.writer import ImageWriter

def generate_barcodes(stl_names, path):
    barcodes = []  # To store the barcode image paths
    
    for name in stl_names:
        barcode_name = f"{name}"
        code128 = Code128(barcode_name, writer=ImageWriter())

        # Generate the barcode as a PIL image
        barcode_image = code128.render(writer_options={"font_size": 1, "text_distance": 1})

        # Save the barcode image as a PNG file
        barcode_file_path = os.path.join(path, f"{barcode_name}.png")
        barcode_image.save(barcode_file_path, "PNG")

        barcodes.append(barcode_file_path)
    return barcodes

The error seems to be related to accessing a resource, but I'm not explicitly using any fonts in my code. The issue appears only when I run the executable generated by PyInstaller; running the script directly in the Python interpreter works fine.

I've tried including the --add-data flag with PyInstaller to ensure resources are packaged, but the issue persists. What could be causing this error and how can I resolve it? Is there anything specific I need to consider when working with the barcode library in a PyInstaller executable?

Any insights or guidance on troubleshooting this issue would be greatly appreciated. Thank you in advance for your help!
<p>I have a Python application that uses the <code>barcode</code> library to generate barcodes and save them as PNG images. The application works perfectly when run from the Python interpreter, but I encounter an error when I create an executable using PyInstaller and try to run it.</p>
<p>The error I'm encountering is as follows:</p>
<pre><code>File "tkinter/__init__.py", line 1948, in __call__
File "Bachmaier.py", line 668, in save_selection
File "Barcode_generator.py", line 13, in generate_barcodes
barcode_image = code128.render(writer_options={"font_size": 1, "text_distance": 1})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "barcode/codex.py", line 255, in render
File "barcode/base.py", line 100, in render
File "barcode/writer.py", line 269, in render
File "barcode/writer.py", line 440, in _paint_text
File "PIL/ImageFont.py", line 791, in truetype
File "PIL/ImageFont.py", line 788, in freetype
File "PIL/ImageFont.py", line 226, in __init__
OSError: cannot open resource

</code></pre>
<p>Here's the relevant code snippet where the error occurs:</p>
<pre class="lang-py prettyprint-override"><code>import os
from barcode import Code128
from barcode.writer import ImageWriter

def generate_barcodes(stl_names, path):
barcodes = [] # To store the barcode image paths

for name in stl_names:
barcode_name = f"{name}"
code128 = Code128(barcode_name, writer=ImageWriter())

# Generate the barcode as a PIL image
barcode_image = code128.render(writer_options={"font_size": 1, "text_distance": 1})

# Save the barcode image as a PNG file
barcode_file_path = os.path.join(path, f"{barcode_name}.png")
barcode_image.save(barcode_file_path, "PNG")

barcodes.append(barcode_file_path)
return barcodes
</code></pre>
<p>The error seems to be related to accessing a resource, but I'm not explicitly using any fonts in my code. The issue appears only when I run the executable generated by PyInstaller; running the script directly in the Python interpreter works fine.</p>
<p>I've tried including the <code>--add-data</code> flag with PyInstaller to ensure resources are packaged, but the issue persists. What could be causing this error and how can I resolve it? Is there anything specific I need to consider when working with the <code>barcode</code> library in a PyInstaller executable?</p>
<p>Any insights or guidance on troubleshooting this issue would be greatly appreciated. Thank you in advance for your help!</p>
 
Top