Category Archives: Python

Integrate Python and Eclipse IDE

There are two main ways you can work with Python: through the command line or through an IDE. I’ve chosen the Eclipse IDE.

  1. Eclipse requires Java Virtual Machine (JVM) – Download and install the Java Developer Kit.
  2. Download and install the 32-bit Kepler version of Eclipse.
  3. Install plug-ins to integrate Eclipse and Python:
    • Mylyn:
      1. Help -> Install New Software
      2. Beside “Work with” – Add “Mylyn” – “http://download.eclipse.org/mylyn/releases/latest”
      3. Select All and follow prompts to install – restart Eclipse
    • Pydev:
      1. Help -> Install New Software
      2. Beside “Work with” – Add “Pydev” – “http://pydev.org/updatest”
      3. Select All and follow prompts to install – restart Eclipse
  4. Configure Pydev – within Eclipse, select Window -> Preferences -> Pydev -> Interpreters -> Python Interpreter :: Enter c:\Python34\python.exe in top panel and click way through to set up.

Setting up Python in Windows 8.1

Set up Python on Windows 8.1

1. Visit the official Python download page and grab the Windows installer. Choose the 32-bit version.

2. Run the installer and accept all the default settings, including the “C:\Python34″ directory it creates.


3. Next, set the system’s PATH variable to include directories that include Python components and packages we’ll add later. To do this:

  • Open the Control Panel (you can find it using Search on the Charms Bar).
  • In the Control Panel, search for and open System.
  • In the dialog box, select Advanced System Settings.
  • In the next dialog, select Environment Variables.
  • In the User Variables section, edit the PATH statement to include this (if there is no PATH variable, click NEW to create one):
C:\Python34;C:\Python34\Lib\site-packages\;C:\Python34\Scripts\;

4. Now, you can open a command prompt (Charms Bar | Search | cmd) and type:

C:\> python

That will load the Python interpreter:

Python 3.4.1  etc etc
Type "help", "copyright", "credits" or license for more information.
>>>

Because of the settings you included in your PATH variable, you can now run this interpreter — and, more important, a script — from any directory on your system.

Press Control-Z plus Return to exit the interpreter and get back to a C: prompt.

Set up useful Python packages

setuptools and pip are installed with python 3.4.1 -  they will cover most of your installation needs, so go ahead and add pip. MechanizeRequests and BeautifulSoup are must-have utilities for web scraping, and we’ll add those next:

C:\> pip install mechanize
C:\> pip install requests
C:\> pip install beautifulsoup4

4. csvkit, which was covered here, is a great tool for dealing with comma-delimited text files. Add it:

C:\> pip install csvkit

You’re now set to get started using and learning Python under Windows 8.1. If you’re looking for a handy guide, start with the Official Python tutorial.

BUT FIRST, … install Eclipse IDE to support working in Python.

Eclipse & JVM

How to install Python 3.4.1 on CentOS 6

CentOS 6 ships with Python 2.6.6 and several critical system utilities, for example yum, will break if the default Python interpreter is upgraded. The trick is to install new versions of Python in /usr/local so that they can live side-by-side with the system version.

Execute all the commands below as root either by logging in as root or by using sudo.

Preparations – install prerequisites

In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.

 

Things to consider

Before you compile and install Python there are a few things you should know and/or consider:

Unicode

Python has a long and complicated history when it comes to Unicode support. In Python 3.4 the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.

Shared library

You should probably compile Python as a shared library. If you compile Python as a shared library you must also tell it how to find the library. Our option:

  • Compile the path into the executable by adding this to the end of the configure command:LDFLAGS="-Wl,-rpath /usr/local/lib"

Use “make altinstall” to prevent problems

It is critical that you use make altinstall when you install your custom version of Python. If you use the normal make install you will end up with two different versions of Python in the filesystem both namedpython. This can lead to problems that are very hard to diagnose.

Download, compile and install Python

Here are the commands to download, compile and install Python.

After running the commands above your newly installed Python interpreter will be available as /usr/local/bin/python3.4. The system version of Python 2.6.6 will continue to be available as /usr/bin/python/usr/bin/python2 and /usr/bin/python2.6.

Setuptools + pip

Setuptools has replaced Distribute as the official package manager used for installing packages from the Python Package Index. Setuptools and pip are installed with Python 3.4.1. It builds on top of Setuptools and provides a few extra functions that are useful when you manage your packages.

The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (where X.Y is the Python version).

What’s next?

Since you are using Python 3.4 you don’t need to install virtualenv because that functionality is already built in.

Each isolated Python environment (also called sandbox) can have its own Python version and packages. This is very useful when you work on multiple projects or on different versions of the same project.

Create your first isolated Python environment

When you use pyvenv to create a sandbox you must install setuptools and pip inside the sandbox. You can reuse the ez_setup.py file you downloaded earlier and just run it after you activate your new sandbox.

Unix (wget)

Most Linux distributions come with wget.

Download ez_setup.py and run it using the target Python version. The script will download the appropriate version and install it for you:

> wget https://bootstrap.pypa.io/ez_setup.py -O - | python

Note that you will may need to invoke the command with superuser privileges to install to the system Python:

> wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

Alternatively, Setuptools may be installed to a user-local path:

> wget https://bootstrap.pypa.io/ez_setup.py -O - | python - --user

Unix including Mac OS X (curl)

If your system has curl installed, follow the wget instructions but replace wget with curl and -O with -o. For example:

> curl https://bootstrap.pypa.io/ez_setup.py -o - | python

Advanced Installation

For more advanced installation options, such as installing to custom locations or prefixes, download and extract the source tarball from Setuptools on PyPIand run setup.py with any supported distutils and Setuptools options. For example:

setuptools-x.x$ python setup.py install --prefix=/opt/setuptools

Use --help to get a full options list, but we recommend consulting the EasyInstall manual for detailed instructions, especially the section on custom installation locations.

How To Set Up Python 3.3.3 on CentOS 6.4

Introduction

When you get started with CentOS, however, you will be surprised to see that Python still is at version 2.6 and it should not be used by deployed applications regardless as it is reserved for system’s use.

We are going to talk about downloading and setting up Python 3.3.3 without breaking the system’s default. It is rather important to not to get involved with that as critical system tools such as YUM depend on it. Furthermore, we will install two popular must-have Python companions pip and virtualenv.

After we are done, you will be able to simultaneously use either versions of Python on your CentOS 6.4 VPS, create and use virtual environments and finally, download and manage Python packages for each version.

Preparing The System and Installing Python

The installation of Python on CentOS consists of a few (simple) stages, starting with updating the system and followed by actually getting Python and proceeding with the set up process.

Remember: You can see all available releases of Python by checking out the Releases page.

Updating the Default CentOS Applications

Before we begin with the installation, let’s make sure to update the default system applications to have the latest versions available.

yum update

Preparing The System for Development Installations

CentOS distributions are lean – perhaps, a little too lean – meaning they do not come with many of the popular applications and tools that you are likely to need.

This is an intentional design choice. For our installations, however, we are going to need some libraries and tools (i.e. development [related] tools) not shipped by default. Therefore, we need to get them downloaded and installed before we continue.

The recommended of doing this is simply downloading a bunch of tools by a single command with yum software groups.

YUM Software Groups consist of bunch of commonly used tools (applications) bundled together, ready for download all at the same time via execution of a single command and stating a group name. Using YUM, you can even download multiple groups together.

How to Install Development Tools using YUM on CentOS

yum groupinstall -y 'development tools'
yum install -y zlib-dev openssl-devel sqlite-devel bzip2-devel

Downloading, Building and Installing Python

Let’s begin with retrieving the (compressed) archive containing Python source code.

wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tar.xz

Extracting the Compressed Source Archive

# Let's decode (-d) the XZ encoded tar archive:
xz -d Python-3.3.3.tar.xz

# Now we can perform the extraction:
tar -xvf Python-3.3.3.tar

Configuring and Installation

Before building the source, we need to make sure that all the dependencies are there and prepare the environment. This is achieved automatically by using ./configure to handle the task for us.

# Enter the file directory:
cd Python-3.3.3

# Start the configuration (setting the installation directory)
# By default files are installed in /usr/local.
# You can modify the --prefix to modify it (e.g. for $HOME).
./configure --prefix=/usr/local    

This procedure should execute without any hiccups – as we have downloaded all the necessary tools and applications. When it is complete, we will be ready to move on to the next step: building and installing.

Building and Installing

After configuring everything for the system we are working on, we can continue with building (compiling) the source and installing the application. Normally, one would use “make install”; however, in order not to override system defaults – replacing the Python already used by the system – we will use make altinstall.

# Let's build (compile) the source
# This procedure can take awhile (~a few minutes)
make

# After building everything:
make altinstall

Example for version 3.3.3:

make && make altinstall   # <--- Two commands joint together

[Optional Step] Adding New Python Installation Location to PATH

Note: If you have followed the instructions using the default settings, you should not have the need to go through this section. However, if you have chosen a different path than /usr/local to install Python, you will need to perform the following to be able to run it without explicitly stating its full [installation] path each time.

Once the installation is complete, we can access the generated binaries (i.e. the Python interpreter for the version we have chosen) only by specifying its full location (path) (e.g. /usr/local/bin/python2.7) – unless of course the path exists already in the PATH variable (i.e. the variable which tells contains information on where to look for files stated).

If you would like to be able to access the newly installed Python interpreter without explicitly telling each and every time where to look for it, its path needs to be appended to PATH variable:

# Example: export PATH="[/path/to/installation]:$PATH"
export PATH="/usr/local/bin:$PATH"

Setting Up Common Python Tools pip and virtualenv

Having installed Python, we can now finalize completing the basics for application production and deployment. For this, we will set up two of the most commonly used tools: pip package manager and virtualenv environment manager.

If you are interested in learning more about these two tools or just quickly refreshing your knowledge, consider reading Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Installing pip on CentOS Using a New Python Installation

Before installing pip, we need to get its only external dependency – setuptools.

From the article on virtualenv and pip:

It [setuptools] builds on the (standard) functionality of Python’s distribution utilities toolset called distutils. Given that distils is provided by default, all we need left is setuptools.

Execute the following commands to install setuptools:

This will install it for version 2.7.6

# Let's download the installation file using wget:
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz

# Extract the files from the archive:
tar -xvf setuptools-1.4.2.tar.gz

# Enter the extracted directory:
cd setuptools-1.4.2

# Install setuptools using the Python we've installed (2.7.6)
python2.7 setup.py install

Installing pip itself is a very simple process afterwards. We will make use of the instructions from the article mentioned above to have it downloaded and installed automatically and securely using cURL library.

Note: To learn more about cURL, you can refer to the section explaining it here.

Let’s download the setup files for pip and have Python (2.7) install it:

This will install it for version 2.7.6

curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python2.7 -

To learn about how to use pip, please refer to the article on Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Installing virtualenv on CentOS Using a New Python Installation

Now that we have pip the package manager ready, getting virtualenv on the system is a breeze.

Run the following command to download and install virtualenv:

pip install virtualenv

To learn about how to use virtualenv, please refer to the article on Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Submitted by: O.S. Tezer

Getting Started with the Google Data Python Library

https://developers.google.com/gdata/articles/python_client_lib

Jeffrey Scudder, Google Data APIs Team, August 2007

Introduction

So you’ve decided to use the Google Data Python client library to write an application using one of the many Google Data services. Excellent choice! My aim with this short tutorial is to quickly get you started in using the client library to develop your application.

You probably want to jump in and start creating your application right away. First though, you may need to configure your development environment and set up the tools you’ll need to run the modules included in the client library. Follow the steps below and you’ll be running code in no time.

Installing Python on Windows

There are quite a few implementations of Python to choose from in Windows, but for purposes of this guide, I’ll be using the .msi installer found onpython.org.

  1. Begin by downloading the installer from the Python download page.
  2. Run the installer – you can accept all the default settings – [Install to c:/python33] -
  3. To see if your install is working as expected, open a command prompt – [press Windows Start + x together, select Command Prompt (Admin)] – and run python -V.

Installing Dependencies

Currently, the only external dependency is an XML library named ElementTree. If you are using Python version 2.5 or higher, you won’t need to install ElementTree since it comes with the Python package.

Installing the Google Data Library

Download the Google Data Python library if you haven’t done so. Look for the latest version [*.zip] on the Python project’s downloads page.

After downloading the library to c:/python33, unpack it using unzip to c:/python33/gdata-2.0.18.

Now you are ready to install the library modules so that they can be imported into Python. There are several ways you can do this:

  • If you have the ability to install packages for all users to access, open a command prompt, navigate to c:/python33/gdata-2.0.18 and run setup.py install.

Once you’ve installed the Google Data library, you’re ready to take the library for a test drive.

Running Tests and Samples

The Google Data Python client library distributions include some test cases which are used in the development of the library. They can also serve as a quick check to make sure that your dependencies and library installation are working. From the top level directory where you’ve unpacked your copy of the library, try running:

./tests/run_data_tests.py

If this script runs correctly, you should see output on the command line like this:

Running all tests in module gdata_test
.......
----------------------------------------------------------------------
Ran 7 tests in 0.025s

OK

Running all tests in module atom_test
..........................................
----------------------------------------------------------------------
Ran 42 tests in 0.016s

OK

...

If you did not see any errors as the tests execute, then you have probably set up your environment correctly. Congratulations!

Actually, get syntax error – apply this patch?

Reported by tobioatom, Aug 1, 2011
remove gdata_tests.base_test
so this test won't break.
run_data_tests.py.patch
894 bytes   View   Download
Oct 7, 2011

Project Member #1 afs…@google.com

Thanks for the patch, there were a few other odds and ends needed removing, but it is applied. Unfortunately a number of the core service tests depends on Google Base, which is now turned off.

Don't know effect of turning off Google Base!

Now you can start running something more interesting. The distribution contains a samples directory which contains code which can provide a starting point for writing your application. If you’d like to try out a simple interactive sample, try running ./samples/docs/docs_example.py.

Open command prompt, navigate to ./samples/webmastertools

The Google Documents List API sample will prompt you for the email address and password for your Google account. If you have any documents or spreadsheets in Google Documents, you can list them by entering 1 for your selected operation. (If you don’t have any documents or spreadsheets, you’ll get a 404 error.)

If there is a particular Google Data service which interestes you, take a look at the sample for it to get a basic idea of how you can use the library. Most, but not all, of the features are demonstrated in the sample code, so you might want to take a look at the documentation for the service you plan to work with to get a clear picture of what you can do with the API.

Writing a “Hello World” Example

Let’s start with a simple example. Here’s a short program to print a list of all of the documents in your Google Documents account:

import gdata.docs.service

# Create a client class which will make HTTP requests with Google Docs server.
client = gdata.docs.service.DocsService()
# Authenticate using your Google Docs email address and password.
client.ClientLogin('jo@gmail.com', 'password')

# Query the server for an Atom feed containing a list of your documents.
documents_feed = client.GetDocumentListFeed()
# Loop through the feed and extract each document entry.
for document_entry in documents_feed.entry:
  # Display the title of the document on the command line.
  print document_entry.title.text

Either save the above code snippet as a file and run it, or paste the code into the Python interpreter to see the Google Data Python client library at work.

Conclusion

Now that you’ve installed and tested the Google Data Python client library, you’re ready to start writing the next great application using:

As you continue to develop your application, you may hit a snag. If so, please check out the list of resources below:

If you happen to think of a great new feature for the library (or by chance find a bug), please enter it in the discussion group. We’re always interested in your feedback!

Happy coding :-)