The code execution environment in OpenAI GPT models provides an interactive Python sandbox. It allows running Python code, performing analysis, generating files, and working with data directly inside ChatGPT or within custom GPTs. While subject to sandbox and security limits, it is a highly flexible tool for learning, prototyping, testing, and exploring Python-based workflows.
-
What is the Code Execution Environment?
-
What is it used for?
-
How can ChatGPT help with programming?
-
Limitations
-
Benefits
-
Installation and Setup (local use)
-
Data Storage
- Detailed explanation of
/mnt/data
- Detailed explanation of
-
Working with Images
-
Working with Excel Files
- Advanced Excel Processing
-
Working with Word Files
- Advanced Word Processing
-
Working with PDF Files
- Advanced PDF Processing
-
Other advanced applications
-
Contributing
-
Credits
The code execution environment (formerly known as the “Code Interpreter”) is a secure Python sandbox embedded inside GPT models. It runs Python code, processes files, and returns results directly to the user.
A wide range of tasks, including:
- Mathematical computations
- Data analysis and data transformation
- Prototyping and debugging Python
- Working with files: images, documents, spreadsheets, PDFs
- Teaching and learning programming
- Automating small workflows
ChatGPT can:
- Generate code
- Review and debug existing code
- Explain concepts
- Create structured workflows
- Help with refactoring, analysis, or documentation
The sandbox has several constraints:
- No internet access
- No access to the host operating system
- Runtime limit around 300 seconds
- Memory limits depending on the model
- Only the directory
/mnt/datais readable and writable - The environment resets between sessions
GPTs (not plain ChatGPT sessions) may also include persistent file storage, but this is separate from the runtime sandbox.
- Safe execution environment
- Immediate feedback loop
- Supports a wide spectrum of Python libraries
- Ideal for experiments, tutorials, and fast prototyping
- No risk to the user’s local system
You only need this if you want to reproduce the examples locally. ChatGPT users do not need to install anything.
pip install pandas openpyxl python-docx pypdf fpdf2 matplotlib pillowNotes:
pypdfis the modern replacement forPyPDF2- Many of these libraries are already available inside ChatGPT’s sandbox
The Python sandbox has access to the directory /mnt/data.
Files placed there can be read, written, and downloaded.
/mnt/data is a temporary storage area that:
- Persists across code cells
- Does not persist after the session ends
- Supports reading and writing of any file type
Examples:
Writing a text file
with open('/mnt/data/numbers.txt', 'w') as file:
for num in range(10):
file.write(str(num) + '\n')Reading it
with open('/mnt/data/numbers.txt', 'r') as file:
numbers = file.readlines()Saving a plot
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [0, 1, 4])
plt.savefig('/mnt/data/plot.png')Examples using PIL and matplotlib:
Display:
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('/mnt/data/your_image.jpg')
plt.imshow(img)
plt.axis('off')
plt.show()Resize:
img_resized = img.resize((800, 600))Rotate:
img_rotated = img.rotate(90)Convert to grayscale:
img_gray = img.convert('L')Reading:
import pandas as pd
df = pd.read_excel('/mnt/data/example.xlsx')
print(df.head())Writing:
df.to_excel('/mnt/data/output.xlsx', index=False)Filtering:
filtered = df[df['Age'] > 30]Sorting:
sorted_df = df.sort_values('Age')Pivot tables:
pivot = df.pivot_table(index='Category', values='Sales', aggfunc='sum')Merging multiple Excel files:
import glob
files = glob.glob('/mnt/data/*.xlsx')
dfs = [pd.read_excel(f) for f in files]
merged = pd.concat(dfs, ignore_index=True)Reading:
from docx import Document
doc = Document('/mnt/data/example.docx')
for para in doc.paragraphs:
print(para.text)Writing:
doc = Document()
doc.add_paragraph('Hello world')
doc.save('/mnt/data/new.docx')Tables:
table = doc.add_table(rows=3, cols=3)Reading text:
from pypdf import PdfReader
reader = PdfReader('/mnt/data/example.pdf')
text = reader.pages[0].extract_text()
print(text)Writing PDFs:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Hello PDF", ln=True)
pdf.output('/mnt/data/new.pdf')Merging:
from pypdf import PdfMerger
merger = PdfMerger()
merger.append('/mnt/data/file1.pdf')
merger.append('/mnt/data/file2.pdf')
merger.write('/mnt/data/merged.pdf')
merger.close()Splitting:
reader = PdfReader('/mnt/data/example.pdf')
for i, page in enumerate(reader.pages):
writer = PdfWriter()
writer.add_page(page)
with open(f'/mnt/data/page_{i+1}.pdf', 'wb') as f:
writer.write(f)Contributions and improvements are welcome. Pull requests and issues are appreciated.
- Volkan Kücükbudak
- OpenAI GPT models for interactive assistance
- Community contributors
- Repository: https://github.com/VolkanSah/The-Code-Interpreter-in-OpenAI-GPT/
updated 11.12.2025