Executing Python Scripts from the Command Line

Or: how to pass a job interview

Python
Data Science Applications
Author

Gio Circo, Ph.D.

Published

September 20, 2024

Code Tests

Here’s a short one for a Friday afternoon.

At my day job we have a pretty simple coding test for first-round interviews for data scientists. I think we are actually pretty realistic and reasonable with what we ask experienced people to do in an interview. We don’t play around with leetcode or hackerrank stuff. We’re generally just looking for people who have a solid statistics and data science background, and who can demonstrate programming prowess. On our team many of us are expected to be “full-stack” (or nearly so), where we handle the development of models and deployment from the ground up.

We have a few questions, but one of the ones that seems to trip up a lot of people who claim experience with python is phrased something like this:

“Please create a python script to print ‘Hello World’ and execute it from the command line”

Amazingly, about half of candidates who have big resumes can’t do this! Here’s a tweet from a team member:

Unfortunately, a lot of this is due to how folks are instructed into data science. Lots of programs focus on highly containerized examples where everything is handled within a single notebook. Notebooks can be great for experimenting, but when it comes to model deployment you will need to learn how to set up environments, manage dependencies, etc…

The Question

So how do we go about answering this question? Well, I can break it down quite simply:

Step 1

First I will assume you have python installed, and it is on your PATH (see a StackExchange post here if you are not familiar). This is necessary so you can call python directly from the command line. When you’re installing python for the first time you typically can just click an option to add python to the PATH variables on Windows.

Step 2

Now we just need to write the file and save it somewhere:

print("Hello World")

You can literally just open a text file, add the code above, and then save the text file with a .py extension. I just saved it as demo.py.

Here is our lonely ‘Hello World’ file

Step 3

Finally, we just need to execute the python file. Book up command prompt (can click the little window icon and type cmd to get it to pop up). Just navigate to where ever the file is (or call the full file path directly) and append python in front of it.

Voila! We’ve passed the first question!

So What?

I know this is a bit of an “old man yells at cloud” kind of post, but I do have a deeper point here. Back when I was teaching stats as a professor many, many of my students did not know how file paths worked, how to navigate through directories, or even how to find a file that they downloaded off the class page! I don’t blame them at all - moreso I blame how most children and teens interact with computers - which is almost exclusively through apps and things like Chromebooks. They certainly make the user experience easy, but leave you with little understanding of how these tools actually work. For most people who don’t need to work in-depth with computers, it might be a huge issue. However, when we have data scientists who are flaunting 4-5 years of python experience and can’t execute a file using command prompt, it makes me a bit more concerned.

Personally, I think a lot of these “bootcamps” that promise to turn you into a fully-fledged data scientist in 12 weeks don’t really give you much more information than how to run off-the-shelf models in a notebook. Definitely good for a start, but woefully inadequate for real-world work. As a self-taught programmer I would highly encourage people interested in data-focused jobs to expand their horizons and broaden their skills.

Incidentally, my friend Andy Wheeler has a book which is a really great into to python on his personal website here. If you are at all interested in dipping your toes into python (especially if you are a crime analyst) I would highly reccomend it.