What's the best practice for running a python script?

I am developing some scripts that I plan to use in my LAB. Currently, I have installed Python and all the necessary modules only locally on the station I am working with (development station).

I would like to be able to run scripts that I develop through each of my LAB stations.

What's the best practice?

Do I need to install the same environment, except for the IDE of course, on all my stations? If so, what is the recommended way to do this?

By the way, is it recommended in most cases to run these scripts from the command prompt screen (Windows) or is there some other elegant way to do this?

+3


source to share


3 answers


You should package these scripts:

https://python-packaging.readthedocs.io/en/latest/

And use one of the built-in methods for defining scripts in the package:

http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html?highlight=scripts

This will help you maintain your scripts. You can get a version of the package that is really useful because you will have bugs (we all write bugs) and you fix them in a newer version. Also, installing / updating a package is easier than managing multiple independent scripts.

For some "best practices" when writing scripts, I recommend:



1) Write unit tests for your code: http://python-guide-pt-br.readthedocs.io/en/latest/writing/tests/

2) Don't put logic under your __name__ check. If anything, just wrap all this logic in a function called main and call that under your __name__ check.

Bad

  if __name__ == '__main__':
       foo = thing()
       args = get_args()
       try:
           blah()
       except DerpError:
           handle_derp()

      

Good

if __name__ == '__main__':
    main()

      

+2


source


If you want to run a single script on multiple computers without installing Python everywhere, you can "compile" the script to an .exe using py2exe, cx_Freeze, or PyInstall. "Compiling" actually packages Python and libraries into generated .exe files or accompanying files.



But if you plan on running a lot of Python scripts, you're better off installing Python everywhere and distributing your scripts and libraries as Python packages (eggs or wheels).

0


source


@moshe, if you have admin rights on all of these machines, I recommend installing the standard Anaconda python distribution. It comes with Jupyter Notebook , an invaluable tool for scripting, saving and sharing your code, and other handy things like visualizing your data.

Alternatively, you can simply start an Amazon AWS free tier Linux instance , configure Python the way you want it, and then just shell your lab machines to ssh in that case. This may be the preferred method if you are using a shared computer where other people can install software and potentially ruin your environment.

0


source







All Articles