Is python convert to machine code?

This doesn't compile Python to machine code. But allows to create a shared library to call Python code.

If what you are looking for is an easy way to run Python code from C without relying on execp stuff. You could generate a shared library from python code wrapped with a few calls to Python embedding API. Well the application is a shared library, an .so that you can use in many other libraries/applications.

Here is a simple example which create a shared library, that you can link with a C program. The shared library executes Python code.

The python file that will be executed is pythoncalledfromc.py:

# -*- encoding:utf-8 -*-
# this file must be named "pythoncalledfrom.py"

def main[string]:  # args must a string
    print "python is called from c"
    print "string sent by «c» code is:"
    print string
    print "end of «c» code input"
    return 0xc0c4  # return something

You can try it with python2 -c "import pythoncalledfromc; pythoncalledfromc.main['HELLO']. It will output:

python is called from c
string sent by «c» code is:
HELLO
end of «c» code input

The shared library will be defined by the following by callpython.h:

#ifndef CALL_PYTHON
#define CALL_PYTHON

void callpython_init[void];
int callpython[char ** arguments];
void callpython_finalize[void];

#endif

The associated callpython.c is:

// gcc `python2.7-config --ldflags` `python2.7-config --cflags` callpython.c -lpython2.7 -shared -fPIC -o callpython.so

#include 
#include 
#include 
#include 

#include "callpython.h"

#define PYTHON_EXEC_STRING_LENGTH 52
#define PYTHON_EXEC_STRING "import pythoncalledfromc; pythoncalledfromc.main[\"%s\"]"


void callpython_init[void] {
     Py_Initialize[];
}

int callpython[char ** arguments] {
  int arguments_string_size = [int] strlen[*arguments];
  char * python_script_to_execute = malloc[arguments_string_size + PYTHON_EXEC_STRING_LENGTH];
  PyObject *__main__, *locals;
  PyObject * result = NULL;

  if [python_script_to_execute == NULL]
    return -1;

  __main__ = PyImport_AddModule["__main__"];
  if [__main__ == NULL]
    return -1;

  locals = PyModule_GetDict[__main__];

  sprintf[python_script_to_execute, PYTHON_EXEC_STRING, *arguments];
  result = PyRun_String[python_script_to_execute, Py_file_input, locals, locals];
  if[result == NULL]
    return -1;
  return 0;
}

void callpython_finalize[void] {
  Py_Finalize[];
}

You can compile it with the following command:

gcc `python2.7-config --ldflags` `python2.7-config --cflags` callpython.c -lpython2.7 -shared -fPIC -o callpython.so

Create a file named callpythonfromc.c that contains the following:

#include "callpython.h"

int main[void] {
  char * example = "HELLO";
  callpython_init[];
  callpython[&example];
  callpython_finalize[];
  return 0;
}

Compile it and run:

gcc callpythonfromc.c callpython.so -o callpythonfromc
PYTHONPATH=`pwd` LD_LIBRARY_PATH=`pwd` ./callpythonfromc

This is a very basic example. It can work, but depending on the library it might be still difficult to serialize C data structures to Python and from Python to C. Things can be automated somewhat...

Nuitka might be helpful.

Also there is numba but they both don't aim to do what you want exactly. Generating a C header from Python code is possible, but only if you specify the how to convert the Python types to C types or can infer that information. See python astroid for a Python ast analyzer.

As I understand, the cause of the speed difference between compiled languages and python is, that the first compiles code all way to the native machine's code, whereas python compiles to python bytecode, to be interpreted by the PVM. I see that this way python codes can be used on multiple operation system [at least in most cases], however I do not understand, why is not there an additional [and optional] compiler for python, which compiles the same way as traditional compilers. This would leave to the programmer to chose, which is more important to them; multiplatform executability or performance on native machine. In general; why are not there any languages which could be behave both as compiled and interpreted?

Kilian Foth

103k44 gold badges285 silver badges303 bronze badges

asked Jun 7, 2014 at 8:51

4

No. The reason why there are speed differences between languages like Python and C++ is because statically-typed languages give the compiler tons of information about the structure of the program and its data which allows it to optimize both computations and memory access. Because C++ knows that variable is of type int, it can determine the optimal way to manipulate that variable even before the program is run. In Python on the other hand, the runtime doesn't know what value is in a variable right until the line is reached by the interpreter. This is extremely important for structures, where in C++, the compiler can easily tell the size of the structure and every location of its fields within memory during compilation. This gives it huge power in predicting how the data might be used and lets it optimize according to those predictions. No such thing is possible for languages like Python.

To effectively compile languages like Python you would need to:

  1. Ensure that the structure of data is static during the execution of the program. This is problematic because Python has eval and metaclasses. Both which make it possible to change the structure of the program based on the input of the program. This is one of the things that give Python such expressive power.
  2. Infer the types of all variables, structures and classes from the source code itself. While it is possible to some degree, the static type system and algorithm would be so complex it would be almost impossible to implement in a usable way. You could do it for a subset of the language, but definitely not for the whole set of language features.

answered Jun 7, 2014 at 11:03

EuphoricEuphoric

35.6k6 gold badges74 silver badges104 bronze badges

6

Two concepts might help us understand better why Python compiled to native machine code "may" not run as fast as compiled C or other commonly compiled languages. They are called early binding and late binding.

I should begin by saying I am not a Python expert, and I came to this site by accident. But I like this site.

As mentioned in another response here, the C++ compiler can know a lot about the program and make decisions about which operations to use for specific data structures. As an example if two integer variables need to be added together, the compiler knows they are native integers, 32 bits wide for example and it can add them together with one "ADD" instruction. So it compiles the ADD instruction into the code. It's locked in and can't be changed while the program is running. That is early binding.

On the other hand in a language like Python we could expect the program to throw different types of data together in complex ways. Now the compiler does not know if our 2 variables are integers, floats, strings or lists. So it has to compile code that determines that information at run time and select the correct operation while the program is running. This is late binding and we can understand that there will be a performance hit for doing that extra work while the program is running. It is the price you pay for keeping those options open in a language like Python but it provides maximum run-time flexibility.

answered Feb 6, 2016 at 13:49

1

I think it has more to do with the Python specifics itself, the same reason you can't compile C# to machine code. Language specifics would actually render your programs buggy even if it were possible due to the nature of the language. Why not just learn the C language? Its much easier than C++ and slightly advanced than Python but still approachable.

answered Nov 11, 2015 at 20:18

1

What is machine code in Python?

Machine code is a strictly numerical language which is designed to run as fast as possible, and may be considered as the lowest-level representation of a compiled or assembled computer program or as a primitive and hardware-dependent programming language.

Does Python use machine language?

Python includes a modular machine learning library known as PyBrain, which provides easy-to-use algorithms for use in machine learning tasks. The best and most reliable coding solutions require a proper structure and tested environment, which is available in the Python frameworks and libraries.

Does Python get converted to C?

Python code can make calls directly into C modules. Those C modules can be either generic C libraries or libraries built specifically to work with Python. Cython generates the second kind of module: C libraries that talk to Python's internals, and that can be bundled with existing Python code.

Is Python a compiler or interpreter?

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine.

Chủ Đề