Installation Wiki

Setting up Working Environment for Python

From InstallationWiki

Jump to: navigation, search
Python Paste
Official Page
Project Documentation
Download
Source Book
200px-184719494X.jpg
ISBN 978-1-847194-94-7
Publisher Packt Publishing
Author(s) Tarek Ziadé

After Installing Python, you need to set up the working environment before getting started.

Contents

[edit] Working Environment

Taking time to set up the working environment is important for productivity. The time used to sharpen the tools is never wasted. It is a bad idea to force the usage of a specific set of tools on all developers when you lead a project. It is better to let each person take care of his or her desk as long as a common set of standards is adopted.

Working on a Python project means writing code, but it also means interacting with data files and third-party servers such as code repositories.

There are two paths to set such an environment: either by building it with a composition of small tools (the old school way), or by using an all-in-one tool (the new school way). Of course, there are various blends between these, and every developer should build his or her environment the way he or she likes it.

To complete the prompt, the developers can use:

  • A classical code editor such as Vim or Emacs, or any other can be used as long as it provides a friendly mode for Python code. This editor has to be completed with a set of tools.
  • An Integrated Development Environment that integrates everything can be used. Eclipse with PyDev is the best pick at this time.

[edit] Using an Editor and Complementary Tools

This kind of environment is the longest one to prepare, but probably the most productive one. This is because you will be able to tweak it to make it fit with the way you are working. If you always use the same computer, it is easier to install and configure a set of chosen tools. But preparing a portable environment is even better. You can bundle it, for example, in a USB key and use it on any computer. It is also a good practice to use the same tools no matter what the platform is. This will help you in working efficiently anywhere.

Portable Python and similar projects:

Portable Python is a project that provides such a feature for Windows, by offering a ready-to-use embedded version of Python and a code editor. We will not create such an exhaustive environment if the target already has Python installed. But this project has an interesting approach and should be looked over. See http://www.portablepython.com.

Damn Small Linux (DSL) is also an interesting solution to embed a set of tools in a USB drive. It knows how to run a Linux embed into a system emulator called Qemu, which runs on any platform. So having a tweaked DSL with Python installed can provide the same features. See http://www.damnsmalllinux.org/usb-qemu.html.

Dragon technology provides a live Ubuntu system that can be used to build a portable Python environment. See http://www.dragontechnology.com/ubuntu_usb.php.

Starting from there, a working environment will be composed of:

  • A code editor that can be found on all platforms, preferably open-source and free
  • A few extra binaries that provide some features we do not want to rewrite in Python

[edit] Code Editor

Many editors are available that are compatible with Python. In a working environment composed of multiple tools, the best pick is an editor that is focused on editing the code and nothing else. That said, the boundary between a simple code editor and an Integrated Development Environment (IDE) will always be a bit fuzzy. Even simple editors provide ways to extend or interact with the system. But a well-configured code editor will not bother you with superfluous features.

For many years, the best choices in this area have been Vim (http://www.vim.org) or Emacs (http://www.gnu.org/software/emacs). They seem unfriendly at first because they have their own standards based on specific keyboard shortcuts, and it takes quite a while to get familiar with them. But when the commands are under control, they are the most productive tools a developer can have. They provide Python-specific modes, and know how to edit other files with a dedicated mode on each format.

Vim is a Python-friendly editor, and lately, the community has shown a lot of interest in it. It can be easily extended with Python. As an example, look up this Pycon 2007 talk: http://www.tummy.com/Community/Presentations/vimpython-20070225/vim.html.

A big advantage of Vim is that it has been installed on all Linux systems for years, so if you have to work on someone else's system or on a server, it will be available.

The next section presents Vim installation and configuration. If you are more likely to use Emacs, a good starting point is this page: http://www.python.org/emacs.

[edit] Installing and Configuring Vim

The latest version is 7.1 and comes with nice features such as a bundled code completer.

If you are under Linux, a version of Vim should already be installed, but probably a version older than 7.1. Check this with the vim --version command. If your version is below 7.0, you should upgrade it either by using the package system of your distribution, or by compiling Vim.

On other systems, Vim needs to be installed. Windows users can get the self-installing executable that provides gvim (a version that comes with a graphical user interface) and also a console version. Mac OS X users need to compile the 7.1 version because binaries for the latest version are not currently available.

Get the right version from the download page here: http://www.vim.org/download.php, and compile if necessary.

If you need to compile Vim while working with multi-byte characters (such as accented letters in French), you need to call configure with the --enable-multibyte command. The compilation sequence will look like this:

 ./configure --enable-multibyte
make
sudo make install

This will install Vim in /usr/local, and the binary will be available at: /usr/local/bin/vim.

The last thing to do is to create a .vimrc file in your home directory if you are under Linux or Mac OS X, and a _vimrc file under Windows. In this last case, you should save it in the installation folder, and add an environment variable called VIM containing this path, so Vim will know where to get it.

The vimrc file content is as follows:

set encoding=utf8
set paste
set expandtab
set textwidth=0
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set backspace=indent,eol,start
set incsearch
set ignorecase
set ruler
set wildmenu
set commentstring=\ #\ %s
set foldlevel=0
set clipboard+=unnamed
syntax on

For instance, the tabstop option will transform a <Tab> stroke into four spaces.

Remember that the :help command under Vim can be called on each option, to understand what it does.

For example, :help ruler will display a help screen on the ruler option.

Vim should be ready to run from here.

[edit] Using Another Editor

If you cannot get used to Vim or Emacs and want a visual mode editor that interacts a little more with the mouse, you can pick another editor. But it should provide a Python mode and respect the following criteria:

Replacing the <Tab> keystroke by four spaces: This is the most important feature and is now handled correctly by most editors. If the editor you try does not have it, just drop it. Otherwise, you will end up with mixing the tab and spaces in your code, which is a mess for the compiler.

  • Removing the trailing spaces on save
  • Offering smart cursor placement on new lines, to speed up the writing
  • Providing a standard color-code highlighting
  • Offering simple code completion

There are a lot of other criteria that can be looked over to compare the code editors. Some are a bit unnecessary such as the code folding, whereas others are quite useful such as API searching. But having the Python interactive prompt, besides the editor, covers enough features to be efficient with the five criteria just mentioned.

If you really feel uncomfortable with editors such as Vim or Emacs, you probably belong to the new school crew.

[edit] Extra Binaries

To complete the editor, a few binaries can be installed to cover common needs:

  • diff, from GNU diffutils, helps comparing the content of two folders or files. This program is available by default on all Linux distributions and Mac OS X. It has to be installed on Windows, and an installer can be found here: http://gnuwin32.sourceforge.net/packages/diffutils.htm. When it is installed, the diff command is available in the prompt.
  • grep provides a command-line utility to search for strings from files. It is more powerful than the system tools, and works in the same way on all platforms. It is available by default on Linux and Mac OS X. It has to be installed on Windows, and can be found here: http://gnuwin32.sourceforge.net/packages/grep.htm.

Notice that both grep Under Windows, these are available with MSYS.

[edit] Using an Integrated Development Environment

Besides a code editor, all complementary tools are integrated in an IDE. This makes it really fast to deploy and use.

The best free open-source IDE for Python available at this time is Eclipse (http://www.eclipse.org) combined with the PyDev (http://pydev.sourceforge.net) plug-in. This add-on is not free.

A very good commercial alternative is Wingware IDE. See http://wingware.com.

Eclipse is portable and will let you work in the same way on any computer. PyDev is a plug-in that enriches Eclipse with certain Python features such as:

  • Code completion
  • Syntax highlighting
  • Quality Assurance (QA) tools such as PyLint and Bicycle Repair Man
  • Code coverage
  • An integrated debugger

[edit] Installing Eclipse with PyDev

Eclipse is written in Java, so the first step is to install the Java Runtime Environment (JRE). If you are running Mac OS X, JRE is already installed. The latest version of JRE can be found on Sun's website at: http://java.sun.com/javase/downloads/index.jsp. Download the correct installer and follow the instructions to deploy it on your system.

Eclipse does not provide an installer, since it is just a folder with Java scripts. So its installation is just a matter of getting an archive and uncompressing it on the system. The plug-ins can then be added through the Eclipse interface with a neat package system. But it can be really painful to install the correct set of plug-ins as the latest Eclipse version might not be compatible with them.

Since the extra plug-ins can be bundled in an archive, the simplest way is to get a custom distribution of Eclipse. There are no specialized distributions for Python, but you can create them online on your own.

Yoxos provides this feature through an AJAX installer located at: http://ondemand.yoxos.com/geteclipse/W4TDelegate. This web page lets you pick the plug-ins you need and prepares a downloadable archive. Search PyDev for an Eclipse plug-in, and double-click on it in the plug-in list tree on the left. This will add it with all its dependencies. You can then click on the Download button on the top right corner to get your archive.

Uncompress the archive on your system, for example in c:\Program Files\Eclipse under Windows, and in your home directory under Linux or Mac OS X. You will find a shortcut in this folder to launch the application. Eclipse will then be ready to use.

[edit] Additional References

  • For instructions on Creating template for Python package, click here
  • For instructions on Installing Python, click here

[edit] Source

The source of this content is Chapter 1: Getting started of Expert Python Programming by Tarek Ziadé (Packt Publishing, 2008).

Personal tools