Difference between revisions of "Python"

From CSE330 Wiki
Jump to navigationJump to search
Line 29: Line 29:
 
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.
 
Since the online documentation for Python is quite extensive and helpful, [http://docs.python.org/tutorial/index.html the online Python tutorial] is definitely the place for any and all of your Python-related needs. Be sure to read the [http://docs.python.org/tutorial/datastructures.html data structures] page to understand how they work in Python.
 
=Installing Python Modules=
 
=Installing Python Modules=
If there isn't a yum package or rpm available for the desired module, then most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:
+
Most likely the module will be distributed according to the python standard described [http://docs.python.org/install/#the-new-standard-distutils here], so you can simply unpack the archive, cd into the resulting directory, and execute the following command:
 
  python setup.py install
 
  python setup.py install
 +
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.
  
Note: you may have to execute the command using sudo depending on the permissions of the directories involved.
+
Though there are yum packages available for installing many python modules, it is recommended that you use the above method because it saves you the trouble of having to move the module directory around or having to add the module to your python path if the install doesn't correctly place it to a directory in your python path already.

Revision as of 21:18, 10 August 2010

Python is an interpreted language with familiar syntax. It's a great language to use for parsing text files and rapid application development. Rather than the time-consuming write-compile-test-rewrite-recompile-retest-etc. cycle of languages like C and C++, Python scripts run through an interpreter. This eliminates the compile step and speeds up the development process.

Getting Started

Installing Python

yum install python

should do the trick. Make sure that python is in your path with

which python

and modify your path if necessary.

Using Python

Python programs can be run in several ways. Two of the most common ways are through the python shell and in .py files. To enter the python shell, simply type

python

into your shell. Interactive mode should start up, and you can type commands to be directly executed. You can exit interactive mode with ctrl+d or with

quit()

To run a .py file, simply use:

python <filename> <args>

Syntax

Since the online documentation for Python is quite extensive and helpful, the online Python tutorial is definitely the place for any and all of your Python-related needs. Be sure to read the data structures page to understand how they work in Python.

Installing Python Modules

Most likely the module will be distributed according to the python standard described here, so you can simply unpack the archive, cd into the resulting directory, and execute the following command:

python setup.py install

Note: you may have to execute the command using sudo depending on the permissions of the directories involved.

Though there are yum packages available for installing many python modules, it is recommended that you use the above method because it saves you the trouble of having to move the module directory around or having to add the module to your python path if the install doesn't correctly place it to a directory in your python path already.