Python Programming/Editing and Running Python Code

The Python interactive mode is great. It lets us experiment, debug, and test your code as you go along, but you do not want to have retype your code every time you want to use it. Thankfully we can store and run Python code from text files, and you can use any standard text editor. There are slight differences in how 'plain text files' formatted on Windows, Unix-ish, and Apple platforms.

EditorsEdit

Many editors have the ability to at least highlight code, and most that have Python capabilities will also assist you with formatting your code properly. Remember in Python, whitespace is significant. Some editors may even have code completion capabilities, now we are moving into the realm of IDE's, and the distinction between editor and IDE is becoming blurred.

eg Notepad++ Scite


IDE'sEdit

An IDE, or Integrated Development Environment, is an Editor that has additional features. Most IDE's that do Python in a serious way do code-completion, syntax highlighting, project management, version control, have a Python Console, output console where output from your program is placed

eg Eclipse/PyClipse, Emacs, Vim, Spyder, Eric, Idle

There are several IDEs available for Python. A list can be found here: http://wiki.python.org/moin/IntegratedDevelopmentEnvironments

Installing Python PyDEV Plug-in for Eclipse IDEEdit

You can use the Eclipse IDE as your Python IDE. The only requirement is Eclipse and the Eclipse PyDEV Plug-in.

Go to http://www.eclipse.org/downloads/ and get the proper Eclipse IDE version for your OS platform. There are also updates on the site, but just look for the basic program, Download and install it. The install just requires you to unpack the downloaded Eclipse install file onto your system.

You can install PyDEV Plug-in two ways:

  • Suggested: Use Eclipse's update manager, found in the tool bar under "Help" -> "install new Software". add http://pydev.org/updates/ in "work with" click add, and select PyDEV ,and click "Next" and let Eclipse do the rest. Eclipse will now check for any updates to PyDEV when it searches for updates.
    • If you get an error stating a requirement for the plugin "org.eclipse.mylyn", expand the PyDEV tree, and deselect the optional mylyn components.
  • Or install PyDEV manually, by going to http://pydev.sourceforge.net and get the latest PyDEV Plug-in version. Download it, and install it by unpacking it into the Eclipse base folder.

Python Mode for EmacsEdit

There is also a python mode for Emacs which provides features such as running pieces of code, and changing the tab level for blocks. You can download the mode at https://launchpad.net/python-mode

Vim support PythonEdit

The venerable vim (vi improved) has excellent support for python.

EricEdit

A purpose build editor with PyQT for Python development.

IDLEEdit

The editor that ships with python, or easily installable on most linux distributions through their respective package management.

Other EditorsEdit

There are countless other editors and environments that support Python. Just visit the Python Website for a taste.

Managing CodeEdit

As the amount of code that you write increases, you will run across a couple of problems that have been solved in different ways.

Libraries and ModulesEdit

Once programs become non-trivial, we run into the requirement of storing code into a series files, or a library. Python calls libraries of code, modules, and we will discuss them in much greater detail later on. Modules also allow us to share code between multiple developers and projects. Managing your 'Python environment' can become complex as you add more libraries from other developers. If you are using Linux, your distribution will likely help you with having some libraries already packaged in their repositories, but there is also the pip tool which lets you download from the Python Package Index (PyPI). When intermixing you do have to be very careful as you can run into 'dependency hell'. When you reach this point of development, it is worthwhile learning how to make your own packages for your distribution.

Version ControlEdit

The second problem that we run into one our programs become non-trivial is tracking changes and versions. This becomes vital your programs get larger and more complex, and as you work with multiple developers. One feature of modern version control systems let you branch your code, so that you can start from a 'known working state' and if you go down the wrong path, you can 'revert' back to where you started. Most open source projects use Git, Mercurial (hg) or Bazar (bzr). There is a book on wikibooks about git, but otherwise using version control systems is out of the scope of this book.

Installing new modulesEdit

Although many applications and modules have searchable webpages, there is a central repository for searching packages for installation, known as the "Cheese Shop".

See AlsoEdit

Last modified on 13 April 2014, at 22:46