How to Run Python Code in Sublime Text 3

  1. Setting Up Sublime Text 3 for Python Development
  2. Running Your First Python Script
  3. Using SublimeREPL for Interactive Python Coding
  4. Debugging Python Code in Sublime Text 3
  5. Conclusion
  6. FAQ
How to Run Python Code in Sublime Text 3

In this tutorial, we will discuss how to run Python code in Sublime Text 3, a popular code editor known for its speed and simplicity. Whether you are a beginner looking to write your first Python script or an experienced developer wanting to streamline your workflow, Sublime Text 3 offers a variety of features that make coding a breeze. From setting up your environment to running your code with ease, we’ll guide you through each step, ensuring that you can get your Python programs up and running without a hitch. Let’s dive in!

Setting Up Sublime Text 3 for Python Development

Before we can run Python code in Sublime Text 3, we need to ensure that the editor is properly set up for Python development. First, make sure you have Python installed on your machine. You can download it from the official Python website. Once you have Python installed, follow these steps:

  1. Open Sublime Text 3.
  2. Go to the menu and select Tools > Build System > New Build System....
  3. This will open a new file. Replace the contents with the following code:
JSON
 jsonCopy{
    "cmd": ["python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}
  1. Save the file as Python3.sublime-build.

This configuration tells Sublime Text to use Python 3 to execute your scripts. The -u flag ensures that the output is unbuffered, which is particularly useful for real-time output.

Now, you can select your build system by going to Tools > Build System and choosing Python3. With this setup, you are ready to run your Python code directly from Sublime Text 3.

Output:

 textCopyConfiguration complete. Ready to run Python code.

Running Your First Python Script

Now that we have set up Sublime Text 3 for Python, let’s create and run a simple Python script. Open a new file in Sublime Text and write the following code:

Python
 pythonCopyprint("Hello, World!")

Save the file with a .py extension, for example, hello.py. To run this script, simply press Ctrl+B (or Cmd + B on Mac). Sublime Text will execute the script using the build system you configured.

You should see the output in the build results panel at the bottom of the editor. This is a straightforward way to verify that your setup is working correctly.

Output:

 textCopyHello, World!

Running your first Python script in Sublime Text 3 is a significant milestone. It not only confirms that your environment is set up correctly but also introduces you to the workflow of coding and testing in the editor. The build results panel provides immediate feedback, allowing you to iterate quickly.

Using SublimeREPL for Interactive Python Coding

For those who prefer an interactive coding experience, SublimeREPL is an excellent plugin that allows you to run Python code interactively within Sublime Text 3. To install SublimeREPL, follow these steps:

  1. Open the Command Palette by pressing Ctrl + Shift + P (or Cmd + Shift + P on Mac).
  2. Type Install Package Control and select it.
  3. After installation, open the Command Palette again and type Package Control: Install Package.
  4. Search for SublimeREPL and install it.

Once installed, you can launch an interactive Python console by going to Tools > SublimeREPL > Python > Python. This opens a new tab where you can type and execute Python commands in real-time.

For example, you can enter the following code directly into the SublimeREPL console:

Python
 pythonCopyx = 10
y = 20
print(x + y)

After pressing Enter, the output will be displayed immediately, allowing for quick testing and experimentation.

Output:

 textCopy30

Using SublimeREPL enhances your coding experience by providing an interactive environment. This is particularly useful for testing snippets of code or exploring libraries without needing to create a full script. You can quickly see results and make adjustments, fostering a more dynamic coding process.

Debugging Python Code in Sublime Text 3

Debugging is an essential part of programming, and Sublime Text 3 offers some features to assist with this process. While it doesn’t have built-in debugging tools like some IDEs, you can still effectively debug your Python code using print statements and the build results panel.

Consider the following code snippet that contains an error:

Python
 pythonCopydef add_numbers(a, b):
    return a + b

result = add_numbers(5, '10')
print(result)

When you run this code, you will encounter a TypeError. To debug, you can add print statements to check the types of your variables:

Python
 pythonCopydef add_numbers(a, b):
    print(f"a: {type(a)}, b: {type(b)}")
    return a + b

result = add_numbers(5, '10')
print(result)

By running this modified code, you will see the types of a and b in the output, helping you identify the source of the error.

Output:

 textCopya: <class 'int'>, b: <class 'str'>

Debugging in Sublime Text 3 may require a bit more manual effort compared to dedicated IDEs, but with strategic print statements, you can effectively troubleshoot your code. This method is straightforward and integrates seamlessly into your workflow, allowing you to identify issues quickly.

Conclusion

Running Python code in Sublime Text 3 is a straightforward process that can significantly enhance your coding experience. With the right setup and tools, such as the build system and SublimeREPL, you can easily create, run, and debug your Python scripts. Whether you’re writing simple scripts or complex applications, Sublime Text 3 provides the flexibility and efficiency needed to streamline your development process. Embrace these techniques, and you’ll find that coding in Python becomes even more enjoyable and productive.

FAQ

  1. How do I install Python on my computer?
    You can download Python from the official Python website and follow the installation instructions for your operating system.
  1. Can I run Python 2 code in Sublime Text 3?
    Yes, you can configure Sublime Text to use Python 2 by creating a new build system similar to the Python 3 setup, replacing python3 with python.

  2. What is the purpose of the build system in Sublime Text?
    The build system allows you to run scripts and see the output directly within Sublime Text, streamlining your coding workflow.

  3. Is SublimeREPL free to use?
    Yes, SublimeREPL is a free plugin that can be installed through Package Control in Sublime Text 3.

  4. How can I debug my Python code in Sublime Text 3?
    You can use print statements to check variable values and types, which helps identify errors in your code.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

Related Article - Python Run