Creating run.py reading Zip Files
From Digi Developer
Contents |
Launching a Serious Project - a RUN.PY and pulling code from ZIP files
If you create a serious project, you'll discover that manually uploading a dozen .PY or .PYC files to your Digi product becomes tedious and error prone. Fortunately, you can pack your project files into a single ZIP file for uploading.
This example project has eleven python files, nine of which are bundled into a single ZIP file. Then the System Path must be updated to enable the Python interpreter to locate the ZIP'd files.
A Real Example File List:
- run.py (see below) is the small script which connects all the pieces together and runs the main routine. There is nothing magic about the name - it could just as easily be zyx.py or destroy_world.py. However, naming it **run.py** will remind you in a few years which script 'runs' your project.
- ab_logix.zip contains the project's nine sub-scripts which parse DF1 packets, manage the Ethernet/IP socket to the PLC and so on. It can have any name, but the name will be hard-coded into the run.py. Any tool creating common ZIP files can be used - a free one is: 7-Zip Open Source Tool.
- dcwan_config.py is an uncompressed file used to configure the project. It could be included within the ZIP, but leaving it outside makes configuration changes easier. For this project it defines the local PLC IP address and selecting the TCP and UDP ports to listen on for cellular polls. The main scripts IMPORT this file, using constants defined to modify their behavior.
- python_ext.zip is supplied with your Digi product and contains other common python modules. You only need to include this if your code imports any of the modules - for this project, the COPY module is required.
Running the Project:
python run.py
The RUN.PY script:
## run.py """\ To run this, use command line: python run.py (options) Where options include: (none yet) """ import os import sys if sys.platform.startswith('digi'): # on Digi, python files are in WEB/python sys.path.append(os.path.join('WEB', 'python', 'python_ext.zip')) sys.path.append(os.path.join('WEB', 'python', 'ab_logix.zip')) else: # on PC is just current directory sys.path.append('python_ext.zip') sys.path.append('ab_logix.zip') if __name__ == '__main__': import aboxy # this is our main project file, which is within ab_logix.zip aboxy.run_aboxy( ) sys.exit(0)
