Printing with Python and Epson POS printer.

Kakar Nyori
4 min readNov 16, 2018

This post is for my future self. I am trying to print using a python package on windows, called python-escpos. This library works fine in both mac and Unix by just installing the library and some of its additional dependencies such as lsusb. However, on windows, you need extra steps to work. So I will be posting any new developments, trials, updates in this post and hopefully I will be able to work out this library on windows OS. This post is/will be in draft mode, and will be published only when I will be able to print with python and the Epson printer on windows.

Let's begin!

Prerequisites:

  1. Windows OS. I am using windows 7 (64 bit).
  2. Python and pip. I am using python 2.7.15 (64 bit)
  3. Virtualenvwrapper (optional)
  4. Install the printer drivers (restart if need be).

Start by installing python-escpos using pip:

pip install python-escpos --pre

At the time of writing this story, I am using a pre-release version of python-escpos (v3.0a4). So, please be sure to refer to the correct version of the documentation, as there are major changes in different versions.

Now after you have installed the library, you can check whether it is working by running the following command as documented, also as one of the maintainers answered on StackOverflow:

from escpos.printer import Usb
Epson = Usb(0x04b8,0x0202)

But you will get an error:

usb.core.NoBackendError: No backend available

I went through many searches regarding this issue.

  1. https://github.com/python-escpos/python-escpos/issues/299
  2. https://github.com/python-escpos/python-escpos/pull/274
  3. https://github.com/libusb/libusb/issues/85

After some digging around, I found this wiki. According to this, I had to use the most recent version of Zadig, an Automated Driver Installer GUI application for WinUSB, libusb-win32 and libusbK. Also, this answer on StackOverflow basically says the same thing.

So, I downloaded Zadig, opened the application, and searched for my printer from the drop-down menu. However, it was not listed on the list. Later I found out, inside the Options menu you can enable “List All Devices”. After I have checked in the option to list all devices, from the drop-down menu, I selected TM-82, and chose WinUSB to replace/install the driver successfully.

Note: If you get an error and you try to reinstall the driver, you will get another error. You can try to delete the usb_driver directory from the user directory and try to install again. Also you can confirm by going to your Device manager.

After that again I ran the same python-escpos commands as documented, and got the same error:

usb.core.NoBackendError: No backend available

Again I had to continue my search for the light, and I went through many more searches

  1. https://github.com/python-escpos/python-escpos/issues/306
  2. Finally, I landed on this issue, https://github.com/pyusb/pyusb/issues/120
  3. Which in turn led me to many StackOverflow issue. Tried many of answers, and lastly, this post helped me https://stackoverflow.com/questions/33972145/pyusb-on-windows-8-1-no-backend-available-how-to-install-libusb/34720024#34720024

So according to the above answer on the StackOverflow, this is what I did:

  1. I downloaded the libusb library from http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download
  2. Extracted the files
  3. Copied libusb-1.0.dll into my windows system directory.
  • On 64-bit Windows:
    MS64\dll\libusb-1.0.dll must be copied into C:\Windows\System32
  • On 32-bit windows:
    MS32\dll\libusb-1.0.dll must be copied into C:\Windows\SysWOW64.

4. Copied libusb-1.0.lib from binaries to lib dir into the python directory
i.e., Copy MS64\static\libusb-1.0.lib to C:\Users\user\Envs\py3env\Lib or Copy MS32\static\libusb-1.0.lib to C:\Users\user\Envs\py3env\Lib

Now when I run the above python-escpos command, I get a different error:

from escpos import *
Epson = printer.Usb(0x04b8,0x0202)

raise USBNotFoundError(“Device not found or cable not plugged in.”)

Since I am not getting NoBackendError error, I hope it will work when I connect an Epson POS printer. I will get back after I have tested with the printer.

Alright, I am back, and after I have connected the printer, I am getting a different error:

Could not set configuration: {0}

So, I searched for the light again, and as it turns out I needed to restart my computer after the driver installation.

Now when I run the command, I am getting back the old error:

usb.core.NoBackendError: No backend available

So, I searched for the light again and searched through many more posts over the ocean of internet! I also went through a pyusb tutorial to check whether the issue is with pyusb (a library dependency of python-escpos). But while going through the pyusb tutorial, pyusb found the printer, as the following command didn’t throw any errors at me.

import usb.core

# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)

# was it found?
if dev is None:
raise ValueError('Device not found')

So, the problem is not with the pyusb or libusb. The problem was with the python-escpos library.

Finally, I landed on this StackOverflow answer:

The problem is plain and simple that the check whether a kernel driver is active which is not possible on Windows. It seems this library is simply not written for use on Windows.

You can use it on Windows if you delete the last 5 lines from the source code starting with if self.device.is_kernel_driver_active(0): or check if the code runs on Windows beforehand and don't call them.

So, what I did was, I commented out the following lines from the printer.py module of python-escpos library from the site-packages inside my virtual environment

try:
check_driver = self.device.is_kernel_driver_active(0)
except NotImplementedError:
pass
if check_driver is None or check_driver:
try:
self.device.detach_kernel_driver(0)
except usb.core.USBError as e:
if check_driver is not None:
print("Could not detatch kernel driver: {0}".format(str(e)))

And now, when I run the above command as documented, I get my printer and can run the python-escpos commands to print!!!

Note: This has been tested on both Windows 7 and windows 10

--

--