âClosed sourceâ is a licensing term, not a human readability term . Technically, if I declare this code block:
print('Hello, lawsuits!')
to be released under a proprietary license, you would not be allowed to use that code without the risk of being sued for copyright infringementš. If you want your code to be closed source, you just need to license it that way.
If you just donât want people to be able to read your Python code, thatâs an entirely different matter and not something that I support, so Iâll leave it to others to answer. But itâs not whether someone can read the code that makes it open source or not, itâs whether theyâre allowed to use, modify, or redistribute the code (with or without changes), so making it unreadable just makes things harder for everybody involved and can be circumvented in various ways anyway.
šI am not a lawyer and donât even pretend to actually understand any of this from a legal perspective, this is all just my basic laymanâs understanding. If you want anything remotely resembling legal advice, talk to a real lawyer
Hi Zachary,
Thank you for your reply.
close source is not license thing nor lagal thing.
I would like to relase some files in source code but some other files in binary or unreadable format.
Why do you want to release them in a binary format?
The only way to make files unreadable is to not release them.
You can try this:
write your file program.py
run python3 -m compileall -b program.py
ship program.pyc
But donât expect that many Python users will be interested, they will
probably be very suspicious of any Python code without source code. And
also donât expect that this will keep your code secret. There are plenty
of tools that can read .pyc files and disassemble them into human
readable byte-code.
Another alternative is to put your code in a zip file, that will protect
it from prying eyes and casual users.
More generally though, âhow to prevent someone Iâve given software
to from looking at it/disassembling itâ isnât a Python-specific
concern. The problem is going to be the same across pretty much all
programming languages. And as others have pointed out, this has
absolutely nothing to do with whether software is âopen sourceâ or
not, thatâs a copyright licensing concept.
What you want to do is basically not possible. For a computer to run
a piece of software it needs to be able to operate on the
instructions in it. If someone controls the computer on which that
software runs, they know what instructions the software is asking
that computer to perform. The most you can hope to do is design the
software to operate on encrypted inputs and outputs so that the
system running it cannot extract a usable copy of the data itâs
being asked to operate on (which is why I mentioned homomorphic
encryption in my prior reply).
Then the pyc (compiled Python) files mentioned earlier are what
youâre talking about. You can disassemble dll/so files too, after
all. Just be aware theyâre going to be specific to the environment
where theyâre compiled to a great extent, so users may not be able
to run them with different interpreter versions, different library
versions, different processor architectures, and so on. Normally
those files are just-in-time (re)compiled based on what the
interpreter finds in the local system, so theyâre not particularly
portable.
Still, I would recommend thinking long and hard about what your
reasons are, and what your risk model is. This just seems like a lot
of unnecessary pain to put yourself and your users through for no
appreciable gain.
Earlier, I asked why you want to do this. You didnât answer.
The best solution for your problem will depend on what the problem you
are trying to solve actually is. Why donât you want to release the
source code?
The answer to that question may suggest a good solution.
However, like @zware and @fungi, Iâd not recommend using those just to obscure your code. They entail pain to compile for every platform and figure out how to distribute the executables.
Many Thanks, Steven and Jean!
Let me explain what I want to do clearly:
I create many python files. some Python files include very import technique, so I want to make those import technique files be close source. however, I would like to make other not important technique file open source.
I have tried Cython to convert my python file to pyd file or so file on Linux. Cython is very good. but I would like to know whether there are some other good solutions beside Cython.
I create many python files. some Python files include very import
technique, so I want to make those import technique files be close
source. however, I would like to make other not important
technique file open source.
[âŚ]
I hate to continue picking apart your use case, but bear in mind
that obfuscating your code by redistributing only byte-compiled
files may be protecting it from accidentally being seen by people
who donât really care whatâs in there anyway, while still doing very
little to actually prevent anyone who wants to know what itâs doing
from running it under a debugger, decompiling or disassembling it,
et cetera.
There is a very good reason why companies with lots of money at
their disposal still mostly rely on intellectual property law to
secure proprietary applications they distribute rather than doing
what youâre trying. They declare their copyright with no license
granted to modify or redistribute (this is how copyright works by
default), and may also require recipients to agree to further
conditions beyond what copyright law protects (via an EULA and/or
NDA, though these frequently arenât upheld by courts). Some may also
patent particular techniques or uses in jurisdictions where thatâs
possible.
But ultimately, your choice is between distributing your software to
others or keeping how your software works a closely-guarded
secret. Thereâs really no middle ground. Youâre essentially talking
about giving someone a machine draped with a sheer piece of cloth,
and expecting them not to peek at whatâs under the cover.
for pure Python code the term âobfuscatingâ might help you.
An âobfuscatorâ is a script, which exchanges human readable variable names and so on with human unreadable ones, if you e.g. use a âloopâ variable, this can be obfuscated by using cryptic concatenations of e.g. âIâ, âlâ, "1â, 'O and '0âs to e.g. âIOll10I1â - which is quite hard to distinguish from similar concatenations for humans (and furthermore, does not have any meaning alike âloopâ)âŚ
Just google âobfuscate Pythonâ and youâll get a lot of hitsâŚ
The simplest way to do that is port your important-techniques code to a compiled language. As others have said, it wonât stop a determined/knowledgeable person from discovering your secrets, but itâll stop most people. Also, if you simply donât talk about those secret techniques when you release your projectâwith part of it in a compiled languageâvery few people will even think about trying to uncover those secrets. Itâs knowing that someone has secrets that makes most people curious.