1

In Python ( CPython) we can import module: import module and module can be just *.py file ( with a python code) or module can be a module written in C/C++ ( be extending python). So, a such module is just compiled object file ( like *.so/*.o on the Unix).

I would like to know how is it executed by the interpreter exactly.

I think that python module is compiled to a bytecode and then it will be interpreted. In the case of C/C++ module functions from a such module are just executed. So, jump to the address and start execution.

Please correct me if I am wrong/ Please say more.

2
  • I'm curious if there's any practical reason you'd like to know the underlying mechanics? Commented Nov 5, 2016 at 20:11
  • I am convinced that knowledge about internals facilitates being a better programmer, what do you think? Commented Nov 5, 2016 at 21:13

1 Answer 1

4

When you import a C extension, python uses the platform's shared library loader to load the library and then, as you say, jumps to a function in the library. But you can't load just any library or jump to any function this way. It only works for libs specifically implemented to support python and to functions that are exported by the library as a python object. The lib must understand python objects and use those objects to communicate.

Alternately, instead of importing, you can use a foreign-function library like ctypes to load the library and convert data to the C view of data to make calls.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.