Coding Education STEM

Running code from within another code (Python)

Most of us who have done any programming know that virtually all languages allow you to execute another program from within a program. For example, you start your program, which may start another process (via calling Execute or the like) that’s a self-contained program by itself, resulting in another process or spawn another thread as desired. Those are done by starting another executable or a DLL typically in Windows. What’s interesting in Python is that the code (calling it script would be an understatement in Python) is interpreted in real-time, executed. But we can actually process a completely unrelated code/program or snippet thereof as text from within another Python code/program without having to link/compile and make binary executable. I think it’s a really powerful and cool feature. In this post, I share a way on how to do exactly that.

The Setup:

First, we write an usual Python program, perhaps set up a function (the cleaner way of doing this) to wrap the code block we are going to execute, and call that helper function. Within that function, the code block will contain everything a Python program normally would except the whole block of code will be wrapped by three double-quotes. Everything within that block can contain any Python syntax such as comments (#…), import statements to import packages/modules, etc. Anything that’s legal in Python syntax.

Then we call that helper function from our driver code. Within the helper function, we call exec() function passing it the entire code block to execute (that was wrapped in three double-quotes).

Show & Tell:

So, let’s define our helper function. I’ll call it myexec_code_fn and it’ll take 2 parameters (for this example, but completely optional). myexec_code_fn() will use these parameters but its entire operations with the parameters will be in a code block (surrounded by three double-quotes). The code block at this point is nothing but a bunch of Python code. In this example, it does some math operations and shows the outputs in decimal and binary formats. The code block is initialized in a variable codeBlock as below:

Notice that although the entire block of code is what looks like a string blob, you still need to pay attention to indentation, and make all the necessary imports of libraries/modules etc. You can even put additional comments inside the string. However, this block is not going to be executable by itself yet.

Then let’s wrap this in the helper function I mentioned so it looks like this:

The key is the exec() call. Pay attention to the indentation as this is part of the helper function myexec_code_fn().

Lastly, all we need to do is call this function passing it the parameters it expects (in this example, two numbers). So, the driver code is simply:


(or any numeric parameters you want)

The output from the above is:

This also means, we can store external Python programs in a separate file, and simply load all or parts of their code into a string and run it from here! Or, you can imagine a set of instructions (per line) in an external file (in any readable format: Text, HTML, JSON, XML, or proprietary) and load each line and execute their contents as you wish from within another app.

Pretty cool I think! Play with the idea.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top