Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling required external modules with cython

I'm building a standalone executable using Cython on Linux.

I have the following code:

import psycopg2 as pg

conn = pg.connect('dbname=**** user=**** password=****')
cur = conn.cursor()
cur.execute('SELECT version()')
print(cur.fetchone())

The problem is when the machine does not have the Python package psycopg2 installed, throws the following exception:

Traceback (most recent call last):
File "test.py", line 2, in init test (test.c:872)
    import psycopg2 as pg
ImportError: No module named 'psycopg2'

Im building using the --embed cython flag.

How can I make Cython to compile that particular package too?

like image 801
Simón Oroño Avatar asked Mar 21 '14 18:03

Simón Oroño


2 Answers

Nuitka is the tool that you need.

You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.

Right now Nuitka is a good replacement for the Python interpreter and compiles every construct that CPython 2.6, 2.7, 3.2, 3.3 and 3.4 offer. It translates the Python into a C++ program that then uses "libpython" to execute in the same way as CPython does, in a very compatible way.

It is somewhat faster than CPython already, but currently it doesn't make all the optimizations possible, but a 258% factor on pystone is a good start (number is from version 0.3.11).

like image 176
Vlad Frolov Avatar answered Oct 21 '22 12:10

Vlad Frolov


--embed means the Python interpreter is embedded in your executable. It does not mean independence from Python. It does not do what you think. It sounds more like you need a tool like py2exe, py2app or pyfreeze.

like image 22
Sturla Molden Avatar answered Oct 21 '22 12:10

Sturla Molden