pythonpsycopg2py2exe

Error when trying to catch exception on psycopg2 (only with py2exe)


I'm having some problems with this code:

...
query = f"INSERT INTO nichos VALUES {parameters}"
try:
    run_query(query)
    print()
    print("Nicho agregado exitosamente.")
    print()
    if ret == 1:
        return cod_nicho
except sql.errors.UniqueViolation:
    print()
    print("         ERROR. El nicho ya se encuentra cargado en el sistema. No se realizaron cambios en el registro.")
    if ret == 0:
        return
    elif ret == 1:
        return cod_nicho
except:
    log_error()
    print("")
    input("         ERROR. Comuníquese con el administrador...  Presione enter para continuar...")
    if ret == 0:
        return
    elif ret == 1:
        return cod_nicho
...

The thing is, when I execute it from Windows console ($ py script.py) it works perfectly, if you try to insert an existing value on database it execute the except block.

However, if I do the same from the .exe created with py2exe it gives me the following error:

Traceback (most recent call last):
  File "funciones_mantenimiento.pyc", line 1459, in alta_nicho
  File "funciones_mantenimiento.pyc", line 160, in run_query
psycopg2.errors.UniqueViolation: llave duplicada viola restricción de unicidad «sqlite_autoindex_nichos_1»
DETAIL:  Ya existe la llave (codigo)=(0202010036).


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ventas.py", line 42, in <module>
  File "funciones_ventas.pyc", line 383, in menu
  File "funciones_ventas.pyc", line 1628, in menu_editar_op
  File "funciones_ventas.pyc", line 1823, in cambiar_nicho
  File "funciones_mantenimiento.pyc", line 1465, in alta_nicho
AttributeError: module 'psycopg2' has no attribute 'errors'

Somebody knows how can I solve this?

Thanks!


Solution

  • This means that py2exe isn't correctly detecting that psycopg2.errors is a dependency of your program.

    To make that easier for it to determine, add the following line:

    import psycopg2.errors