Scripting and testing
Introduction to Python scripting
Please do not copy without permission. © ExploreAI 2024.
Scripting and testing
An introduction to Python scripting
| Python scripting is often used to automate a specific series of tasks, leading to a more efficient
process. Python scripts can be used for:
● Facilitating data analysis and visualisation. ● Machine learning and artificial intelligence.
● Developing both desktop and web applications. ● Business applications.
● The development of complex scientific and
numeric applications.
Running Python in Jupyter notebooks is not the only way to interact with code. We can run scripts through IDEs Integrated
Development Environments) such as PyCharm or VS Code or even through the command line. Through the rest of this train we will
expand on the different tools used for compiling and executing Python scripts.
2
Scripting and testing
Tools used to compile and execute Python scripts
Integrated Development Environment
Command line Jupyter Notebook
IDE
There are various reasons why we An IDE is an application that can be Jupyter Notebook is an open-source
might wish to utilise Python in a used to write code in a script format. web application that allows us to
command line. create and share documents that
Scripts enable us to write much contain code, equations,
These may include: lengthier code, which can then be visualisations and narrative text.
executed in a command terminal or
● We want to conduct quick imported into a Jupyter Notebook. Applications include:
one-liner computations.
● We want to execute Python IDEs facilitate this process. ● Data exploration
scripts using parsed Examples of IDEs include VS Code ● Machine learning
arguments. and PyCharm. ● Aiding in explanation and
● We want to run simple code. interactivity of code
3
Scripting and testing
Using Python in a command terminal on Windows
Letʼs see how we can use Python in a command
02.
terminal on Windows:
01. Click the Start button.
02. From the menu, search for “Anaconda promptˮ and select it.
03. Enter “Pythonˮ at the blinking cursor and hit Enter. This will
start an instance of Python.
03
.
01.
Scripting and testing
Using Python in command terminal on Mac
Letʼs see how we can use Python in a command
terminal on Mac:
01. In Finder, navigate to Applications > Utilities > [Link].
02. Start a Python instance by typing Python at the blinking
cursor and hit Enter.
02.
We can now run Python commands in the terminal. Since Python
03
is a universal language, the commands will be the same whether .
we are using a Mac or Windows O.S. 04
We can input code at each line starting with >>>. The output of .
the code is displayed below this line. 05.
03. Simple addition
04. List comprehension
05. Setting variables
Scripting and testing
What are Python scripts?
| Python scripts allow us to write much longer sections of code that can then be executed in a
command line or imported to a Jupyter notebook.
Though we can use a Notepad or a word processor to write our
Python scripts, itʼs much more advisable to use an IDE. These
come with a number of features that make writing and executing
scripts far easier.
There are a number of IDEs available for free, including:
● PyCharm (community edition is free)
● VS Code
● Spyder
None of these are perfect. Each comes with its own advantages
and disadvantages. As a growing data scientist, we each have
our own preferences. For the purpose of this tutorial, weʼll be
using VS Code.
Scripting and testing
Installing Visual Studio Code on Windows
| Follow the links and instructions below to install VS Code on Windows.
03
.
01. Install the Windows version of VS Code.
02. Download and install the Python extension for VS Code.
03. Open VS Code. Click ‘New fileʼ or add an existing workspace.
04. Start coding!
02.
01.
7
Scripting and testing
Installing Visual Studio Code on Mac
| Follow the links and instructions below to install VS Code on Windows.
01. Install the Mac version of VS Code. 04
02. Locate the downloaded archive file. Move the visual studio .
[Link] file to the application folder.
03. Download and install the Python extension for VS Code. Be
sure to take note of the Python requirements!
04. Open VS Code. Click ‘New fileʼ or add an existing workspace.
05. Start coding!
03
.
01.
8
Scripting and testing
Writing our first Python script
| Weʼre now ready to write our first Python script. We can use a text editor or IDE to write our
script. Letʼs have a look at the process of writing a script.
01. Open a text editor or IDE of choice; for this train we will be 01.
using VS Code.
02. Navigate to File > New File.
03. To be able to execute our new script we will have to save our
file with a .py extension.
03
.
02.
9
Scripting and testing
Executing a Python script on Windows
To execute our Python script:
01.
01. Open a new instance of an anaconda prompt.
02. Navigate to the path where we saved our script.
To do this: 02.
● Take note of the path where the file is saved in VS Code.
● Convert the ‘>ʼ symbols to ‘\ʼ symbols.
● Therefore C:>Users>User>Documents> becomes
C:\Users\User\Documents\.
03. Next, we type the path from step 2 into our anaconda 03
.
prompt as cd <your path here> which in our case becomes
cd C:\Users\User\Documents.
04. We can now execute our script by running
python my_first_script.py. The output should be the printed
word ‘barʼ. 04
.
Scripting and testing
Executing a Python script on Windows
To execute our Python script:
01. Open a new Terminal window.
02.
02. Copy the path to the scriptʼs .py file.
A shortcut: Right-click on the file while holding down the Option
key and select “Copy … as Pathnameˮ to copy the path to the
clipboard.
03. In the Terminal window, type cd and then paste the path that
we copied in step 2.
03
04. We can now execute the script by typing . 04
python my_first_script.py and hitting Enter. The output .
should be the printed word ‘barʼ.
Scripting and testing
Importing a Python script into a Jupyter notebook
Letʼs say weʼve written a very useful Python script that
weʼd like to run in a Jupyter notebook.
We can run it by doing the following:
01. Open a command prompt in the same directory as the script
and type Jupyter notebook, then hit Enter. This will open an
instance of Jupyter in our browser.
01.
02. Start a new notebook. In the first cell of the new notebook,
type import my_first_script and execute the cell by hitting
“Shift+Enterˮ.
03. We can now access the foo function in this script by typing
my_first_script.foo(). Executing this in a new cell will print
the word ‘barʼ.
Scripting and testing
Tools used to compile and execute Python script
Jupyter Notebook Visual Studio Code IDE Command line
Medium scale coding operations. About
Large-scale coding operations. Small-scale coding operations.
100 to 200 lines of code as longer-length
Can cover thousands of lines of code. Usually only a couple of lines.
notebooks become unmanageable.
Can import Python scripts and execute
Can import Python scripts. Can import other Python scripts.
Python scripts.
Not recommended for a production Can form the backbone of a production Not used in production outside of
pipeline. pipeline. debugging and monitoring.
Useful for executing Python scripts and
Useful for presenting and exploring data. Useful for writing large amounts of code.
debugging.
Can execute cells in any order. Always executes from top to bottom. Always executes from top to bottom.
Does not take parsed arguments. Can work with parsed arguments. Can parse arguments to a script.
13
Scripting and testing
Extra reading
Extending Python scripts with arguments:
Argument parsing
Argument parsing in Python
Python scripting:
Running Python scripts
Python scripts
Visual Code Tips and Tricks:
Visual Code tips and tricks
Python scripts vs modules:
Python modules vs scripts
Reusing code