3

I finished building a Python script containing eight functions which download files, extracts them, manipulate and analyze their data and then produce graphs and export them into PNG files.

The input files are located in a remote FTP site, but some intermediate files and the output PNG files as well are saved locally in the computer file system. There are 100 lines of code in total.

Currently, I first execute it so that the Python console gets the function definitions, then I call the eight functions one by one to get the results.

For instance, I call the downloading function to download the archive files, then I call the function that extracts those files and so on. I am sure this isn't the best way to use a Python script, so I thought of asking here if anyone could provide the steps to get the most out of my script in terms of usage. I suspect a good way would be to turn the script into one or more modules, but I am not sure at all about that.

If possible, please provide details for two possible scenarios:

  1. The script is only used by me and I do have Python installed and know how to use it
  2. The script is used by other users who don't have Python installed
  • "The script is used by other users" - what kind of "other users" - people which know how to use a command line, users which expect a GUI? – Doc Brown Jul 24 '15 at 21:09
  • @DocBrown, people who know how to use a command line. – multigoodverse Jul 24 '15 at 21:29
3

Have the 8 function calls wrapped in

if __name__ == '__main__':

Then you can call the script by running

python NAME_OF_YOUR_SCRIPT.py

If you need to pass any variables in when running it, use the argparse module

  • As far as I am aware, I can still execute the script where the functions are being called with python script.py even without using if __name__ == '__main__':. Correct? If that is so, then why using if __name__ == '__main__':? – multigoodverse Jul 27 '15 at 7:12
  • @UneUne You don't NEED to wrap it in that if statement, but wrapping it is good practice. It allows you to import the file if you ever want to re-use some functions from it in another script. – tom Jul 27 '15 at 19:57
  • clear! And would I need to put the script in the Python modules path. – multigoodverse Jul 28 '15 at 7:24
  • Only if you wanted to be able to run/import the script from any directory. If you want to run the script by cd'ing to its directory, then running it, it shouldn't be necessary. – tom Jul 28 '15 at 14:37
2

I would suggest implementing command line parameters and command line help using argparse. The different functions could then be selected by command line options. If others without python installed need to use your program, then I would package it as an executable using pyinstaller.

Pyinstaller can be used to build 3 possilbe executables: onefile, onedir or console. The console type doesn't need a gui and is a stand alone exe. The onefile is a stand alone exe and onedir is a typical installation with required component files like images and dlls and such in a folder.

  • Do I need to implement a GUI into my script for the pyinstaller to work? – multigoodverse Jul 24 '15 at 18:30
  • No, you can use pyinstaller to build 3 possilbe executables. onefile, onedir and console. The console type doesn't need a gui and is a stand alone exe. The onefile is a stand alone exe and the onedir is a typical installation with required component files like images and dlls and such in a folder. – panofish Jul 24 '15 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.