Python compile()

5/5 - (1 vote)

If you’re like me, you love those TLDR; overviews to grasp the big picture quickly. Here is mine about Python’s compile() function:

Python’s built-in compile() method returns an executable code object as an “Abstract Syntax Tree” represented as an ast object. By passing this code object into the exec() or eval() functions, you can run it dynamically in your Python code. This way, you can programmatically create source code and execute it at runtime. To use the function, pass the string code to be executed, the filename, and the execution mode. For example compile('print("hi")', '<string>', 'exec') creates a code object consisting of one line print("hi").

This article shows you how to use Python’s built-in compile() method.

Python compile() built-in function -- Illustrated Explanation

Usage compile()

Learn by example! Let’s say, you have a code file with the following content:

# some_code.py file
customers = {"Alice": 40000, "Bob": 33000}
print(f"Bob: {customers['Bob']}")

Here’s an example on how to use the compile() built-in function.

# Read the code from the file or define it explicitly:
code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")'

# Create the ast code object
code_obj = compile(code, '<string>', 'exec')

# Execute the ast code object
exec(code_obj)

# Result:
# Bob: 33000

First, you create the code as a string. Second, you pass the string into the compile() function to create an ast object. You can then pass this object into the exec() function and run it dynamically.

Video compile()

Python compile() - The Ultimate Guide

Why Using compile() Instead of exec() With a String?

Before you dive into the syntax, you may not be motivated to use the compile() function in the first place. Why? Because you can also use the exec() function on a string instead of a code object.

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code.

Here’s the same code without compile():

code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")'
exec(code)
# Bob: 33000

The output is the same. However, there are two advantages of using a code object:

  • You make it explicit. Without compiling the string explicitly, the exec() method would do the same work implicitly. Thus, if you need to run the same code multiple times, you can save significant compilation overhead by just compiling it once in advance.
  • You can use the powerful ast object otherwise. This gives you access to some helper methods and additional information about the code such as the names defined in the code:
>>> code_obj.co_names
('customers', 'print')

You obtain this information from a compiled string which is very convenient!

Syntax compile()

You can use the compile() method with a number of different arguments.

Syntax: 
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 
ArgumentssourceA string to be compiled into a code object.
filenameThe file from which the code given in source was read. If this was a string, use '<string>'.
modeThe execution mode—must be one of the following:
'exec' — If source is a sequence of statements
'eval' — If source is a single expression
'single' —If source is a single interactive statement
Optional: flags=0Controls which compiler options should be activated and which future features should be allowed.
Optional: dont_inherit=FalseDo you want to avoid inheritance of the compiler options and future features?
Optional: optimize=-1Optimization level of the compiler:
-1 selects the optimization level of the interpreter as given by -O options.
0 selects no optimization and __debug__ to True.
1 specifies that asserts are removed and sets __debug__ to False.
2 additionally removes docstrings.
Return ValueastReturns an AST object that represents the code as an abstract syntax tree.

Interactive Shell Exercise: Understanding compile()

Consider the following interactive code:

Exercise: Print the number associated to Alice in the dictionary!


But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


How to Read and Compile Code from a File

Say, you have the following code in a code file:

# filename.py file
customers = {"Alice": 40000, "Bob": 33000}
print("Bob: " + str(customers['Bob']))

How to read the code from a file, compile it, and execute it at runtime?

Here’s the code:

# 1. Read code from file
f = open('filename.py', 'r')
code = f.read()
f.close()

# 2. Compile code string
code_obj = compile(code, 'filename.py', 'exec')

# 3. Run the code object (ast)
exec(code_obj)

Alternatively, you can also use the following one-liner to read the code from a file:

code = open('filename.py').read()

Frankly, I don’t see a huge problem with not closing the file if you have a small script and you don’t access the file anywhere else. Python will close it automatically after the code terminates.

Summary

The Python compile() method returns an executable code object as an “Abstract Syntax Tree” that is represented as an ast object.

There are many applications of an ast such as the following: You can pass this code object into the exec() or eval() functions and run it dynamically in your Python code.

This way, you can programmatically create source code and execute it at runtime.

To use the function, you must pass the string code to be executed, the filename, and the execution mode.

For example compile('print("hi")', '<string>', 'exec') would be a valid call that creates a code object consisting of one line print("hi").


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!