Modules are python files (*.py) that can be imported in a script. Packages are collections of modules within a single namespace. If you like, read up on the exact definitions here or here.
It can be handy to build your own package. For example: you have build a set of scripts for a specific project. Let's say Project X. For Project X you have written some scripts for
- input of specific data (inputX.py)
- output to a format the client specifically requested (outputX.py)
- tailored plots (plotX.py)
To keep track of these functions for this project, you want to build a single package containing these functions. This can easily be achieved without leaving DeltaShell.
Writing a module
- Open DeltaShell
- In the Toolbox, create a new script in the upper 'Scripts' directory and name it 'outputX'
Copy-paste the following dummy function and save the file:
def dummyX(): print "Hello World!"
- Create a new script called 'runfile'
In runfile, we will import outputX and run the dummyX function:
import outputX outputX.dummyx()
In the Messages window, you will see the 'Hello World' output appear. We have imported 'outputX' as a module.
Writing a package
- To make a package, insert a new folder in the scripts folder and name it 'ProjectX:
- To move 'outputX' to the new 'ProjectX' package, right click on 'outputX' and select 'Open location with windows explorer'.
- In windows explorer, cut-paste outputX.py in the ProjectX directory. The directory structure should now look like this:
- To use the dummyX function, change the code in runfile.py to:
from ProjectX import outputX outputX.dummyx()
Congratulations! You have written your own Python package with DeltaShell.