Monday, November 6, 2017

PYTHON ON OS X

Just like the Linux operating system, python is already installed on most OS X systems. We need to check whether it is installed or not. And when we are certain that python is installed, we’ll need to install a text editor and ensure to configure it correctly. Their are different types of text editor in the net, you just have to select any one of your choice to work with.
Checking the version of python installed on our OS X

In order to check whether python is installed on our OS X, we open a terminal window. After opening the terminal window, we type python in lowercase.
We should see an output that looks like the one below, telling us the version of python installed on it.
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on classicalAitem
Type "help", "copyright", "credits", or "license" for more information.
>>> 
The above output tells us that python 2.7.5 is currently installed on the computer. We can press CTRL + D or type exit() to exit the python interpreter and go back to the normal terminal window.
To check if we have python 3 installed on our system, we follow the same convention as that describe in Python on Linux and type python3 in the terminal window.
You may get an error message but if the output indicated that python3 existed on our system, we are good to go. That means we’ll be able to use python3 without the need to install it. Throughout our lectures, whenever you come across python to run your code, endeavor to use python3.
Running python in a terminal window
We can run snippets of python code in the terminal window of OS X just like we did on the Linux terminal window. Open a terminal and type python (if you are using python3, use python3 instead). This command should open up the python interpreter. Enter the following line of code:
>>> print("Hello World!")
Hello World!
>>> 
The message should be displayed directly after the line containing the code. Exit the python interpreter by pressing CTRL+D or type exit().
Choosing and installing a text editor.
There are numerous text editor that can be used for our work but I’ll suggest the use of Sublime Text which is a simple text editor. On Linux, we made use of Geany, on OS X, we are going to be using Sublime Text. It is quite easy to install and allows the running of code directly from the editor rather than going to run our code from the terminal window. It has an embedded terminal session in its windows which make it easy to see the output of our codes.
Sublime Text can be downloaded from http://sublimetext.com/3, click on the download link and look for an installer of OS X. After downloading the installer, open it and drag the sublime text icon into the application folder.
Configuring the sublime text for python3
If we are using a command other than python (say python3) to open our python command interpreter, we’ll need to configure sublime text so that it can know where to find python on our system. Use the command below in a python interpreter to see the full path of where python3 is installed
$ type -a python3
python3 is /usr/local/bin/python3
Now let configure our sublime text editor, open sublime text and go to Tools->Build System->New System Build, this will open up a new configuration file. Delete whatever you see and set it to:
{
. "cmd": ["/usr/local/bin/python3", "-u", "$file"],
}
The code above gives a directive to the sublime text to use python3 command when running a currently opened file. Ensure to use the path indicated in the output of using the command type –a python3. Save the file as python3.sublime-build in the default directory that sublime text opens when you select save.
Running the Hello World! Program
Before we begin, let create a folder where we will be saving our works somewhere on our system. Let name it python_practise.
Save an empty python file by clicking File->Save As and name the file as hello_world.py, make sure to navigate to the earlier created folder python_practise. Don’t forget to use the extension .py which indicate that we are dealing with python files. After saving our work, type the following
print("Hello world!")
if the command python works on your computer, you can run the program by selecting Tools->Build in the menu bar.
But if the you have configured sublime text to use python3, as described above, select Tools->Build Systems and select python3. This will set python3 as the default. Now select Tools->Build or press command+B to run our program henceforth.
A terminal window should come up at the bottom of our sublime text window and display:
Hello world!
[Finished in 0.1s]
If you do not see the above output, check your code very well to ensure that is matched the way we had typed it.
That is all about the configuration and installation of python3 on our OSX.

Stay updated with us to keep learning more on python. Don’t forget to subscribe to our newsletter and drop us a comment when you are having doubts so we can help you further.

The next episode is definitely what most of our viewers have been waiting for, watch out for the python on windows in the next episode.
Thank you for reading.
Share:

0 comments:

Post a Comment