Python on Linux

 < Day Day Up > 

Fedora comes with Python installed by default, as do many other versions of Linux and Unix even Mac OS X comes with Python preinstalled. Part of this is due to convenience because it is such a popular scripting language and it saves having to install it later if the user wants to run a script. However, in Fedora's case, part of the reason is that several of the core system programs are written in Python, including Yum itself.

If, for some reason, you manage to have a Fedora install without Python, you can install it using Yum using the command yum install python as root.

The Python binary is installed into /usr/bin/python; if you run that, you will enter the Python interactive interpreter where you can type commands and have them executed immediately. Although PHP also has an interactive mode (use php a to activate it), it is neither as powerful or as flexible as Python's.

As with Perl, PHP, and other scripting languages, you can also execute Python scripts by adding a shebang line to the start of your scripts that point to /usr/bin/python and then setting the file to be executable.

The third and final way to run Python scripts is through mod_python, which is installed by default when you select the Web Server application group from the Add/Remove Packages dialog.

For the purposes of this introduction, we will be using the interactive Python interpreter because it provides immediate feedback on commands as you type them. Furthermore, knowledge of the interactive interpreter is essential for debugging when you start writing more advanced scripts.

Getting Interactive

We will be using the interactive interpreter for this chapter, so it is essential that you are comfortable using it. To get started, open a terminal and run the command python. You should see this:

 [paul@caitlin ~]$ python Python 2.3.4 (#1, Oct 26 2004, 16:42:40) [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 

The >>> is where you type your input, and you can set and get a variable like this:

 >>> python = 'great' >>> python 'great' >>> 

On line one, the variable python is set to the text great, and on line two that value is read back from the variable simply by typing the name of the variable you want to read. Line three shows Python printing the variable; on line four you are back at the prompt to type more commands.

Python remembers all the variables you use while in the interactive interpreter, which means you can set a variable to be the value of another variable.

When you are done, press Ctrl+D to exit. At this point, all your variables and commands are forgotten by the interpreter, which is why complex Python programs are always saved in scripts!

     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net