Author - StudySection Post Views - 31 views
data python build

Add and Access Data Files in Python Build

Introduction

We often need to include data files like txt file, HTML file, ini file in our packaged application. We can include all types of these data files in our packaged application, instead of providing them separately in Python.

Steps to include data files in Python Build

There are two ways to achieve this –

  1. Running pyinstaller command from the command line

    pyinstaller -F --add-data "sampleText.txt;." myscript.py
    Here sampleText.txt;. Is in the format of
    format: {source}{os_separator}{destination}
    source - location of the data file to include
    os_separetor - ; (to differentiate between source and destination)
    destination - location where source file is present (‘.’ - It means source file is present at root level)

  2. Through Spec files –
    When we run pyinstaller a spec file is generated.
    In this spec file, we can add data files location under datas
    For e.g. –
    datas=[("/var/www/html/myProject/config.ini", 'myFiles'), (/var/www/html/myProject/sampleText.txt', '.'),

    Here /var/www/html/myProject/config.ini will be present in myFiles
    And /var/www/html/myProject/sampleText.txt at root level.

Access Data files in Pyinstaller

  1. First, we need to check whether we are accessing data files from the packaged app or from python script –

    try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    basePath = sys._MEIPASS
    except Exception:
    basePath = os.path.abspath(".")

  2. Then we will concatenate our data file path.
    bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
    sampleTextFile = os.path.join(bundle_dir, 'sampleText.txt')

Being the most extensively used JavaScript library, a jQuery Certification will add enormous value to your skill-set. jQuery provides various functionalities to the developer in order to develop complex applications with ease and efficiency.

Leave a Reply

Your email address will not be published. Required fields are marked *