DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Debugging Tips and Tricks for Python Structural Pattern Matching
  • Docker and Kubernetes Transforming Modern Deployment
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • OpenTelemetry Python: All You Need to Know About Tracing

Trending

  • Detection and Mitigation of Lateral Movement in Cloud Networks
  • Creating a Web Project: Caching for Performance Optimization
  • Secrets Sprawl and AI: Why Your Non-Human Identities Need Attention Before You Deploy That LLM
  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Running and Debugging a Python Barcode App in a Docker Container

Running and Debugging a Python Barcode App in a Docker Container

Efficiently test the compatibility of your Python barcode extensions built with CPython and the Dynamsoft Barcode Reader.

By 
Eric Parker user avatar
Eric Parker
·
Aug. 23, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.3K Views

Join the DZone community and get the full member experience.

Join For Free

When creating Python barcode extensions using CPython and Dynamsoft Barcode reader, one of the major concerns you have to face is testing your code compatibility with different versions of Python.

To test if your Python barcode extension works in your Windows, Linux, or macOS, you will need to have each version of Python installed on your computer. However, it’s time-consuming.

Here, we have an easy hack for you! Instead of installing all those Python versions, you can use a Docker container.  

Creating Docker Image With Python Barcode SDK 

First of all, download the Docker desktop and install it.  

If your system does not have Python 3.8, then go to the Docker hub and pull out a Docker image that contains Python 3.8. Now invoke the command from the command prompt:

docker run -it --rm python:3.8 bash 

If the docker image does not exist, the command given above will by itself trigger the download. 

download triggered

Next, you can create a Dockerfile to create a fresh Docker image that will include the Dynamsoft Barcode Reader SDK: 

FROM python:3.8  

RUN pip install dbr 

To generate a Docker image: 

docker build --rm -t yushulx/dynamsoft-barcode-reader:python-3.8

Further, run a container test. It will help you validate if your Python-based Dynamsoft Barcode Reader is working fine or not.  

docker run -it --rm yushulx/dynamsoft-barcode-reader:python-3.8 

outcome

Next, you need to publish the Docker Image to your Docker Hub after your container is validated:
docker login  

docker push yushulx/dynamsoft-barcode-reader:python-3.8 

 You can visit this link to test the image. 

Mounting Local Folder to Docker Container

To mount a local Windows folder to the Linux Docker Container, execute the Python script using Python 3.8. 

After that, select the shared drive from the settings of the Docker desktop: 

select the shared drive from the settings of the Docker desktopTo mount the Windows folder to the Linux container, execute the following command: 

docker run -it --rm -v d:/code/docker:/dynamsoft yushulx/dynamsoft-barcode-reader:python-3.8  bash 

Command executed

How to Debug Python Code With Visual Studio Code Remote-Containers

The preferred option to write and debug the Python code is to use Visual Studio Code.

To start with it, first, install the Docker extension for VSCode as shown below: 

install Docker extension for VSCodeClick the F1 key to execute the “Attach to Running Container” option.

Click the F1 key to execute the “Attach to Running Container” option.

 Double-click on the project folder. 

Double-click on the project folder

test

It will allow you to edit the Python file in VSCode. Next, install the Python debugger for the container to debug the code:  

install the Python debugger for the container to debug the code

 For debugging of the code remotely, click F5.

 For debugging of the code remotely, click F5

continued

 The complete Python barcode detection code is given below: 

Python
 
import os 
 

from dbr import DynamsoftBarcodeReader 

dbr = DynamsoftBarcodeReader() 

 
 

def InitLicense(license): 

    dbr.InitLicense(license) 

 
 

def DecodeFile(fileName): 

    try: 

        results = dbr.DecodeFile(fileName) 

        textResults = results["TextResults"] 

        resultsLength = len(textResults) 

        print("count: " + str(resultsLength)) 

        if resultsLength != 0: 

            for textResult in textResults: 

                print(textResult["BarcodeFormatString"]) 

                print(textResult["BarcodeText"]) 

                localizationResult = textResult["LocalizationResult"] 

                x1 = localizationResult["X1"] 

                y1 = localizationResult["Y1"] 

                x2 = localizationResult["X2"] 

                y2 = localizationResult["Y2"] 

                x3 = localizationResult["X3"] 

                y3 = localizationResult["Y3"] 

                x4 = localizationResult["X4"] 

                y4 = localizationResult["Y4"] 

                localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)] 

                print(localizationPoints) 

        else : 

            print("No barcode detected") 

    except Exception as err: 

        print(err) 

 
 

if __name__ == "__main__": 

    # Get the license from https://www.dynamsoft.com/customer/license/trialLicense/ 

    licenseKey = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="  

    fileName = r"test.jpg" 

    InitLicense(licenseKey) 

    dir_path = os.path.dirname(os.path.realpath(__file__)) 

    DecodeFile(os.path.join(dir_path, fileName)) 


Barcode Debug (command) Docker (software) Python (language)

Published at DZone with permission of Eric Parker. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Debugging Tips and Tricks for Python Structural Pattern Matching
  • Docker and Kubernetes Transforming Modern Deployment
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • OpenTelemetry Python: All You Need to Know About Tracing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!