1

For example imagine I have myfile.py.
Sample code:

a = 6
b = 4
print(a+b)

So how can I convert this into bytecode?

I tried this:

source_code = '''a = 6
b = 4
print(a+b)'''
compiled_code = compile(source_code, '<string>', 'exec')
print(compiled_code.co_code)

Result:

b'\x97\x00d\x00Z\x00d\x01Z\x01\x02\x00e\x02e\x00e\x01z\x00\x00\x00\xa6\x01\x00\x00\xab\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00d\x02S\x00'

It does give me a bytecode, but when I run it using exec(), it results in this error.

Traceback (most recent call last):
  File "c:\Users\Dimuth De Zoysa\Desktop\Python_projects\OBftestcode.py", line 2, in <module>
    exec(compiled_code.co_code)
ValueError: source code string cannot contain null bytes
4
  • 2
    How doesexec(bytecode) not give you a NameError? You never defined bytecode... Commented Aug 24, 2023 at 11:25
  • 2
    exec(compiled_code) works just fine Commented Aug 24, 2023 at 11:27
  • If you need to work with the actual bytecode, there's e.g. decompyle3. Commented Aug 24, 2023 at 12:05
  • Only to mention it you can use jythonc to compile python files into bytecode Commented Aug 25, 2023 at 6:01

4 Answers 4

0

Don't execute your result bytecode. Execute compiled code as an object returned by compile function like this:

source_code = '''a = 6
b = 4
print(a+b)
''' # good style for using such functions as compile and others is to
#     write new line character at the end of the code
compiled_code = compile(source_code, '<string>', 'exec')
exec(compiled_code)

Do not execute exec(compiled_code.co_code), as it contains byte code translated to viewable form, and doing this gives your error.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok. But I just want to hide the source code , so nobody can see it
0

See https://docs.python.org/3/library/functions.html#exec and https://docs.python.org/3/library/functions.html#compile

exec(compiled_code) will work just fine.

Were you trying to exec the string representation instead of the actual code_object by mistake?

3 Comments

So how can I just exec bytecode without showing the source code.
Not really sure why you would want to do this. But, seems you want to create a code_object that you can "exec" from raw byte codes rather than source code. You might want to look at stackoverflow.com/questions/16064409/…. Not for the feint-hearted.
They don't want to run compiled_code, they want to run the actual bytecode (compiled_code.co_code).
0

You can compile the code with the py_compile module.

from py_compile import compile
compile("my_script.py", "my_script.pyc")

Then you can run the bytecode file with python.

> python my_script.pyc
10

Comments

-3

Try below code:

import codecs

source_code = '''a = 6
b = 4
print(a+b)'''

compiled_code = compile(source_code, '<string>', 'exec')

# Convert bytecode to hexadecimal representation
bytecode_hex = codecs.encode(compiled_code.co_code, 'hex')
print(bytecode_hex)

1 Comment

How does this solve the problem? Actually the exec(compiled_code) already works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.