Sublime text python запуск кода

Sublime Text 4
Using Python in Sublime Text

As macOS comes with Python pre-installed, Sublime Text can usually run it out-of-the-box:

  • Using the Sublime Text Build System:
    • Open a Python script (a file with the “.py” extension) in Sublime Text then try to build it using either the menu (Tools > Build) or the keyboard shortcut (Cmd+B)
    • If a box appears in the lower part of the screen with text detailing information about your build, then Sublime Text has successfully run Python on your script
    • If no box appears and you see the words “No Build System” in the very bottom-left of the screen (in the bottom grey bar) then it hasn’t worked and you need to follow the “Creating a Python Build System” instructions below
    • A Sublime Text package called SublimeREPL can be installed and used to run Python. You may prefer it to the Build System or you may not, it’s up to you.
    • This can be installed through Sublime Text’s Package Control:
      • If you do not have Package Control yet, first install it either from the menu (Tools > Install Package Control…) or from the Command Palette (Tools > Command Palette… [or use the keyboard shortcut Shift+Cmd+P], search for “Install Package Control” and press Enter)
      • Once you have Package Control installed, open the Command Palette again, search for “Package Control: Install Package”, hit Enter, search for “SublimeREPL” and hit Enter again
      • Terminus is another Sublime Text package. It opens a terminal as a window in Sublime Text which, just like a regular terminal, can be used to run Python on a file.
      • Install Terminus from Package Control as detailed above: open the Command Palette either from the menu (Tools > Command Palette…) or via the keyboard shortcut (Shift+Cmd+P), search for “Package Control: Install Package”, Enter, search for “Terminus”, Enter again
      • It’s useful to create a custom key binding for Terminus: from the menu go Sublime Text > Preferences > Package Settings > Terminus > Key Bindings and on the right-hand side (Default (OSX).sublime-keymap) add the following to the JSON file (leave the final comma out if it’s the last key binding in the file):

      However, even though Python can now be used from within Sublime Text there are a couple of things to be aware of:

      Troubleshooting

      When changing settings, always close-and-open Sublime Text and/or your terminal to let the changes take effect.

      • What version of Python is being used? And is it Python 2 or Python 3?
        • Check by running the following script:
        import platform print(platform.python_version())
        • If it’s a version of Python 2 you should definitely change it as that version is no longer supported
        • If you are using the build system it will be whatever Python version is specified in the relevant sublime-build file (which you may or may not have access to) so, if it isn’t using the version you want, see below about how to create a new build system
        • If you are using SublimeREPL you will have to fiddle around with that package’s settings to change the Python version
        • If you are using Terminus it will be whatever version corresponds to the terminal command you are using, eg python3.11 will use 3.11 but python3 and python will either use the computer’s default version or whatever version is set as an alias in your terminal’s settings file (eg ~/.bash_profile , ~/.zprofile or ~/.zshrc ). Either explicitly use the command that corresponds to the Python version you want to use or change the alias(es) in the settings file(s).
        • If you have multiple copies of Python installed — eg one in /Library/Frameworks/ and another in /usr/local/bin/ — it will probably not be clear which one Sublime Text is using
        • Check by running this script:
        import sys print(sys.executable)
        • If you are using the build system then it will be using a terminal command in the background to run Python and the copy being used will be that associated with the command. Ensure you know which terminal command is being used by the build system (eg by creating your own build system) then you can edit which copy of Python this command is aliased to in a terminal settings file ( ~/.bash_profile , ~/.zprofile or ~/.zshrc )
        • If you are using SublimeREPL you will have to fiddle around with that package’s settings to change it
        • If you are using Terminus then, like the build system, you will be using a terminal command. Edit a terminal settings file to change which copy of Python your command is aliased to.
        • From macOS 10.15 Catalina onwards, the default shell is Zsh (as opposed to Bash, which is what it was before). This is relevant because it will determine what your terminal settings file is ( ~/.zprofile or ~/.zshrc for Zsh, ~/.bash_profile for Bash).

        Creating a Python Build System

        To create a custom build file for Python 3.11:

        • Save it as Python 3.11.sublime-build in the default location (which will be /Users//Library/Application Support/Sublime Text 3/Packages/User if you’re using Sublime Text 3 or initially installed Sublime Text 3 and have since upgraded to 4)
        • Open a Python file in Sublime Text and go Tools > Build With… (keyboard shortcut: Shift+Cmd+B) > Python 3.11
        • You should only have to do this once as the ‘build with’ command sets the default build system for a file type. From now on you should be able to just do Tools > Build (Ctrl+B)

        For the more generic ‘Python 3’ (which will use a version of Python 3, not necessarily the latest one you have installed) you can use the following (for any other version of Python, just replace the python3 with the version you want — provided you have it installed, of course):

        Ubuntu

        Ubuntu 22.04

        • Running Python files in Sublime Text usually works out-of-the-box:
          • Open a Python file (a file with a .py extension) in Sublime Text
          • From the menu bar, go to Tools > Build (or press Ctrl+B). This should run the Python script.

          Changing the Version of Python used by Sublime Text

          • The Sublime Text Python Build System works by running the python3 command on the file that is currently open. By default, this will be Python 3.10.6 because that is the version Ubuntu 22.04 comes pre-installed with. If you want to change that (eg if you have Python 3.11 installed and working and want Sublime Text to use that) you will need to create a new build system:
            • Go to Tools > Build System > New Build System…
            • Replace all text with:
            • Save it to the default location (which is ~/.config/sublime-text/Packages/User ) with a meaningful name like “Python 3.11.sublime-build”

            Using SublimeREPL

            • Install SublimeREPL via Package Control (Ctrl+Shift+P > search for “install package” > search for “SublimeREPL”)
            • Tools > SublimeREPL > Python > Python — RUN current file (you can optionally create a user key binding to do this via a keyboard shortcut)
            • Shift+Alt+8 creates a lower window which you can drag the Python output console to
            • Troubleshooting:
              • If the code runs fine but you get the following window:
              SublimeREPL: obtaining sane environment failed in getenv() Check console and 'getenv_command' setting WARN: Falling back to SublimeText environment

              Using Terminus

              Python files can also be run ‘manually’ from a Terminus terminal, look here for more info on installing that.

              Troubleshooting: Use LaTeX format with Matplotlib

              This is for when you are using the code plt.rc(‘text’, usetex=True) in an attempt to use LaTeX in Matplotlib plots but it’s causing them to not render or save. You will be seeing a very long error and/or an error saying that ‘dvipng’ could not be found.

              • Firstly, make sure that LaTeX is installed and working outside of Sublime Text (eg by rendering a .tex file from the terminal or in a LaTeX editor like TeXstudio)
              • Secondly, check that the Sublime Text package “LaTeXTools” is installed and working by running a LaTeX file in Sublime Text
              • If that works, everything should be sorted by installing dvipng from the terminal with:
              sudo apt-get install dvipng
              • If you get an error saying matplotlib.cbook.Locked.TimeoutError: LOCKERROR , delete your tex.cache folder before trying again:
              rm /home//.cache/matplotlib/tex.cache

              Ubuntu 16.04, 18.04 and 20.04

              # the terminal command "python" should open python3, not python2 alias python=python3
              • Save, close and restart the terminal before running python —version again. The Python version should now be 3.x.x.
              • This usually doesn’t work out-of-the-box
              • Go to Tools > Build System > New Build System…
              • Replace all text with:
              • Save it to the default location (which is ~/.config/sublime-text/Packages/User ) with a meaningful name like “python3.sublime-build”
Оцените статью