Tuesday, November 7, 2017

PYTHON ON WINDOW

Unlike the Linux and OSX operating system, windows doesn’t always come with python, we will have to download and install python and download and install a text editor.

Installing python

Before installing python, we need to verify that python does not exist on our system already by opening a command prompt by typing cmd in start menu. When the command window comes up, type “python” into it in lowercase.


C:\> python

If you get a python prompt (>>>), it means python is already installed on your system. If python is not installed, you will get an error message like the one below:

C:\> python

‘python’ is not recognized as an internal or external command, operable program or batch file

If the above error message is gotten, we need to download python installer for our system by visiting http://python.org/download/. This will bring up two download option, one for downloading python2 and the other for python3. Choose the python3 and the download should start automatically and the correct installer for your system will be downloaded. After downloading the installer, run it, make sure to check the option “Add python to PATH”, which will make it easier to configure our system correctly. Follow the prompt till the installation is completed. After the installation is completed, open the command window again and type the command python in it and the output should look like:

C:\> python

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> 

If this should work, it means we have python installed on our system. If it doesn’t work still saying “‘python’ is not recognized as an internal or external command, operable program or batch file”

if this happen, we need to tell window how to find the python we just installed. The system python command is usually saved directly on our C drive. Open explorer and open the drive C. look out for a folder name python. Inside the python folder, look for a file name python. Copy the full path of the location and paste it into the command prompt like this:

C:\> C:\python\python

Note: this is the path gotten from my own system. Your system path may differ from my own system. Press the return key and you would see something like this:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> 

If this worked, we now know how to access python on our system.

Running python in a terminal session

Open the command prompt and type python in it to open the python command interpreter and type the following into it :

>>>print (“Hello World!”)

Hello World

>>> 

Anytime we want to run snippets of python code, we open a command window and start a python terminal session. To close the python command interpreter, press CTRL+Z or type exit() and this will return you to the main command prompt.

Installing out text editor.

Visual studio code is a simple text editor to use as our text editor on window.it is easy to install and use. It has series of features which make it more reliable and flexible to use. It has syntax highlighting which makes our code easier to read and understand. It can be downloaded from http://code.visualstudio.com/, select the download button and the installer will be downloaded. After downloading, run the installer and make sure to check the box that says “Add ‘open with code’ action to windows explorer file context menu” and also “Add ‘open with code’ action to windows directory file context menu”. These features allow you to open the VScode in any directory currently opened. After the installation is complete, we are good to go.

Configuring the VScode.

VS code works with various programming language and various extension can be downloaded into the VS code. Open the VS code, in the side menu bar, there are about five menu there which are labelled: Explorer, Search, Source Control, Debug and Extension. Make sure you are connected to the internet and click on the Extension, and in the search box provided, search for python compiler and click install to install the python compiler into the VS code.

After completion, we can run python code directly from the VS code rather than going to the command prompt. Before we proceed, let create a folder where we will be saving our works. We need to make sure that our works are saved into the same directory where python is installed so as to make it easier for our computer to know the directory where the python compiler is located. Let name the folder as python_practise in our python folder located in drive C.

Running the Hello World

After creating the folder, open the folder and right-click anywhere in the folder and select the option “open with code”, this open the current directory in VS code and any work done will be save directly into the python_practise folder. On the welcome page of the VS code, select new file and save an empty file as hello_world.py. Don’t forget to include the extension .py which tells our interpreter that we are working with python file. Type the following into the editor:

print (“Hello World”)

save the program, click on the run code button which is located on the right of the title bar or press CTRL+ALT+N to run our code. On clicking the run button, just below the editor, there will appear the command interpreter and it will look like the following:

[Running] python "c:\python\python_practise\hello_world.py"

Hello World!



[Done] exited with code=0 in 0.138 seconds

This means that our program run successfully. We can also run our program from the command prompt by going into out python_practise folder and hold down the SHIFT key and right-click. Click on “open command window here”, when the command window come up, type the following

C:\python\python_practise> python hello_world.py

Hello World!

The output for the code will be display directly under the command. This is how to run our python code from the VS code and from the command line.



In our next section, we will be talking on variables and our coding class would be staring fully.

Don’t forget to drop us comment or ask any question and we will be glad to attend to your question.

Thank you for reading.
Share:

0 comments:

Post a Comment