# How to secure python script?

**URL:** <https://forums.unrealengine.com/t/how-to-secure-python-script/461883>\
**Category:** Editor Scripting\
**Tags:** Python, security, unreal-engine, question\
**Created:** [February 28, 2020, 8:13am UTC](https://forums.unrealengine.com/t/how-to-secure-python-script/461883 "2020-02-28T08:13:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_308f0a98](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_308f0a98](https://forums.unrealengine.com/u/anonymous_user_308f0a98)\
**Post date:** [February 28, 2020, 8:13am UTC](https://forums.unrealengine.com/t/how-to-secure-python-script/461883/1 "2020-02-28T08:13:37Z")

</div>

I have been using UnrealEnginePython plugin for using python in runtime.

I have some python scripts from which i made pyc files for securing my code. But pyc is not recompiled easily.

My question is how python code keeps security in professional project?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b58cf9e8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b58cf9e8](https://forums.unrealengine.com/u/anonymous_user_b58cf9e8)\
**Post date:** [February 28, 2020, 7:43pm UTC](https://forums.unrealengine.com/t/how-to-secure-python-script/461883/2 "2020-02-28T19:43:37Z")

</div>

I’m not sure how UnrealEnginePython plugin packages python scripts, but note that pyc files can be decompibled pretty trivially revealing semi-human readable code. One thing you can do to apply some simple obfuscation to your code is to compile with the `-OO` option which will apply basic optimizations as well as stripping comments and docstrings from your compiled files.

`python -OO py_compile <your_program.py>`

However this only applies extremely basic obfuscation and will not prevent your code from being decompiled. There are other techniques for obfuscating python code encoding as base64 or using a utility like [pyarmor](https://pypi.org/project/pyarmor/) to obfuscate the bytcode.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_308f0a98](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_308f0a98](https://forums.unrealengine.com/u/anonymous_user_308f0a98)\
**Post date:** [March 5, 2020, 9:54am UTC](https://forums.unrealengine.com/t/how-to-secure-python-script/461883/3 "2020-03-05T09:54:25Z")

</div>

thank you so much and sorry for the late response. Seems like that plugin could only work on pyc files but pyc can easily be decompiled. By using cython, i created pyd file of python module. I searched a bit. So far pyd file is very very hard to decompile or not possible (not sure, but it is the safest i could find). Since plugin could not work on pyd, i made a normal pyc file calling the module which is pyd file. Thats when there was no problem, plugin took it and it worked and i also could save my important code putting them in a module of pyd file. Of course, i aint totally sure if it is the best solution or not but so far it is where i am now.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b58cf9e8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b58cf9e8](https://forums.unrealengine.com/u/anonymous_user_b58cf9e8)\
**Post date:** [March 5, 2020, 5:16pm UTC](https://forums.unrealengine.com/t/how-to-secure-python-script/461883/4 "2020-03-05T17:16:00Z")

</div>

Sounds like a good solution. I believe that a determined adversary could still decompile the pyd and reverse engineer the decompiled C code from there; but this would be quite difficult, and unless your code is top secret it is probably good enough and will definitely prevent casual plagiarism.

---

<div class="post-metadata">

**Author:** ![ZQlby](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ZQlby](https://forums.unrealengine.com/u/ZQlby)\
**Post date:** [November 7, 2023, 8:49am UTC](https://forums.unrealengine.com/t/how-to-secure-python-script/461883/5 "2023-11-07T08:49:59Z")

</div>

what about not, i counter this problem as well
