Running a Python file as main, take 2

Saturday 9 May 2009

Recently, I tried to simulate the way Python runs programs, but I was worried that there was a gotcha. It turns out there is one: when imp.load_module loads a Python source file, it compiles it to bytecode, and saves it as a file.

Python uses a very simplistic way of naming the compiled file: it adds a ‘c’ to the end of the source file name. This works well when the file is “foo.py” and the compiled file is “foo.pyc”. But if you try to run a script in this way, the file may not have a .py extension. So for example, if the python file is named “zzz”, then load_module will create a compiled file named “zzzc”. This is bad.

There was no reasonable way to prevent this compiled file creation, so I went back to the execfile technique, instead setting up more of the proper sys structures to get the proper execution context. If you’re interested in the details, the code is in execfile.py, with tests in test_execfile.py.

BTW: This was basically Fredrik’s advice from the first post, and he was right. Marius suggested I look at runpy in the standard library, and it’s similar to what I’m trying to do here, but it takes a module name rather than a file, so it isn’t quite right.

Comments

[gravatar]
Larry Hastings 4:39 PM on 10 May 2009

Well, to be precise,, Python adds a "c" by default, but it adds an "o" if you are running with optimization turned on.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.