Prevent users from viewing source code of an imported module

I want to provide jupyterhub users with some proprietary modules in jupyter notebooks. However, one can use import inspect to view the source code. How can I prevent that?

I have seen in Quantopian notebooks that import inspect is forbidden. How is blocking an import statement like this implemented?

Also, what’s the best way to provide the modules without having an explicit directory in the notebook server of a user? I’d imagine something to do with adding source code to the docker image. I’d appreciate if someone could point me in the right direction.

What you’re looking for is called “obfuscation”.

PyArmor only mentions scripts, not modules: https://pypi.org/project/pyarmor/

PyMinifier also talks about executables: https://liftoff.github.io/pyminifier/

There’s an SO question that goes into your direction:

But you should be aware that there’s no way to stop a determined person from decompiling your Python code, no matter how much you obfuscate it:

Thanks, @rolweber. After going through these links, I better understand how code protection works. I don’t think code obfuscation is worth doing for my case.

I understand that the source code of the modules and/or models I provide in a jupyterhub notebook can be accessed. I just wanted to limit it’s exposure to the APIs.

1 Like