Written by a Google engineer sublime text is a cross-platform IDE developed in C++ and Python. It has basic built-in support for Python. Sublime text is fast and you can customize this editor as per your need to create a full-fledged Python development environment. You can install packages such as debugging, auto-completion, code linting, etc. There are also various packages for scientific development, Django, Flask, and so on.
SublimeREPL Allows you to run a Python interpreter session in an editor view. Sublime Text for Python development — My 2016 review – When you ask for editor recommendations as a Python developer one of the top choices you’ll hear about is Sublime Text. In this post I’ll review the status of Python development with Sublime Text as of. According to the documentation referenced by the Sublime Text website itself, the embedded Python intrepreter is not intended for general development. This restricted usage recommendation even applies to ST2 for OS X, which is the only version that does use the system's Python version. SublimeREPL lets you run an interpreter of a range of languages (NodeJS, Python, Ruby, Scala and Haskell to name a few) right inside Sublime Text. Let us run a Python interpreter and see if it works. Write and run Python code using our online compiler (interpreter). You can use Python Shell like IDLE, and take inputs from the user in our Python compiler.
Downloading and Installation
Sublime Text exposes its internals via an Application Programming Interface (API) that programmers can interact with using the Python programming language. An embedded Python interpreter is included in the editor. The embedded interpreter is useful to inspect the editor’s settings and to quickly test API calls while developing plugins.
Sublime Text 3 can be downloaded from its official site sublimetext.com. To install sublime text 3 on Windows, go through How to install Sublime Text 3 in Windows?
Setting up Environment Variable
- Step 1: Click the Advanced system settings link.
- Step 2: Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
- Step 3: In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Working on Sublime Text3
Step 1: Create a new file and save it with extension .py for example save it as checkversion.py. Now, Go to Tools -> Build System -> Python then type on your checkversion.py
This is showing the version of python. This means python is successfully installed and added in Environment Variable.
This is showing the version of python. This means python is successfully installed and added in Environment Variable.
Step 2: Add new build system on your Sublime Tools -> Build System -> New Build System and make sure that the new build system has this following command
'cmd' :[ 'C:/Users/<user>/AppData/Local/Programs/Python/Python37-32/python.exe' , '-u' , '$file' ], 'file_regex' : '^[ ]File '(...?)', line ([0-9]*)' , } |
Select your new system build newPython3 and re-run the checkversion.py and now it should be using Python 3
ALL Done…
Now create any file and save it with .py extension
Now create any file and save it with .py extension
Now you can run your Python code by using CTRL+SHIFT+B and choose from the 2 options.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the 'Improve Article' button below.
A solid text editor is a developer’s best friend. You use it constantly and itbecomes like a second pair of hands. The keyboard commands you use dailybecome so engrained in your muscle memory that you stop thinking about thementirely.
With Sublime Text, it’s possible to turn your text editor into the functionalequivalent of a Python IDE. The best part is you don’t have to install an IDEto do it.
Requirements¶
Here are my requirements for an ‘IDE’:
- It should provide excellent, configurable syntax colorization.
- It should allow for robust tab completion.
- It should offer the ability to jump to the definition of symbols in otherfiles.
- It should perform automatic code linting to help avoid silly mistakes.
- It should be able to interact with a Python interpreter such that whendebugging, the editor will follow along with the debugger.
Which Version?¶
Version 2 will be fine, but I would urge you to consider updating to version 3.Some of the plugins I recommend are not available for version 2.
Basic Settings¶
All configuration in Sublime Text is done via JSON. It’s simple to learn. goand read that link then return here.
There are a number of different levels of configuration in Sublime Text. Youwill most often work on settings at the user level.
Open Preferences -> Settings-Default to see all the default settingsand choose which to override.
Create your own set of preferences by opening Preferences -> Settings-User. This will create an empty file, you can then copy the settings you wantto override from the default set into your personal settings.
Here’s a reasonable set of preliminary settings (theme, color scheme and fontare quite personal, find ones that suit you.):
Especially important is the setting translate_tabs_to_spaces, which ensuresthat any time you hit a tab key, the single t character is replaced by fours characters. In Python this is vital!
Note
Remember, the font, color scheme and theme settings above are those Iuse. You will need to install extra packages to get them.
Extending the Editor¶
Most of the requirements above go beyond basic editor function. Use Plugins.
Sublime Text comes with a great system for Package Control. It handlesinstalling and uninstalling plugins, and even updates installed plugins foryou. You can also manually install plugins that haven’t made it to the big-timeyet, including ones you write yourself. Happily, the plugin system isPython!
Installing Package Control¶
Note
Some earlier versions of Sublime Text came with the package control systemalready installed. This is no longer the case. You’ll need to install ityourself. Follow the instructions athttps://sublime.wbond.net/installation.
To install a plugin using Package Control, open the commandpalette withshift-super-P (ctrl-shift-P on Windows/Linux). The super key is commandor ⌘ on OS X. When the palette opens, typing install will bring up thePackageControl:InstallPackage command. Hit enter to select it.
After you select the command, Sublime Text fetches an updated list of packagesfrom the network. It might take a second or two for the list to appear. When itdoes, start to type the name of the package you want. Sublime Text filters thelist and shows you what you want to see. To install a plugin, select it withthe mouse, or use arrow keys to navigate the list and hit enter when yourplugin is highlighted.
Useful Plugins¶
Here are the plugins I’ve installed to achieve the requirements above.
Autocompletion¶
By default, Sublime Text will index symbols in open files and projects, butthat doesn’t cover installed python packages that may be part of a non-standardrun environment.
There are two to choose from:
Sublime Anaconda Python Interpreter
- SublimeCodeIntel offers strong support for multiple languages throughit’s own plugin system. It is a bit heavy and requires building an index.
- SublimeJedi only supports Python, but is faster and keeps an index on itsown.
I’ve installed SublimeJedi, and used settings similar to thesefor each projectto ensure that all relevant code is found:
The python_interpreter_path allows me to indicate which Python executableshould be introspected for symbol definitions.
The python_package_paths setting allows designating additional paths thatwill be searched for Python packages containing symbols. In the above case, Iam using buildout to manage installed packages, andthe omeletterecipe to provide a single folder in which all installed code can bereferenced. If you work with virtualenvor some other sandbox system, your value for python_package_paths will lookquite different.
Once configured, you should be able to use the ctrl-shift-G keyboardshortcut to jump directly to the definition of a symbol. You can also usealt-shift-F to find other usages of the same symbol elsewhere in your code.
Code Linting¶
Code linting shows you mistakes you’ve made in your source before you attemptto run the code. This saves time. Sublime Text has an available plugin for codelinters called SublimeLinter.
Python has a couple of great tools available for linting, the pep8 andpyflakes packages. Pep8 checks for style violations, lines too long,extra spaces and so on. Pyflakes checks for syntactic violations, likeusing a symbol that isn’t defined or importing a symbol you don’t use.
Another Python linting package, flake8 combines these two, and adds inmccabe, a tool to check the cyclomatic complexity of code you write. Thiscan be of great help in discovering methods and functions that could besimplified and thus made easier to understand and more testable.
![Interpreter Interpreter](/uploads/1/1/8/9/118945276/507321633.jpg)
There is a nice plugin for the SublimeLinter that utilizes flake8. For it towork, the plugin will need to have a Python executable that has the Pythontools it needs installed.
Use `virtualenv`_ to accomplish this. First, create a virtualenv and activateit:
Then use Python packaging tools to install the required packages:
The Python executable for this virtualenv now has the required packagesinstalled. You can look in /path/to/sublenv/bin to see the executablecommands for each:
(sublenv)$ ls sublenv/binactivate easy_install-2.7 pip2.7activate.csh flake8 pyflakesactivate.fish pep8 pythonactivate_this.py pip python2easy_install pip2 python2.7
Now install SublimeLinter and then SublimeLinter-flake8 using Package Control.
Here are the settings you can add to Preferences -> PackageSettings ->SublimeLinter -> Settings-User:
The paths key points to the path that contains the flake8 executablecommand.
The python_paths key points to the location of the python executable to beused.
The settings inside the flake8 object control the performance of thelinter. Read more about them here.
White Space Management¶
One of the issues highlighted by flake8 is trailing spaces. Sublime textprovides a setting that allows you to remove them every time you save a file:
Do not use this setting
Removing trailing whitespace by default causes a ton of noise in commits.
Keep commits for stylistic cleanup separate from those that make importantchanges to code.
Sublime Python Repl
The TrailingSpaces SublimeText plugin can help with this.
![Sublime Python Interpreter Sublime Python Interpreter](/uploads/1/1/8/9/118945276/626956638.png)
Here are the settings you can use:
This allows trimming whitespace on save, but only on lines you have directlymodified. You can still trim all whitespace manually and keep changesetsfree of noise.
Follow-Along¶
The final requirement for a reasonable IDE experience is to be able to follow adebugging session in the file where the code exists.
There is no plugin for SublimeText that supports this. But there is a Pythonpackage you can install into the virtualenv for each of your projects that doesit.
The package is called PDBSublimeTextSupport and its simple to install with pip:
Sublime Python Download
With that package installed in the Python that is used for your project, anybreakpoint you set will automatically pop to the surface in SublimeText. Andas you step through the code, you will see the current line in your SublimeText file move along with you.