Command Line Interface

This file shows some use case of Pythran on the command line.

Firstly lets clear the working space:

$> rm -f cli_*

One of the most classic use case in Pythran is to generate a native .so module:

$> printf '#pythran export foo()\n#pythran export msg\nmsg = \"hello world\"\ndef foo(): print(msg)' >
$> pythran -o
$> ls

The generated native .so module can then be used with the Python interpreter:

$> python -c 'import cli_foo ;'
hello world
$> python -c 'import cli_foo ; print(cli_foo.msg)'
hello world

Pythran version can be dumped through --version:

$> pythran --version 2>&1

The module-level __pythran__ variable indicates that the module loaded has been pythranized:

$> python -c 'import cli_foo ; print(hasattr(cli_foo, \"__pythran__\"))'

You can choose your optimization level by using -O flag:

$> rm
$> pythran -O2 -o
$> ls

Out of curiosity, you can check the generated output:

$> pythran -E

That’s some heavily templated code ;-) Pythran can then compile it for you to a Python module:

$> pythran cli_foo.cpp -o

Pythran can also generate raw C++ code, using the -e switch:

$> printf 'msg = \"hello world\"\ndef foo(): print(msg)' >
$> pythran -e -o cli_foo.hpp
$> printf '#include \"cli_foo.hpp\"\nusing namespace __pythran_cli_foo ; int main() { foo()(); return 0 ; }' > cli_foo.cpp
$> `pythran-config --compiler --cflags` -std=c++11 cli_foo.cpp -o cli_foo
$> ./cli_foo
hello world

You can use -p option to apply a Pythran optimization. For example, the python code can propagate constants using the Pythran ConstantFolding optimization:

$> pythran -e -p pythran.optimizations.ConstantFolding

If you want to specify the path of generated file:

$> pythran -o /tmp/ -DNDEBUG
$> ls /tmp/

To know more options about Pythran, you can check pythran --help :-)