You can distribute your scripts as source files (as .py files). You can also ship them as byte code (as .pyc or .pyo files). A byte code file will run under any platform as long as it has been written portably. The standard way of packaging and distributing Python modules and applications is found in the distutils package. The details of creating a distribution are beyond the scope of this article, but if you want to package and distribute your Python code, the documentation on the python.org website has an extensive guide and a beginner's tutorial on the use of distutils. The heart of a distutils package is a setup.py file that you create, basically a Python program that controls the installation. distutils has a number of options and lets you create built distributions tailored for Windows and several UNIX platforms. Although it's not the purpose of this article to dwell on platform-specific tools, it's worth mentioning that py2exe creates Windows standalone programs and py2app does the same on the Mac OS X platform. By standalone, I mean they're single executables that can run on machines that don't have Python installed.
In many ways, standalone executables aren't ideal, because they tend to be larger and less flexible than native Python applications; but in some situations they're the best, and sometimes the only, solution. It's also possible to create an executable Python program that will run on machines that don't have Python installed by using the freeze tool. You'll find the instructions for this in the Readme file in the freeze directory in the Tools subdirectory of the Python source directory. If you're planning to use freeze, you'll probably need to download the Python source distribution. In the process of "freezing" a Python program, you create C files, which are then compiled and linked using a C compiler, which you need to have installed on your system. It will run only on the platform for which the C compiler you use provides its executables. Note that it will still be possible for someone to reverse-engineer your Python code from the resulting output.
This also doesn't always work in a straightforward manner. This is one situation where you may end up needing to obtain support from the Python newsgroup, depending on the specifics of your application and which C compiler you're using. When you have your executable, it's generally robust. Python scripts in their most basic form are sequences of Python statements placed in a file. A slightly more structured way to organize scripts that contain more than a few lines of Python code is presented here. This is a simple matter of using a control function to buffer out some of the control and interface logic. Modules can be instrumented to run as scripts, and scripts can be set up so they can be imported as modules.
This provides a modular way to create large scripts and a simple mechanism to instrument a module with a regression test mode. Scripts can be made executable on UNIX. They can be set up to support commandline redirection of their input and output, and with the optparse module it's easy to parse out complex combinations of command-line options and arguments. On Mac OS X, you can run scripts from a terminal window in exactly the same way as on other UNIX platforms. In addition, you can use the Python Launcher to run Python programs, either individually or as the default application for opening Python files. On Windows, you can call scripts a number of ways: by opening them with a double- click, using the Run window, or using a command-prompt window. It's possible to use command-line options and arguments in the last two of these and redirection in the last, but this isn't as convenient as on Linux/UNIX. Therefore, on Windows, command- line arguments and redirection are normally used only for special situations like test modes. This is also generally true for GUI programs on all three platforms. Finally, as an alternative to scripts, py2exe, py2app, and the freeze tool provide the capability to provide an executable Python program that will run on machines that don't contain a Python interpreter.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: Douglas Clark at 01052010
1. Lambda expressions and generator functions in Python
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.