Obfuscating Python code

Thomas 'PointedEars' Lahn PointedEars at web.de
Tue Mar 15 14:59:33 EDT 2016


Ben Finney wrote:

> Swanand Pashankar <swanand.pashankar at gmail.com> writes:
>> Embedding a Python file in C code works, but it exposes your Python
>> script. Didn't find any free fool-proof way to obfuscate Python code
>> either.
> 
> What exactly is it you want to prevent? Why do you think obfuscating the
> code will achieve that?

On a more constructive note, python(1) (CPython) creates a binary (byte-
code) “.pyc” file from “.py” files when it runs them.  ISTM that you can 
then run the “.pyc” file as if it were the “.py” file (if the “.pyc” file is 
given the executable flag, you can even execute it as a standalone command, 
but that might only work on my system).  So apparently you do not have to 
distribute the source code of a program written in Python if you do not want 
to.

If you want to distribute the “.pyc” file (perhaps under another name), then 
the “-O” and “-OO” optimization switches for python(1) could come in handy 
(see “python --help”).  [It is then perhaps not a coincidence that “-O” is 
documented to change the filename suffix from “.pyc” to “.pyo”; cf. “.so”]

(The Python manual should have more on this, I have not checked.)

That said, not distributing the source code of a program as well (or at 
least making it available to users in some way) strikes me as unpythonic 
since Python is at least Open Source software, and Python 2.0.1, 2.1.1 and 
newer are GPL-compatible Free Software.

<https://docs.python.org/3.5/license.html>
<http://www.gnu.org/licenses/license-list.html#Python>

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list