⚡ 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.
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
:
Note that we also copied
the KoboReader.sqlite
file
(the input of our Python program)
from our Kobo eReader
to the same folder.
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
:
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:
and select the
File > Open command prompt > Open command prompt
menu:
you will get a new Command Prompt window, already located at the correct directory:
At this point, we are ready to run our program.
Type python export-kobo.py KoboReader.sqlite --list
and hit Enter/Return:
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:
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:
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:
If a -h
or --help
argument is given,
then a more verbose help message will be printed:
Congratulations, now you should be able to download and run a Python program on your own!
⚡ Lab Contents
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.
Make some changes in the code and observe the output.
MIT © Realocity