Skip to content
Advertisement

Using a compiled Python shell in Linux

Is it possible to use a “more complex” shell than just a single command shell? We have written a python shell that is a command loop, and it works fine in /etc/passwd like this:

user:x:1000:1000::/home/user:/usr/bin/ourshell.py

Of course the Python file has the shebang line for /usr/bin/python in it. However, we’d like to compile the Python shell into a .pyc file to save a bit of time on execution in login. So, after compiling, I’ve been trying to “quote” the shell line in /etc/passwd as “python ourshell.pyc”, and I even tried making the shell a bash script which simply executes that same command (with the initial arguments).

Of course none of this has worked. When we SSH in, there is always some kind of error. Is there any special trick to what I am trying to do?

Advertisement

Answer

CPython’s .pyc files are not text, and do not allow use of a shebang line. The traditional method is to have your called script be tiny; it would simply import a module with the rest of the program, which can then be precompiled. For instance, here is the main script of xonsh:

#!/usr/bin/env python3 -u
from xonsh.main import main
main()

This script takes negligible time to compile. It is also possible to run installed modules using -m, but that takes module names, not filenames, so is not suitable for a shebang script.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement