augustus

Python-Mysqldb (troubleshooting)

1. check if python-mysqldb installed and find where it files located:

$ dpkg-query -s python-mysqldb

bla bla bla…

$ dpkg-query -L python-mysqldb

/.
/usr
/usr/lib
/usr/lib/python2.4
/usr/lib/python2.4/site-packages
/usr/lib/python2.4/site-packages/_mysql.so
/usr/lib/python2.5
/usr/lib/python2.5/site-packages
/usr/lib/python2.5/site-packages/_mysql.so
/usr/share
/usr/share/doc
/usr/share/doc/python-mysqldb
/usr/share/doc/python-mysqldb/FAQ.txt.gz
/usr/share/doc/python-mysqldb/changelog.Debian.gz
/usr/share/doc/python-mysqldb/copyright
/usr/share/doc/python-mysqldb/changelog.gz
/usr/share/doc/python-mysqldb/README.gz
/usr/share/doc/python-mysqldb/MySQLdb.txt.gz
/usr/share/pycentral
/usr/share/pycentral/python-mysqldb
/usr/share/pycentral/python-mysqldb/site-packages
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/__init__.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/CR.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/FIELD_TYPE.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/ER.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/FLAG.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/REFRESH.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/constants/CLIENT.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/__init__.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/converters.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/connections.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/cursors.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/release.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQLdb/times.py
/usr/share/pycentral/python-mysqldb/site-packages/_mysql_exceptions.py
/usr/share/pycentral/python-mysqldb/site-packages/MySQL_python2.5-1.2.1_p2.egg-info
/usr/share/pycentral/python-mysqldb/site-packages/MySQL_python2.4-1.2.1_p2.egg-info

Ok, we find binary _mysql.so module. Let’s go to /usr/lib/python2.5/site-packages/ and check if this module working…
$ cd /usr/lib/python2.5/site-packages/
$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2-default ubuntu installation
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import _mysql

Ok, binary module works!

>>> import MySQLdb

Yes! MySQLdb imports!

If you will get import error here (or some other error) so you have problems with mysql module. Otherwise your python just “looks” for packages in other places and don’t looks here.

Let’s look where your python start from:

$ which python
/usr/bin/python

If there is any other path then you run non default ubuntu python. If you add that other python expressly you have to be able to check your PYTHONPATH (where python search for modules) and add site-packages with MySQLdb iunstalled to it.

You can check your PYTHONPATH like this:

$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import sys
>>> print sys.path
[”, ‘/usr/lib/python25.zip’, ‘/usr/lib/python2.5’, ‘/usr/lib/python2.5/plat-linux2’, ‘/usr/lib/python2.5/lib-tk’, ‘/usr/lib/python2.5/lib-dynload’, ‘/usr/local/lib/python2.5/site-packages’, ‘/usr/lib/python2.5/site-packages’, ‘/usr/lib/python2.5/site-packages/Numeric’, ‘/usr/lib/python2.5/site-packages/gst-0.10’, ‘/var/lib/python-support/python2.5’, ‘/usr/lib/python2.5/site-packages/gtk-2.0’, ‘/var/lib/python-support/python2.5/gtk-2.0’]

Hope this helps….

 

Fonte: http://ubuntuforums.org/archive/index.php/t-369409.html

EasyInstall (Python)

Downloading and Installing a Package

For basic use of easy_install, you need only supply the filename or URL of a source distribution or .egg file (Python Egg).

Example 1. Install a package by name, searching PyPI for the latest version, and automatically downloading, building, and installing it:

easy_install SQLObject

Example 2. Install or upgrade a package by name and version by finding links on a given “download page”:

easy_install -f http://pythonpaste.org/package_index.html SQLObject

Example 3. Download a source distribution from a specified URL, automatically building and installing it:

easy_install http://example.com/path/to/MyPackage-1.2.3.tgz

Example 4. Install an already-downloaded .egg file:

easy_install /my_downloads/OtherPackage-3.2.1-py2.3.egg

Example 5. Upgrade an already-installed package to the latest version listed on PyPI:

easy_install –upgrade PyProtocols

Example 6. Install a source distribution that’s already downloaded and extracted in the current directory (New in 0.5a9):

easy_install .

Fonte: http://peak.telecommunity.com/DevCenter/EasyInstall#using-easy-install

Python 2.7 @ Squeeze

Python 2.7 in Debian Squeeze 6.0
19 May 2011
Posted by stylesen

In work I wanted python2.7 for using some latest modules. I run Debian 6.0 ie., squeeze in my laptop, but python2.7 is available in Wheezy or Debian testing. In order to install python2.7 from the testing repository add the following in your sources.list file ie., /etc/apt/sources.list

deb http://mirror.cse.iitk.ac.in/debian/ testing main contrib

After adding the testing repository, do an update and then install python2.7 package as follows:

$ sudo aptitude update

$ sudo aptitude install python2.7

Now we will have python2.7 installed in our debian squeeze system, but wait! This will give more problems since all other softwares installed in the system are dependent on python2.6 which is the default in Debian 6.0. One of the first problems is ‘update-manager’ will not work, since it depends on python2.6. I also suggest to remove the testing repository added to ‘sources.list’ if you do not plan to ‘dist-upgrade’ to the testing (wheezy) release.

It will be better to have a way to switch between python versions (that too Debian way would be great!). Following is how I acheieved this using update-alternatives.

Add all the python versions and create a ‘update-alternatives’ link:

$ sudo update-alternatives –install /usr/bin/python python /usr/bin/python2.7 2

$ sudo update-alternatives –install /usr/bin/python python /usr/bin/python2.6 1

$ sudo update-alternatives –install /usr/bin/python python /usr/bin/python2.5 0

NOTE: The 2, 1, 0 at the end of the commands above are priorities and it is required to define the priorities in ‘auto-mode’.

Once we have added all the python versions, switching between these python versions is very easy like the following:

$ sudo update-alternatives –config python
There are 3 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
————————————————————
0 /usr/bin/python2.7 2 auto mode
1 /usr/bin/python2.5 0 manual mode
* 2 /usr/bin/python2.6 1 manual mode
3 /usr/bin/python2.7 2 manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode.
stylesen@dejas:~$ python
Python 2.7.1+ (default, Apr 20 2011, 22:33:39)
[GCC 4.5.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
stylesen@dejas:~$ sudo update-alternatives –config python
There are 3 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
————————————————————
0 /usr/bin/python2.7 2 auto mode
1 /usr/bin/python2.5 0 manual mode
2 /usr/bin/python2.6 1 manual mode
* 3 /usr/bin/python2.7 2 manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.5 to provide /usr/bin/python (python) in manual mode.
stylesen@dejas:~$ python
Python 2.5.5 (r255:77872, Nov 28 2010, 19:00:19)
[GCC 4.4.5] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
stylesen@dejas:~$ sudo update-alternatives –config python
There are 3 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
————————————————————
0 /usr/bin/python2.7 2 auto mode
* 1 /usr/bin/python2.5 0 manual mode
2 /usr/bin/python2.6 1 manual mode
3 /usr/bin/python2.7 2 manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python2.6 to provide /usr/bin/python (python) in manual mode.
stylesen@dejas:~$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

Thus we can have python2.7 installed in a debian squeeze system.

Update: Another non-debian way of installing multiple versions of python using ‘pythonbrew’ is available in this link – http://www.howopensource.com/2011/05/how-to-install-and-manage-different-versions-of-python-in-linux/

Fonte: http://www.stylesen.org/python_27_debian_squeeze_60

Python Eggs

A small introduction to Python Eggs

You can find Python Eggs on quite a few places on the web, e.g. at a package author’s website. The biggest repository of eggs is the Cheeseshop (or PyPI) though), an index for Python packages. In order to be able to install eggs you simply need to install easy_install which is easily done by downloading ez_install.py (you can download it here) and calling it (you need to have rights to install components in your python installation of course).

Once you have done this you can simply install an egg by calling:

easy_install somepackage.egg

You can also give a URL to an egg and use

easy_install http://somehost.somedomain.com/somepackage.egg

If an egg is not found at that location or in the directory you give, easy_install will automatically query the Cheeseshop for the egg location. So if you want to install SimpleJSON you simply give

easy_install simplejson

and it will download and install the most recent version. If you are not running as root and you have Python installed as root you of course need to use something like “sudo easy_install” instead (e.g. on MacOSX).

A great additional feature is also that Eggs can define dependencies on other packages which easy_install will then try to automatically download and install aswell.

BTW, the easy_install program is part of the setuptool package by Philip Eby and is based on the distutils package which is part of the standard python distribution.

Creating Python Eggs

Now we know how to install Python Eggs, how can we actually create them? There are actually two ways to do that, one is more manual and the other directly creates a skeleton for you to work with which might be handy for projects you start from scratch.

As an example we will use the above mentioned eventlet package (you can find the SVN repository here). The directory structure right now looks like this:

README
eventlet
examples
setup.py

So as we can see it already has a setup.py which is part of the distutils package and makes it easy to install eventlet by simply typing

python setup.py install

This will build and install the library in your Python installation. You need to make sure you have greenlet installed though.

The setup.py now looks like this:

#!/usr/bin/env python

from distutils.core import setup

setup(
name=’eventlet’,
version=’0.1′,
description=’Coroutine-based networking library’,
author=’Linden Lab’,
author_email=’sldev@lists.secondlife.com’,
url=’http://wiki.secondlife.com/wiki/Eventlet’,
packages=[‘eventlet’])

So we can see some metadata about this package, like name, version, description, author and so on, but also a list of packages to be installed. In this case it’s “eventlet” and this means that the directory “eventlet” on the filesystem will be used as the package to install so you later can say from eventlet import …. The directory eventlet will be what will be in your PYTHONPATH.

Now so far it’s great but we might miss some features of eggs here, namely the ability to package it as one file, to register it with the cheeseshop and to define dependencies. So to add this we have to do just a few changes to setup.py.

The first one is to import not from distutils but from setuptools instead:

from setuptools import setup

And then we need to extend the amount of data mostly by further metadata we need for the cheeseshop, such as license and categories:

setup(
name=’eventlet’,
version=’0.1′,
description=’Coroutine-based networking library’,
author=’Linden Lab’,
author_email=’sldev@lists.secondlife.com’,
url=’http://wiki.secondlife.com/wiki/Eventlet’,
packages=[‘eventlet’],
long_description=”””\
eventlet is a coroutines-based network library for python …
“””,
classifiers=[
“License :: OSI Approved :: GNU General Public License (GPL)”,
“Programming Language :: Python”,
“Development Status :: 4 – Beta”,
“Intended Audience :: Developers”,
“Topic :: Internet”,
],
keywords=’networking eventlet nonblocking internet’,
license=’GPL’,
install_requires=[
‘setuptools’,
‘greenlet’,
],
)

(actually I haven’t checked the license of eventlet/mublib so this might be wrong but you can change this accordingly).

So we added those classifiers to put it into the right categories on Cheeseshop and most importantly we added the requirement “greenlet”. If you then install the egg setuptools will automatically install greenlet as well.

Once you have this you can create an egg out of this by calling

python setup.py bdist_egg

This will create an egg distribution file which you can find in dist/eventlet-0.1-py2.4.egg (if you used Python 2.4)

You can check the contents by calling unzip -t eventlet-0.1-py2.4.egg and as you can see it’s basically the contents of the eventlet directory plus an EGG_INFO directory with metadata. Also included are the .pyc files as it’s a binary distribution.

The resulting .egg file can now be distributed by whatever means and installed by the above easy_install command.

Starting from scratch with Python Paste

If you just want to start your project there is an easier way to create the initial structure. You simply use Python Paste and a template. What we are interested in is especially paste.script which is some sort of template based automation system.

So here is what you do to install the right packages:

easy_install -U ZopeSkel

You can alternatively just do

easy_install -U PasteScript

but ZopeSkel will install PasteScript as well and offers additionally templates which might be of use to you.

Now that PasteScript is installed you can list the available templates:

$ paster create –list-templates
Available templates:
archetype: A Plone project that uses Archetypes
basic_namespace: A project with a namespace package
basic_package: A basic setuptools-enabled package
basic_zope: A Zope project
nested_namespace: A project with two nested namespaces.
paste_deploy: A web application deployed through paste.deploy
plone: A Plone project
plone2.5_buildout: A buildout for Plone 2.5 projects
plone2.5_theme: A Theme for Plone 2.5
plone2_theme: A Theme Product for Plone 2.1 & Plone 2.5
plone3_buildout: A buildout for Plone 3 projects
plone3_portlet: A Plone 3 portlet
plone3_theme: A Theme for Plone 3.0
plone_app: A Plone App project
plone_hosting: Plone hosting: buildout with ZEO and any Plone version
tgbase: tg base template
tgbig: For more complex projects
tgwidget: TurboGears widget projects
turbogears: web framework

(I have Turbogears installed as well which is why here are more templates listed than you have)

The interesting ones for Python are basic_package and basic_namespace. We will use basic_package for now as it creates a package ready for egg distribution.

So we can call it like this:

paster create -t basic_package

and it will ask us a lot of questions, basically all the metadata we have to put into the setup.py file. After it’s finished you should have a new directory with a basic egg supporting structure. Now you just need to code your actuall component but that’s left as an exercise for the reader!

Fonte: http://mrtopf.de/blog/en/a-small-introduction-to-python-eggs/

Python Install

Installing Python 2.6/2.7 on Debian Lenny

Just a quick note to Google-eyed installers of Python who can’t get python to start after compiling it from source. Debian 5 ships with Python 2.5.x and complains about libpython after you install 2.6 or 2.7. The solution is very simple and quick:

Create the following 2 symlinks to sort yourselves out:

ln -s /usr/local/lib/libpython2.6.so.1.0 /usr/lib/
ln -s /usr/local/lib/libpython2.6.so /usr/

If you’re on a 64 bit OS replace /usr/lib/ with /usr/lib64/

My full setup script for Python including threads etc is:

# as sudo / root:
wget http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
tar -xvzf Python-2.6.4.tgz
cd Python-2.6.4
./configure –with-threads –enable-shared
make
make install
# fix libpython2.6 problem
ln -s /usr/local/lib/libpython2.6.so.1.0 /usr/lib64/
ln -s /usr/local/lib/libpython2.6.so /usr/

Fonte: http://www.harelmalka.com/?p=159