How do I protect my Python codebase so that guests can't see certain modules but so it still works?

后端 未结 3 1473
情深已故
情深已故 2021-01-03 09:09

We\'re starting a new project in Python with a few proprietary algorithms and sensitive bits of logic that we\'d like to keep private. We also will have a few outsiders (sel

3条回答
  •  心在旅途
    2021-01-03 09:49

    In the __init__ method of the foo package you can change __path__ to make it look for its modules in other directories.

    So create a directory called secret and put it in your private Subversion repository. In secret put your proprietary bar.py. In the __init__.py of the public foo package put in something like:

    __path__.insert(0,'secret')
    

    This will mean for users who have the private repository and so the secret directory they will get the proprietary bar.py as foo.bar as secret is the first directory in the search path. For other users, Python won't find secret and will look as the next directory in __path__ and so will load the normal bar.py from foo.

    So it will look something like this:

       private
        └── trunk/
            └── secret/
                └── bar.py
        public
        └── trunk/
            ├── __init__.py
            └── foo/
                ├── __init__.py
                ├── bar.py
                ├── baz.py
                └── quux.py
    

提交回复
热议问题