Distributing closed source modules

Dave Brueck dave at pythonapocrypha.com
Thu Mar 24 15:22:22 EST 2005


Jiri Barton wrote:
> I'd like to be able to distribute some python modules of my system (plugins)
> without the source. So far, I have done this by including only the *.pyc
> files. However, I have recently found they are platform dependent and
> python version dependent.
> 
> This approach has been very convenient because I don't have to mess up with
> __import__ and the like - which seem to be kind of a pain when inter-module
> dependencies are introduced.
> 
> Can some one point me in another direction of protecting the code? I know
> and this whole thing just does not sound right to me either but I am forced
> to do so.

Protecting code in any language is pretty tough and/or futile, but you can 
Google the archives if you're interested in reading more on that.

Anyway, you can create a module on the fly like this (untested):

import new, sys
name = 'MyModule'
m = sys.modules[name] = new.module(name)
exec codeStr in m.__dict__

where codeStr is a string that contains the source code of your module (e.g. 
from file('somemodule.py').read() ).

You can combine the above with whatever mechanism you come up with for 
distributing the code itself. You could store it in an encrypted archive file, 
you could download it on the fly from a remote server over a secure connection, etc.

-Dave



More information about the Python-list mailing list