0

I have written python code and saved as program.py file.Now When i open "program.py" file it should be unreadable format.

Can anyone suggest me how to fix this issue.

Alperen
  • 3,772
  • 3
  • 27
  • 49
kishore sagar
  • 31
  • 2
  • 5
  • Maybe this link can help you out: [link](https://stackoverflow.com/questions/261638/how-do-i-protect-python-code) –  Dec 04 '17 at 15:37

2 Answers2

4

You can creat pyc file with compileall:

Put your python files in a folder and run the command in this folder:(Source)

python -m compileall .

This command will produce a pyc file in __pycache__ folder. You can use this file like other python files:(Source)

python myfile.pyc
Alperen
  • 3,772
  • 3
  • 27
  • 49
  • You can still read the .pyc file and some import info is seen as well using Linux "cat" command – manjesh23 Aug 26 '20 at 15:41
  • @manjesh23 Yes, strings and docstring are also readable, but its priority is not hiding the code. `pyc` files contain the bytecode to speed up the process for the next run. However, it is not readable in general. – Alperen Aug 27 '20 at 03:01
0

One way to do that is by compiling the source code using the py_compile module:

python -m py_compile my-py-source.py

The compiled source code will be in _pycache/my-py-source.pyc (relative to your current directory). And the code will still run normally when you run python my-py-source.pyc.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Derskeal
  • 11
  • 4