Installing Python On Sublime Text Editor 3
Sublime text give you the power to work on almost all programming language of your choice. However, there may be some programming languages that might not be pre-installed and hence you have to explicitly add them on the fly. One of them is python. Within this tutorial we will look into
- how to install package controller in sublime text editor
- how to install and configure python in sublime text editor
- writing our first hello world program
Install package controller in sublime text editor 3
First open console on sublime text editor 3 using VIEW > SHOW CONSOLE
Next paste the following code in text editor and press enter. Restart sublime text editor.
import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
Next, to install packages click CTRL + SHIFT + P and start typing install and select Package Control: install package. Press enter and search for available package. You can remove, update package in this manner.
Complete details about package controller can be found here https://packagecontrol.io/docs/usage
Install and configure python in sublime text editor 3
Now we have the package controller installed, we will use the package controller to install other packages, plugins and themes. To get python working on you system you need to install python first, you can download python via command line or installer.
To install python via command line use the following command :
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
To download and install python via installer click on this link, which redirects you official python page and you can download python version of your choice.
Once we have the python installed we need store its path. For ubuntu we will use command line First type python and hit enter. If you are able to successfully enter python command line, it means you have successfully installed python on you system, copy the version number from there.
Next type whereis python 3.6 to get the location of python. Copy the first location from there and that is your path of python. Store it because we will need it further.
Now we have the location of python, we have previously installed package controller, now lets integrate python with sublime text editor.
Open package controller by typing CTRL + SHIFT + P type install till it shows Package Controller : Install Package. Next type SublimeREPL and click enter, it will install sublimeREPL on you sublime text editor. Now restart sublime text editor to ensure all changes have taken place.
Next go to TOOLS -> BUILD SYSTEM -> NEW BUILD SYSTEM and enter the following json to link python with sublime text editor 3
"cmd": ["/usr/bin/python3", "$file"],
"file_regex": "file \"(...*?)\", line([0-9]+)"
Now save the file as SublimeREPL-python.sublime-build and restart sublime text editor to ensure all the changes have taken place.
Next, go to TOOLS -> BUILD SYSTEM -> SublimeREPL-python. Once appropriate build system has been selected, create a new file and type print("hello world") and press CTRL + B to build the system.
If all the above steps has been installed successfully you should be able to see hello world on the output screen ensuring that python is running successfully with sublime text.
That's all for now! We were able to install package controller on sublime text editor, understood how to install packages/plugins on sublime text editor 3. Installed python on the system and linked python with sublime text. Moreover, just to confirm we even wrote a small hello world program in python.
In case of any doubts you can reach me out at [email protected], you'll get a follow up within the next 24 hours regarding the same.