Arquivo de Tags: python

Problem:  When uploading images to Satchmo I got this error message: "Upload a valid image. The file you uploaded was either not an image or a corrupted image." even with valid JPEG files.

To test the Python Image Library (PIL) I’d  executed this code on python console:

from PIL import Image
file = open("/tmp/logo.jpg")
image_test = Image.open(file)
image_test.load()

what output:

<PixelAccess object at 0x37dce100>

This is ok, but when I’d run the same code inside a Django view I got the error "decoding error when reading image file".

I checked the Python Path and it was the same for both console and Django Application.

After looking on google I discover a post from Peter Schoenster on “Django-Photologue“  wiki page saying he had the same problem and discovered that python shell was using one version of libjpeg and Apache was using other. (He, as me had two version of this library on the system). He discovered it using the command

# lsof | grep libjpeg

I just remove the old libjpe62 from my system using:

# apt-get remove libjpeg62

And everything works !

Problem: Error on Uploading a image file to Satchmo. Message: "Upload a valid image. The file you uploaded was either not an image or a corrupted image."

To test the Python Image Library (PIL) I run this code on python console:

from PIL import Image
file = open("/tmp/logo.jpg")
image_test = Image.open(file)
image_test.load()

what output:

<PixelAccess object at 0x37dce100>

This is ok, but when I run the same code inside a Django view I got the error "decoding error when reading image file".

I checked the Python Path and it was the same for both console and Django Application.

After looking on google I discover a post from Peter Schoenster on “Django-Photologue”  home page saying he had the same problem and discovered that python shell was using one version of libjpeg and Apache was using other. (He, as me had two version of this library on the system). He discovered it using the command

# lsof | grep libjpeg

I just remove the old libjpe62 from my system using:

# apt-get remove libjpeg62

And everything works !

When I tried to install the Library PIL on a Ubuntu 8.04 where I had installed  Python 2.6 I got:

.
.
.
running build_ext
building ‘_imagingtk’ extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/freetype2 -IlibImaging -I/opt/python.org/python/python-2.6/include -I/usr/local/include -I/usr/include -I/opt/python.org/python/python-2.6/include/python2.6 -c _imagingtk.c -o build/temp.linux-i686-2.6/_imagingtk.o
_imagingtk.c:20:16: error: tk.h: No such file or directory
_imagingtk.c:23: error: expected ‘)’ before ‘*’ token
_imagingtk.c:31: error: expected specifier-qualifier-list before ‘Tcl_Interp’
_imagingtk.c: In function ‘_tkinit’:
_imagingtk.c:37: error: ‘Tcl_Interp’ undeclared (first use in this function)
_imagingtk.c:37: error: (Each undeclared identifier is reported only once
_imagingtk.c:37: error: for each function it appears in.)
_imagingtk.c:37: error: ‘interp’ undeclared (first use in this function)
_imagingtk.c:45: error: expected expression before ‘)’ token
_imagingtk.c:51: error: ‘TkappObject’ has no member named ‘interp’
_imagingtk.c:55: warning: implicit declaration of function ‘TkImaging_Init’
error: command ‘gcc’ failed with exit status 1

I’d founded the file ‘tk.h‘ on ‘/usr/include/tcl8.4‘ and after a look at the setup.py file I changed the line number 41 from:

TCL_ROOT = None

to

TCL_ROOT = “/usr/include/tcl8.4″

Now you can run your ‘python setup.py install’ again and get PIL working!

Note: You may perhaps have to install the tk-dev and tcl-dev packages before running the setup.py. I’d already had these packages, so I’m not sure about it.

On Ubuntu run:

apt-get install tk-dev tcl-dev

Satchmo is a great shopping cart application that uses Python and Django. If you are looking for this kind of application it worth a look.

One of the best point of Satchmo is how easy and beautiful is to customize your application. You can create customized product model without touch the base Satchmo models. It’s a great feature, that make easy to apply future Satchmo updates.

You can learn how to create a custom product in JuanjoAlvarez Blog.

I followed the instructions but get the error:

Wed, 12 Aug 2009 11:57:17 configuration DEBUG SettingNotSet: PRODUCT.PRODUCT_TYPES
Traceback (most recent call last):
File "./manage.py", line 27, in
execute_manager(settings)
.
.
.
.
raise SettingNotSet('%s config group does not exist' % group)
livesettings.models.SettingNotSet

SOLUTION: After apply a grep in the Satchmo code for “PRODUCT_TYPES” I figured out my mistake: My package was named product: the same name of the Satchmo product application. So it was overriding the framework application.

Thank god Satchmo is a Open Source project!

Download psycopg2 at http://initd.org/pub/software/psycopg/

To install  (I’m using the version 2.0.10) you need to run two commands:

# python setup.py build

and after the build is complete:

# python setup.py install

Well, but it’s not so easy if you do not have the dependencies already installed.In my case I got errors like:

NameError: global name ‘w’ is not defined psycopg2

or

error: Setup script exited with error: command ‘gcc’ failed with exit status 1

To solve this I have to install the libpq-dev library and the python development package:

# apt-get install libpq-dev

# apt-get install python2.5-dev

After that I just run the usual python setup.py install and all done.

Esse problema na realidade não é do Django, e sim do módulo psycopg2 que faz acesso ao PostgreSQL (No meu caso eu estava utilizando a versão 2.0.6 deste módulo). Para resolver este problema é preciso atualizá-lo para o 2.0.7.

Leia Mais »

Um recurso bacana da linguagem Python que facilita a vida um bocado e deixa o código mais legível são os Decorators. O seu uso é bem simples e sua implementação também.

Para mais informações dê uma olhada nesta página (foi a melhor referência que eu encontrei):

http://www.python.org/dev/peps/pep-0318/

Um exemplo:

import time
def imprime_tempo(func):
    def decorator(*arg, **kwargs):
        t1 = time.time()
        result = func(*arg, **kwargs)
        t2 = time.time()
        print '%s levou %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
        return result
    return decorator
@imprime_tempo
def minha_funcao(x):
    print 'testando'
    x = x**2

Criei este exemplo com base no que está em:

http://www.daniweb.com/code/snippet368.html

Outro link:

http://www.phyast.pitt.edu/~micheles/python/documentation.html

Neste site são apresentados alguns problemas no uso de Decorators (basicamente a alteração da assinatura da função) e uma solução para o problema

Diagnóstico: No admin do Django é exibida a mensagem “Please correct the errors below.” mas nenhum campo possui erro. Este problema acontece quando temos relacionamentos inline.

Forma de resolver: Aplicar o patch que está em http://code.djangoproject.com/attachment/ticket/9076/inline_queryset.diff

Para criar um novo projeto em Django execute o comando:

django-admin startproject <nome projecto>

com isso será criada a seguinte estrutura:

mysite/
    __init__.py
    manage.py
    settings.py
    urls.py

continua…