how we can run python in sublime text3

4.3k Views Asked by At

code doesn't run correctly

ctrl + B and search on google, and also I searched on Youtube, Google and I tried several ways. there was some solutions but it didn't work. there is no error but unfortunately, my code doesn't run

2

There are 2 best solutions below

3
Hermann12 On BEST ANSWER

Download the portable sublime3, go to: Tools | Build Systems | Python.

With "Build" you can run your code.

enter image description here

Crtl + B shows your python version, what is in your system environment variable defined: enter image description here

I would anyway suggest Thonny

0
Kishan Gurumurthy On
  1. Python Setup:

    • Windows:
      • Please download the latest Python installer from the official Python website (https://www.python.org/downloads/) and run it. If you have already have the python downloaded, please make sure you have added the python bin to the path.
      • During installation, make sure to select the option to add Python to the system PATH.
    • macOS:
      • macOS usually comes with a pre-installed version of Python. However, you can install an updated version using package managers like Homebrew (https://brew.sh/) or by downloading the installer from the official Python website.
    • Linux:
      • Python is often pre-installed on Linux distributions. You can check if Python is available by running python --version or python3 --version in the terminal. If Python is not installed, you can install it using your package manager.
  2. Sublime Text 3 Configuration:

    • Open Sublime Text 3 and go to "Tools" -> "Build System" -> "New Build System".

    • In the new build system file, paste the following configuration:

      {
          "cmd": ["python3", "-u", "$file"],
          "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
          "selector": "source.python",
          "encoding": "utf8",
          "env": {"PYTHONIOENCODING": "utf-8"}
      }
      
    • Save the file with a descriptive name, for example, "Python3.sublime-build".

  3. Running Python Code:

    • Open a Python file in Sublime Text 3.
    • To execute the Python code, go to "Tools" -> "Build System" and select the build system you created ("Python3" in our example).
    • Press Ctrl+B (Windows/Linux) or Cmd+B (macOS) to run the Python code.
    • The output will be displayed in the Sublime Text console at the bottom.

    Note: Make sure to save the Python file with a ".py" extension before running it.

By configuring Sublime Text 3 with the Python build system, you can easily execute Python code within the editor and view the output. This setup allows for quick testing and debugging of Python programs directly from Sublime Text.

Remember to install any necessary Python packages or dependencies required by your code using tools like pip or virtual environments, as Sublime Text does not handle package management directly.