Create pypy process

344 views Asked by At

I would like to create a process which runs PyPy. I tried the following and it works:

import os
os.chdir('<path-to-pypy-download>/bin/')
os.execl('pypy', 'pypy', '-c', 'print "Hi!"')

However, when I remove the chdir as:

import os
os.execl('<path-to-pypy-download>/bin/pypy', 'pypy', '-c', 'print "Hi!"')

I get:

debug: WARNING: Library path not found, using compiled-in sys.path.
debug: WARNING: 'sys.prefix' will not be set.
debug: WARNING: Make sure the pypy binary is kept inside its tree of files.
debug: WARNING: It is ok to create a symlink to it from somewhere else.
debug: OperationError:
debug:  operror-type: ImportError
debug:  operror-value: No module named os

Please, does anyone know how to spawn a PyPy process without changing the working directory?

1

There are 1 answers

0
abarnert On BEST ANSWER

This may not be correct (in which case I'll delete it), but I'm pretty sure that all you need to do is:

os.execl('<path-to-pypy-download>/bin/pypy', 
         '<path-to-pypy-download>/bin/pypy', '-c', 'print "Hi!"')

In other words, pass the full path for arg0 as well as path.

Why? Well, when PyPy starts up, it's got to be using (the RPython/compiled-to-C equivalent of) sys.argv[0] to find the path to a custom stdlib. What else could it use? Which is of course just copied from the char *argv[] argument passed to the interpreter's main function. When you let the OS launch a program for you, it will put the full path in there. But when you explicitly set it with execl, it will just copy whatever you gave it.

It's actually a bit more complicated than this—it does a readlink (to allow symlinks) and an abspath (to allow itself to be run by relative path—as in your first example). But the basic idea is the same.

As a side note, you might want to consider using a fully-installed PyPy instead of a run-out-of-build-tree PyPy, in which case its sys.prefix will be set up properly so it won't need this.