Skip to content
Justine Tunney edited this page Sep 5, 2023 · 10 revisions

The third_party/python folder in the repo contains a copy of the CPython 3.6.14 source code, with several patches to improve performance and make use of all the goodies offered by Cosmopolitan Libc, which include: loading pure-python packages from within the executable, backports from Python 3.7, tab-completions in the REPL (read-eval-print-loop) via bestline, size optimization tricks and more!

Screenshot from 2023-09-04 21-12-11

How to build python.com?

git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
make o//third_party/python/python.com   # this will build python.com only
make o//third_party/python              # this will run CPython stdlib tests as well
./o/third_party/python/python.com

As part of the repo, python.com can be built as a Makefile target. The configuration and build requirements for python.com are specified in o//third_party/python/python.mk -- no need to run ./configure.

Features

  • The files comprising the CPython 3.6.14 standard library are stored within the executable itself, which you can view by:

    unzip -vl python.com 

    to add your own files to python.com, just:

    mkdir ./.python
    cp /your/package.py ./.python
    zip -r ./python.com ./.python

    now you can import your own package within python.com.

  • .args support: if you wanted python.com to run a specific script on startup instead of opening the REPL, you can create a file called .args and add it to python.com. This is very useful if you want to rename python.com into your own executable and perform only some specific behavior. See the example below:

Screenshot from 2023-09-04 21-46-02

The .args file should have one argument per line. These arguments are inserted before other CLI arguments. The ... indicates that arguments from the command line should be accepted.

  • python.com has been modified to use bestline, enhancing the REPL experience across operating systems:

Screenshot from 2023-09-04 21-19-52

see the copy typed, and the right being suggested in the background? That's bestline in action. It helps a ton when typing in the REPL.

  • python.com provides a special module called cosmo, which provides access to some low-level utilities from the Cosmopolitan Libc library. For example, you could:
import cosmo 

# view number of CPU cycles
# since the program started
print(cosmo.rdtsc() - cosmo.kStartTsc)

def foo(a, b):
  return a+b

# trace C function calls in a block
# (needs python.com.dbg, output logged to stderr)
with cosmo.ftrace() as f:
    foo(2,3)

References

Reading through some of the issues/PRs related to the development of python.com in the Cosmopolitan Libc repo would help understand how python.com came to be:

Clone this wiki locally