Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Realocity/Python-Practicals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python-Practicals

Lab Practice - Implementation of Python Programs

We love Programming. Our aim with this course is to create a love for Programming.

⚡ Running A Python Program

Excellent, now you have all the tools required to run a Python program on the command line.

As an illustration, I will use my simple Python script export-kobo, which reads annotations and highlights from the database file of a Kobo eReader device (KoboReader.sqlite), and prints them on the prompt or exports them to an output file.

Step 1: Download The Source Code

First, download the source code of the Python program you want to run.

This usually implies downloading either a single Python source code file (with extension .py), or a ZIP file containing several Python source code files and other resource files that you need to uncompress somewhere on your disk.

The exact details depend on your Python program, hence be sure to carefully read its install documentation. You can download the prescribed files with your browser, and then copy/uncompress them using the Windows graphical file manager.

In our example, we download the raw file export-kobo.py from the GitHub repository.

Remember Safety Rule 3 ("copy your Python files into a separate folder")?

We put the downloaded export-kobo.py file in a new folder C:\export-kobo:

Running a Python program

Note that we also copied the KoboReader.sqlite file (the input of our Python program) from our Kobo eReader to the same folder.

Step 2: Open A Command Prompt And cd There

Then, open a Command Prompt as explained above, and change the current working directory to the folder where you put your Python program source files.

In our example, cd C:\export-kobo:

Running a Python program

A simpler alternative to using the cd command takes advantages of the Windows file explorer. Just navigate the file explorer to the folder where your Python code is:

Opening a prompt from file explorer

and select the File > Open command prompt > Open command prompt menu:

Opening a prompt from file explorer

you will get a new Command Prompt window, already located at the correct directory:

Opening a prompt from file explorer

Step 3: Run The Python Program

At this point, we are ready to run our program.

Type python export-kobo.py KoboReader.sqlite --list and hit Enter/Return:

Running a Python program

The Python interpreter will load our export-kobo.py program, and run it with arguments KoboReader.sqlite and --list.

Clearly, the semantics of the arguments vary from program to program, depending on what each program is supposed to do.

In our case, export-kobo.py will read the file whose name is passed as the first parameter (KoboReader.sqlite) and it will list (--list option) the titles of all the eBooks with annotations or highlights in the database.

If we specify different command arguments, for example python export-kobo.py KoboReader.sqlite --csv --output exported.csv, we will get a different behavior:

Running a Python program

In particular, this second command exported all the information contained in the KoboReader.sqlite file into the newly created file named exported.csv in CSV format:

Running a Python program

You must check the documentation of your Python program to know the semantics of its arguments.

Usually, if you run a Python program without arguments you will get a synopsis of the accepted arguments:

Running without arguments shows the synopsis

If a -h or --help argument is given, then a more verbose help message will be printed:

Running with -h shows an help message

Congratulations, now you should be able to download and run a Python program on your own!

⚡ Lab Contents

Usage

How do I run a Python script that I downloaded from GitHub? Download python installer from their official website here. After installing, open the folder where you have downloaded the script, press and hold shift and right click anywhere in the folder, in the popup there will be a option to open command prompt here (if you are using windows 8.1 or lower) or open powershell here (if you are using windows 10) click on it, a black window will appear, paste the below command in that where filename.py is name of the python file you have downloaded.

python filename.py

And now press enter. Voila your python script will now run.

Intro User Image

Interesting things to try

Make some changes in the code and observe the output.

Built by (A-Z)

License

MIT © Realocity

Releases

No releases published

Packages

No packages published

Languages