Create your Own IDE || Build a Python Compiler || Best IDE for Python || Python Project

Tandap Noel Bansikah
7 min readMay 19, 2023

Have you ever thought of building your own IDE or compiler? Yes you can build your own compiler. In this article i will be taking you through the processes of building your own compiler using Python.

In this project, you will be able to do all the fun things a code editor can do ie. you will be able to write your code, see the output of your code, save your code, run code, open another file, exit the compiler and so on…

So let’s get started, to build this project i will be using pyCharm a python IDE just for a little bit and soon we will have our IDE. So you can create a new project and give it a name maybe my IDE or any name of your choice.

After that, create a new file name and name it compiler.py or any name of your choice but it should end with the .py extension. Let’s Code.

Import tkinter and create an variable

from tkinter import *

compiler = Tk()
compiler.mainloop()

after that you can run to see that the tkinter is imported. Results:

But the fun will begin when we have something to write and compile so lets continue. So to do this you will have to add just these two lines of code.

from tkinter import *

compiler = Tk()
editor = Text()
editor.pack()
compiler.mainloop()

Now you can write something. Results:

And if you want to give a fancy name for your IDE you can just name it with this code.

from tkinter import *

compiler = Tk()
compiler.title('My Fantastic IDE')//name of IDE
editor = Text()
editor.pack()
compiler.mainloop()

Now let’s ride on, We are going to add some menu options on our IDE, to do this we need to write this code.

compiler.title('My Fantastic IDE')#Naming you ID

#code for Menu bar
menu_bar = Menu(compiler)

run_bar = Menu(menu_bar)
run_bar.add_command(label='Run')
menu_bar.add_cascade(label='Run', menu=run_bar)
compiler.config(menu=menu_bar)

editor = Text()
editor.pack()
compiler.mainloop()

Results:

To add more functionalities to our code, we will need to create some functions.

compiler = Tk()
compiler.title('My Fantastic IDE')#Naming you ID

#creating our functions
def run():
print('Code will be run')

#code for Menu bar
menu_bar = Menu(compiler)

run_bar = Menu(menu_bar, tearoff=0)
run_bar.add_command(label='Run', command=run)
menu_bar.add_cascade(label='Run', menu=run_bar)
compiler.config(menu=menu_bar)

with the command run and the run function you can now run your code. Results:

Now we need to capture or get what ever code that is written in our compiler. To do this just add this code(code = editor.get(‘1.0’, END)) within the run function.

from tkinter import *

compiler = Tk()
compiler.title('My Fantastic IDE')#Naming you ID

#creating our functions
def run():
code = editor.get('1.0',END)#To get your code
print(code)

#code for Menu bar
menu_bar = Menu(compiler)

run_bar = Menu(menu_bar, tearoff=0)
run_bar.add_command(label='Run', command=run)
menu_bar.add_cascade(label='Run', menu=run_bar)
compiler.config(menu=menu_bar)

editor = Text()
editor.pack()
compiler.mainloop()

To execute our code we will need to use the exec(code)

#creating our functions
def run():
code = editor.get('1.0',END)
exec(code)

and you see below the code is been executed

Now this not the best way to execute our code but, it is the easiest way, we will have the best way as we move further. We are going to add more functionalities like creating the create files and so on. Now we have the code below.

Add this where you have your codes for creating menu bar.

file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label='Open', command=run)
file_menu.add_command(label='Save', command=run)
file_menu.add_command(label='Save As', command=run)
file_menu.add_command(label='Exit', command=exit)
menu_bar.add_cascade(label='File', menu=file_menu)

You can add as many functions as you want… Now we are going to modify the save as functionality by creating a function def save_as(): and also we will have to import the from tkinter.filedialog

Don’t forget to change the command below to save_as

file_menu.add_command(label='Save As', command=save_as)

Now our save as is working, Now we are going to do same for the open file option.

from tkinter.filedialog import asksaveasfilename, askopenfilename//added askopenfile to the import

def open_file():
path = askopenfilename(filetypes=[('Python Files', '*.py')])
with open(path, 'r') as file:
code = file.read()
editor.delete('1.0', END)
editor.insert('1.0', code)

And don’t forget to change command below to open_file.

file_menu.add_command(label='Open', command=open_file)

Now we have one extra thing which is to save a file. So what we need is the file path, the destination or the location.

file_path = ''

def set_file_path(path):
global file_path
file_path = path

and also we have to set the file path in the open and save as functionality with the code.

  set_file_path(path)

the last thing we will have to do in the save as function is to use the if condition to check.

def save_as():
if file_path == '':
path = asksaveasfilename(filetypes=[('Python Files', '*.py')])
else:
path = file_path
with open(path, 'w') as file:
code = editor.get('1.0', END)
file.write(code)
set_file_path(path)

Don’t forget to change your function name in the file menu to save_as

file_menu.add_command(label='Save', command=save_as)

Now let’s see how our code works.

Now we can save and edit our codes in our newly build IDE, but that’s now all we are going to make our IDE independent of pyCharm from viewing its output that mean soon we will be see our results on our own IDE, so let ride on.

So we will add our output section with just two lines of code. Below the editor = Text() editor.pack() add these two lines of code

code_output = Text(height=10)
code_output.pack()

Now to get our results in the output section, we will need to write some code. To do so we will need to update our run function and import a subprocess.

import subprocess

Now in our run function, previously we were doing it an easier way but that way is not efficient some we will need to modify it as i stated above.

def run():
command = f'python {file_path}'
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
code_output.insert('1.0', output)
#code = editor.get('1.0', END)
# exec(code)

Results:

Now you see we can execute our code and have an output. Lets see of our code editor can detect some errors and detect to us. You can do that by adding code_output.insert(‘1.0’, error) below your run function

code_output.insert('1.0', error)

So far our code editor is working but if you just open a file or maybe a file path does not exist or there is nothing in the file you want to execute it is going to do some unnecessary behavior. To fix this we will use an if condition.

#include in the run function  
if file_path == '':
save_prompt = Toplevel()
text = Label(save_prompt, text='Please save your code')
text.pack
return

So this is all about our compiler, hope your enjoyed it . I will put the complete code below.

from tkinter import *
from tkinter.filedialog import asksaveasfilename, askopenfilename
import subprocess

compiler = Tk()
compiler.title('My Fantastic IDE')#Naming you IDE
file_path = ''

def set_file_path(path):
global file_path
file_path = path

def open_file():
path = askopenfilename(filetypes=[('Python Files', '*.py')])
with open(path, 'r') as file:
code = file.read()
editor.delete('1.0', END)
editor.insert('1.0', code)
set_file_path(path)

def save_as():
if file_path == '':
path = asksaveasfilename(filetypes=[('Python Files', '*.py')])
else:
path = file_path
with open(path, 'w') as file:
code = editor.get('1.0', END)
file.write(code)
set_file_path(path)

#creating our functions
def run():
if file_path == '':
save_prompt = Toplevel()
text = Label(save_prompt, text='Please save your code')
text.pack
return
command = f'python {file_path}'
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
code_output.insert('1.0', output)
code_output.insert('1.0', error)
#code = editor.get('1.0', END)
# exec(code)

#code for Menu bar
menu_bar = Menu(compiler)

file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label='Open', command=open_file)
file_menu.add_command(label='Save', command=save_as)
file_menu.add_command(label='Save As', command=save_as)
file_menu.add_command(label='Exit', command=exit)
menu_bar.add_cascade(label='File', menu=file_menu)

run_bar = Menu(menu_bar, tearoff=0)
run_bar.add_command(label='Run', command=run)
menu_bar.add_cascade(label='Run', menu=run_bar)

compiler.config(menu=menu_bar)

editor = Text()
editor.pack()

code_output = Text(height=10)
code_output.pack()

compiler.mainloop()

Hope you learned how to build your own IDE using python, please do well to like and comment in the comment sections . Thank you

--

--