0

I am using python 3.6.

I have below directory structure:

 test_run.py

 addition --> add.py

I have test_run.py file which is importing one of the function called add_values() from add.py present inside Addition directory or package.

Code for test_run.py and add.py is as below:

test_run.py:

from addition.add import add_values
 
print(add_values(5,4))

add.py:

def add_values(a,b):
    return (a+b)

I compiled everything using cpython compileall to create a .pyc file for each python file. Because I want to get rid of all python files and use only byte code files or compiled files.

test_run.py created test_run.cpython-36.pyc
and add.py created add.cpython-36.pyc.

Then I deleted actual python files (test_run.py and add.py) and ran compiled file test_run.cpython-36.pyc but it is giving me error:

ModuleNotFoundError: No module named 'addition.add' 

It works fine with source files (test_run.py), it fails only when I run bytecode file(test_run.cpython-36.pyc) without source files(after deleting the source files)

Please guide me how do I get rid of original python files and use only byte code files? I want to achieve complete obfuscation of code.

Yogesh
  • 73
  • 1
  • 6
  • the folder is `Addition` with a capital `A` but you are importing as `addition` with lower case `a`? Does the program work correctly when run from the source files or does it fail in both cases? – Tadhg McDonald-Jensen Jul 15 '20 at 16:26
  • 1
    its typo , folder name is 'addition' and code works fine with source files. It fails only with bytecode files – Yogesh Jul 15 '20 at 16:30
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Jul 15 '20 at 16:40
  • code may run from different folder and then Current Working DIrectory is different (check `os.getcwd()`) and you may have to get path to folder with code and add it to PATH. – furas Jul 15 '20 at 16:42
  • It works fine with source file (test_run.py and add.py) , it fails when I delete the source file(test_run.py and add.py) and execute the bytecode file (test_run.cpython-36.pyc) – Yogesh Jul 15 '20 at 16:52
  • Does this answer your question? [How to run a Python project using \_\_pycache\_\_ folder?](https://stackoverflow.com/questions/53918318/how-to-run-a-python-project-using-pycache-folder) – ead Jul 15 '20 at 17:29

1 Answers1

0

As far as I know, there is no way to just use the bytecode to run your python script. The pycache folder is just to speed up the process of importing and it cannot itself be imported

here is a helpful post that explains what the pycache folder does. What is pycache

Dharman
  • 30,962
  • 25
  • 85
  • 135
Saad Ahmad
  • 66
  • 4