Skip to content

2. Installing a Python interpreter

2.1. ActivePython

The examples in this document have been tested with the ActiveState Python interpreter:

http://www.activestate.com/activepython/downloads
 

Notes:

  • Do not download the 64-bit version of ActivePython. It does not allow the installation of the MySQLdb module, which is required later in this document.

Installing ActivePython creates the following directory structure:

 

and the following menu in the Applications folder:

  • 1: the ActivePython documentation. This is very comprehensive, and it’s a good idea to consult it whenever you encounter a problem;
  • 2: an interactive Python interpreter;
  • 3: the ActivePython package manager. We will use this to install the Python package that allows us to work with MySQL databases.

We will not be using the interactive Python interpreter. You should simply be aware that the scripts in this document could be run using this interpreter. While it is useful for testing how a Python feature works, it is not very practical for scripts that need to be reused. Here is an example:

 

The >>> prompt allows you to enter a Python statement that is executed immediately. The code typed above means the following:

1
2
3
4
5
6
>>> name='python'
>>> print 'name=%s' % (name)
name=python
>>> print "type=%s" % (type(name))
type=<type 'str'>
>>> 

Lines:

  • 1: Initialization of a variable. In Python, you do not declare the type of variables. They automatically take on the type of the value assigned to them. This type may change over time;
  • 2: display of the name. 'name=%s' is a display format where %s is a formal parameter denoting a string. (name) is the actual parameter that will be displayed in place of %s;
  • 3: the result of the display;
  • 4: display of the type of the variable name;
  • 5: the variable name is of type str (string).

Below, we provide scripts that have been tested as follows:

  • the script can be written using any text editor. Here we used Notepad++, which offers syntax highlighting for Python scripts;
  • the script is run in a DOS window.
 
  • Navigate to the folder containing the script to be tested;
  • The %python% variable is a system variable:
C:\data\work\2010-2011\python\tutorial>echo %python%
C:\devpython\ActiveState27\python.exe
  • Line 1: displays the value of the %python% environment variable;
  • Line 2: its value: <installdir>\python.exe, where installdir is the ActivePython installation folder.

2.2. Python Tools for Visual Studio

[Python Tools for Visual Studio] is available (February 2012) at the following URL: [http://pytools.codeplex.com/]. Running it adds Python features to Visual Studio 2010. The Express version of Visual Studio 2010 is free and available at [http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express].

Once the [Python Tools for Visual Studio] extensions are installed, you can create Python projects with VS 2010:

  • for all examples in this document, option [1] is appropriate;
  • [2]: The menu (Tools / Options) allows you to configure the Python environment.

The ActivePython interpreter should have been detected by Visual Studio [3]:

  • in [4], we specify that program execution should pause until the user presses a key on the keyboard.

Let’s create a Python project (File / New / Project):

  • in [1,2]: select a Python project of type [Python Application];
  • in [3]: choose a folder for the project;
  • in [4]: give the project a name;
  • in [5]: the generated project.

The file [essai_02.py] is a Python program:


print('Hello World')

Run it by pressing [Ctrl-F5]: the result appears in a DOS window [6]. All the code in this document has also been tested in this way. The examples are provided as a VS 2010 solution: