0

This is not a language compare question like asked on various forums. I am interested to know about more specific term core libraries/modules calling/execution in python.

As I checked python modules installation directory like /usr/lib/python2.7 (On Ubuntu). I found .py (Source Code) and .pyc (Byte Code). I am assuming Python interpreter/compiler call .pyc file when we using import statement or more specifically called class/function from that module.

While php is using .so (Shared object) files for libraries. As I seen on /usr/lib/php5/20090626. Yes python also have a directory /usr/lib/pyshared/python2.7 for .so files. But still lot of important libraries are stored as .pyc files.

Is it not a good idea for using only .so extension for core libraries like php for performance benefits ?

kuldeep.kamboj
  • 2,462
  • 3
  • 21
  • 54
  • I am thinking If python like to behave alike JAVA World where jvm read .class/.jar files instead of compiled versions, But I am unsure if java have core modules are not native compiled. – kuldeep.kamboj Mar 19 '14 at 11:43

1 Answers1

1

.py files are compiles on the fly to .pyc files, the .pyc is used if it is more recent than the .py file.
Some modules can be written in C/C++, then they are delivered as a .so file.

Nicolas Defranoux
  • 2,588
  • 1
  • 8
  • 11
  • I know about pyc files. My point is that still python doing byte-code read even for lot of core modules. For 3rd party module / applications, it is understandable But for core modules, what is the issue if used .so compiled version. – kuldeep.kamboj Mar 19 '14 at 11:41
  • @kuldeep.kamboj CPython is an interpreter. It does not produce native code. .so files are native code; the ones that come with Python were written in C. – Janne Karila Mar 19 '14 at 12:12
  • @JanneKarila In directory /usr/lib/python2.7/ I found popen2.pyc, socket.pyc, urllib2.pyc and lot more. If they are not core libraries which comes with python default installation. Still my question is same cpython can generate pyc for user's applications and that is understandable, but for core modules like popen, urllib2, socket native compiled version can be distributed with python default install. In php, we even do not have byte compiled files similar to pyc, So php intepreter read/parse php files. But for core libraries there are no .php or byte-compiled version but .so version used. – kuldeep.kamboj Mar 19 '14 at 12:31
  • 1
    @kuldeep.kamboj You are essentially asking why aren't more standard library modules written in C. That's a viable topic for discussion but Stack Overflow is not really a place for that. Note, however, that eg. `socket` is only partially written in Python. You should be able to find a `_socket.so` file on your system somewhere. – Janne Karila Mar 19 '14 at 17:26
  • @Janne Karila. As I understand it the question is why don't we compile python standard libraries to native code, so that rather than running the bytecode we run native code. The answer is because there is no way to compile python code to native code, see http://stackoverflow.com/questions/8786203/compiling-python-to-native-code – Nicolas Defranoux Mar 20 '14 at 11:34
  • It may be python design which not allow or make it complex to using native coded libraries instead of byte coded. Perhaps due to on fly byte coded files .pyc generation. As php do not have on-fly byte compilation So they look for native-compiled version .so . – kuldeep.kamboj Mar 21 '14 at 12:51