Python applications are distributed as source files

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. Th...
This article was sent to us by: Douglas Clark at 01052010

1 Programming » Python applications are distributed as source files
Bookmark and Share

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.

Legal Disclaimer

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.

Related Articles

1. Associative arrays in Python are dictionaries
This article discusses dictionaries, Python's name for associative arrays, which it implements using hash tables. Dictionaries are amazingly us...

2. What can be used as a key in Python
Python permits more than just strings to be used in this manner. Any Python object that is immutable and hashable can be used as a key to a dic...

3. Definitions and uses of functions in Python
This article assumes you're familiar with function definitions in at least one other computer language and with the concepts that correspond to...

4. Python functions handle variable numbers of arguments
Python functions can also be defined to handle variable numbers of arguments. You can do this two different ways. One way handles the relativel...

5. Lambda expressions and generator functions in Python
Short functions like those you just saw can also be defined using lambda expressions of the form. Lambda expressions are anonymous little funct...

6. How to create a basic program in Python
Up until now, you've been using the Python interpreter mainly in interactive mode. For production use, you'll want to create Python programs or...

7. How to make Python script execution in UNIX and Mac OS X and in Windows
If you're on UNIX, you can easily make a script directly executable. Note that if Python 3.x isn't your default version of Python, you may need...

8. The difference between scripts on Windows scripts on UNIX
The way you call scripts on Windows differs from the way scripts are called on Linux/ UNIX, and that difference can affect what kind of scripts...

9. How to use the Python file system
Working with files involves one of two things: basic I/O and working with the filesystem (for example, naming, creating, moving, or referring t...

10. Client complaints about inconsistent query results
You've probably seen it before: a client has called the help desk and reported that a couple of queries have seemingly inconsistent results. Yo...