Friday, 20 November 2015

Python importing compiled .pyc files in to the __main__ namespace

I had written a rather long piece of code in Python, which was all in one file, and all using the same namespace. I later needed to split the file and was hoping to find a method that worked by allowing me to "include" the file in a list, just like the code was in one large file.

Using import gave each module its own namespace, and so they weren't able to access the other imported modules without first importing the module again. Similarly, using any combination of from [name] import * had the same problem.

What ended up being the solution was the imp library.

The code that I have used is as follows:

import imp
imp.load_module(“__main__”, filename)

By passing "__main__", this loads the module into the workspace __main__, which is the namespace my code was using. I only used this to load .pyc files. The filename doesn't have to be a full path, and can just be the file name if located in the same folder as the script, assuming you haven't changed directories.

In hindsight, the problems were mostly from poor code structure, and each module should run as a standalone module, but due to time constraints I didn't have time to fix these issues.

No comments:

Post a Comment