Search code examples
pythongoogle-colaboratory

How to share Google colab with private code


I am wanting to share a Google colab notebook, but needs to hide part of the code. Is it possible to put the private code in a separate .py or Colab notebook, and import it, so that those who have access to the public Colab notebook cannot see the private .py or Colab notebook?

Many thanks in advance.


Solution

  • Not really; your ability to hide code is only as good as your users' inability to inspect it:

    import some_file # your private .py
    
    from inspect import getsource
    print(getsource(some_file))
    

    The bottom line will print the file's entire source code - no matter how it was imported.

    If you don't expect your users to know or figure this (not a safe bet), then yes, you could run an import via a self-authenticating request to a storage (e.g. Drive, Google Cloud Bucket) that the user cannot access, but that'll involve lots of steps (of which I wouldn't know).

    See this SO for more info on hiding code, and some specific methods for making inspection harder.


    Update: a workaround is to share in read-only mode, where cells can be executed but not edited - see this SO for several methods. Note that this still doesn't make your code fully private, but some of the methods in the link can be very difficult to circumvent (require hacking) - effectively, success.