Getting Started With Python

Getting Started With Python

Check out the first article "Getting Started With Python" in our "How To Use Python To Automate Almost Anything" by Josh Grant

By Josh Grant

You Picked Python: Congratulations!

You've decided to try automating some of your tasks. Awesome! Good automation can help free up time and computing resources, and make tasks clean and uniform. It can sometimes even help do things that are otherwise impossible. For modern software testers, having familiarity with automation approaches and tools is almost essential. Using automation well can make testing efficient and even more enjoyable.

I think Python is a great choice for simple automation tasks.

Why use Python?

Pros Of Using Python

It's Cross-Platform: Python works well on Microsoft Windows, macOS and most mainstream versions of Linux such as CentOS, Ubuntu and Debian. Simple scripts can be written, which are perfectly portable from one operating system or environment to another. This means that using Python doesn't bind testers to a particular environment. This can be helpful for using automation in multi-platform settings that testers may encounter and work with regularly. It's also beneficial as Python scripts will work well on operating systems as they update from version to version because even as an OS changes, the Python scripts should still work.

It’s Fun To Write: For software testers, automation often, but not always, means working with a programming language. This means writing code. Programming language syntax comes in all shapes and sizes, all of which come with a particular philosophy. Python's underlying philosophy is to focus on simplicity, readability and being able to integrate various components together. Python has a syntax that makes scripts look uniform, even from author to author. Python scripts tend to make the intention of the script clear even with a casual glance.

It's A Mainstream Language With A Mature Ecosystem: While the language itself is important, the ecosystem around it is also helpful. This includes both the community aspect and tooling aspect. Python has a mature ecosystem, meaning that there are mature conventions and tools around how Python is used. Conventions include rules for writing "clean" code and common set-ups for Python projects. Python has first-class language tools such as a package manager for getting libraries for different utilities (called modules in Python), a full-featured interpreter, developer tools such as Integrated Development Environment (IDE) support, and so on. There's a wealth of knowledge available online and offline for Python that any tester can tap into if they need it.

Lots Of Cool Modules & Tools To Do Almost Anything: As a scripting language, Python has a large number of modules to help with a variety of tasks. This means that scripts can take advantage of existing functionality instead of writing lines of code from scratch. Modules that could be of interest for testers are processing HTMLreading and writing Excel filesgenerating random values and driving a web browser. Python also has more typical programming features like text manipulation and reading and writing to text files. With Python, it really is possible to automate almost any task on a typical computer.

Cons Of Using Python

There are some downsides to be aware of as well, such as:

You Need An Interpreter To Run Scripts On A Machine: Python is an interpreted language, meaning that scripts get run through a Python interpreter. An interpreter is a program that takes a Python script as input and executes the script as output. This has some upsides, but I’ve also seen this be a bit of a downside, namely because it restricts where a script can be run. Basically, to use Python you need to install a Python interpreter on a machine, which can be a pain point when using Python on teams. There are solutions to this problem, but as a first step it is good to consider where you need to install the Python interpreter to use Python effectively.

Python 2 vs Python 3: Like other software, Python has major and minor releases periodically to add new features and fix bugs. The latest major version of Python is 3, which was released in December 2008. This release included breaking changes from the previous major version, Python 2. Some libraries and utilities that work with Python 3 may not fully work with Python 2, and vice versa, due to these breaking changes between versions.

Even taking these downsides into account, Python is still one of my favourite tools for automating tasks. Being a mainstream language means that I can usually find libraries and modules to do almost exactly what I need to do, with some basic Internet searching. I’ve Python scripts for manipulating PDFs and Excel files, generating unit tests templates based on a list of file names, and to visit and screenshot a large collection of websites. To me, the diversity of tasks possible and how easy you can combine modules is the best selling point of Python.

It’s a language that mostly gets out of the way and lets me accomplish what I want without a lot of overhead. As a tester, that’s a big plus when trying to automate tasks to help with your work. Based on all this, how could any tester not want to automate things with Python? Let's get started!

Python Setup

Now that we've seen some of the nice features Python has, let's go through setting up Python for usage. As mentioned previously, Python is a cross-platform language and is available on Windows, Mac and Linux operating systems. These sections will walk through getting Python setup on common operating systems. The rest of this article will be based on the latest major version of Python (3.6 at the time of writing).

Linux & macOS

Both Linux and macOS include versions of Python by default on recent versions. You can check by opening a terminal typing 'python --version' and hitting enter. This should tell you the Python version installed on the system or throw an error.

Note: A terminal is a program on Linux or macOS that creates a special environment for working with a computer. It creates a command line interface for interacting with an OS. Terminals allow you to call programs by typing in their names with extra parameters. It is common to use terminals for working with programming languages like Python.

On most Linux systems you can open a terminal by pressing the Control+T keys at the same time. On macOS you can open a terminal by pressing Command+T.  You can open several terminals at once if you wish.

If Python is missing on your Linux operating system, you can usually install it using your operating system's package manager. For example, Ubuntu uses apt-get and Python can be installed by typing 'apt-get install python' and hitting enter (you may need to use 'sudo' here based on your operating system permissions). If you use another Linux distribution, check your package manager to see if Python is included.  

Note: On Linux systems, sudo is a command that can be used by non-administrator users to execute commands that require administrator-like permissions. One example of an operation that typically requires a sudo before a command is installing new software, which is what we are doing here. After calling a command with sudo you may be prompted to enter your login password, which will then allow the command to be executed.

If Python is missing on your macOS, or you need to update it, you can use Homebrew to install Python. You can use homebrew by opening a terminal, typing 'brew install python3' and hitting enter.


Typically, Windows operating systems do not include Python by default. Luckily, installation is straightforward. Find the latest version of Python and download the installer. All major versions of Python have full Windows installers that configure Python for you.

What Comes With Python

Installing Python installs several components. The central component is the Python interpreter. This is called by typing 'python' into a terminal and hitting enter. When called like this with no arguments, it starts a Python REPL that allows Python commands to be called in an isolated and safe way. This is a great way to explore Python commands. When called with a Python script in the form 'python /path/to/' the Python interpreter tries to execute the contents of the script. This is the other common usage of the Python interpreter.

The other tool that is installed with Python is its package manager, called Pip. Pip manages Python modules, allowing for a whole variety of tools to be installed. The usual way of installing Python modules using Pip is to call 'pip install --update MODULE_NAME'. This will install the module with the given name, if it’s not already installed, or update the module to the latest version if it’s already installed. More information on Pip can be found on the official documentation pages if you're interested beyond the basic usage.

Along with modules installed using Pip, Python comes with some standard modules. These are mainly based on common programming usage, such as working with file systems (the os and sys modules).

With Python set up, the next step is to think of a good scripting project. Let's see what we can do!

A Basic Example: Hello, World!

Let’s ease into using Python, starting with a basic “hello, world!” script. This is a script that prints the phrase “hello, world!” to the screen. This is the first program many people write in a new language to see how that language works.

To get started, open a terminal and run the command 'python' to start the REPL and then wait for the >>> prompt. Python’s “hello, world!” command looks like this:

    print(“hello, world!”)

Typing that at the prompt and hitting Enter prints out “hello, world!”. That’s it, you’ve written your first line of Python! You may call other commands, or type 'quit()' and press Enter will exit the Python REPL.

Similarly, you can also run the same commands as a script. Create a new file called '' and add the single line of

   print(“hello, world!”)

and save. To run the script, type 'python' and hit Enter. You should see the line “hello, world!” on your terminal. Congratulations, you’ve written your first Python script!

This may seem trivial but “hello, world!” does show some of the basic mechanics that will carry into our more complex examples later in this series.

Next Steps

We’ve seen a basic overview of what Python can do and how it works. Getting a basic script under our belts means we can proceed onto more complex - and more interesting! - uses of Python.

As an example, I’ll walk through a script that automates taking screenshots of URLs through a browser. This script will open a browser, iterate through a list of URLs and take a screenshot of each one. It’s a good example of a task testers may need to do that can show off many of Python’s capabilities. It may also give some inspiration for what’s possible to automate with Python. Let’s take a step towards better testing through automation!


Author Bio:

Josh Grant is interested in automation, software development and big ideas. He’s currently working as a test developer doing cool automation work in Toronto, Canada. You can find him active on Twitter.

Share this article: