Getting started with Python on Linux

Like it? Share it!

Code segments FIXED! You can now copy and paste the code displayed on this page without the line numbers 🙂

So I recently started undertaking an online uni course called Programming for everybody, offered on coursera by the University of Michigan. One of the first extra credit assignments is installing python onto your computer for use throughout the course. Now I’m no stranger to Linux or PPA’s but since I have to prove I can install it I thought I may as well create a little guide for everyone who is interested.

If you are reading this I will assume a few things, firstly that you have linux installed (Preferably a debian based distro) and that you know how to access the terminal. Other than that you should be fine!

Step 1 – Installing Python

The first step is probably the python_terminaleasiest! As most distros come with Python already installed which is great. Also I recommend Python 2.7 however you can install both.

The easiest way to test is to simply type python into the terminal:

If everything is installed properly you should see your terminal change into the python prompt like this:

If it doesn’t work you can simply install python via PPA (apt-get) with the following command:

sudo apt-get install python

Congratulations! You now have python installed, test it out by doing the classic hello world! Remember the terminal program is just an interpreter and reads code one line at a time. If you want to exit at anytime simply type exit()

 

Step 2 – Installing Sublime Text 3

I only discovered Sublime a few months ago and as a Web designer and enthusiast programmer let me tell you it’s amazing! FYI if any web devs are reading this you may also want to check out Brackets. However as far as Python goes I highly recommend Sublime over full fledged IDE’s such as PyCharm ,etc.

What makes Sublime text so great as that you can build / compile your python code directly from within Sublime without have to save your .py file and then run it through the interpreter at the terminal.

Firstly I’m going to show you what I believe to be the easiest way to install Sublime Text 3, as before it’s via PPA’s (apt-get).

Add the webup8team repository:

sudo add-apt-repository ppa:webupd8team/sublime-text-3

Update your PPA cache:

sudo apt-get update

Finally install Sublime Text!

sudo apt-get install sublime-text-installer

Just follow the prompts in the terminal, it’s as simply as pressing Y when asked. Believe me when I say this is much easier than building from source and the beauty of using the package manager is that it will keep your programs up to date too!

 

Step 3 – Test it all out (run your script from within sublime)

first_program1At this point you have everything you need to start writing your own python code! As mentioned before sublime is great because you can execute your code from within sublime itself and without having to launch the interpreter or use a bulky IDE.

For now just do a simple Hello World. Now when you want to test out your code just goto Tools > Build (or press Ctrl+B) Make sure you save your script first with the .py extension and Sublime will automatically set the build engine to Python. As well as correct syntax highlighting (notice how print is in pink, otherwise you can do it manually under Tools > Build System > Python. As you can seen from the screenshot, the executed code appears down the bottom of the text editor. Please note that this is for basic code and I still recommend the regular Python interpreter via the terminal for full fledged/large Python scripts.

 

Step 4 – Install Sublime Package Control (Optional)

sublime_packing_installThe easiest way to install package control is via the sublime text console. Press Ctrl + `
or goto View > Show Console

Then copy and paste the following code, Please Note: This code changes upon update of package control, check the website for the current code to use (https://packagecontrol.io/installation)

 import urllib.request,os,hashlib; h = 'eb2297e1a458f27d836c04bb0cbaf282' + 'd0e7a3098092775ccb37ca9d6b2e4b7d'; 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) 

If all is well then Packing install should well… install. To check press Ctrl + Shift + P and a little windows with commands will popup as pictured here

Step 5 – Running your code from within Sublime

Sublime text is great on it’s own but there are a few plugins I am going to show you how to make it great! Firstly, remember how I said the build command from the menu was very limited. Most notably is when you use raw_input you get an error. So we fix that with SublimeREPL, basically this can open a new tab with a full Python console allowing you run your script! Open the Command Palette with Ctrl + Shift + P or goto  Tools > Command Palette. Just start typing Install and look for Install Package in the list. This will bring up another list and search for SublimeREPL and press enter to install it, you will see some actions going on in the sublime console at the bottom of the window. Once it is done you will have a new addition to the Tools menu. Under SublimeREPL look for Python and run your code by selecting Python – Current file.

{ "keys": ["ctrl+alt+b"], "command": "run_existing_window_command", "args":
{
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
}}

 

So stay tuned for much more coming very soon!

If you like my guide please share it around and give me some cred @Ompster

This article was viewed (292) times!

Like it? Share it!

Comments

comments