The world’s leading publication for data science, AI, and ML professionals.

Creating a data scientists tool belt.

How to get your Python program out of the IDE and into your command line

Photo by jesse orrico on Unsplash
Photo by jesse orrico on Unsplash

TL;DR all the necessary steps are in a code snippet at the bottom.

As a student, I see many of my classmates create impressive tools that I want to incorporate into my workflow. But these functions are relegated to either Jupyter Notebooks or a collection of code snippets shared over Slack.

The goal of this article is to give you a working knowledge of how to make a script executable from anywhere on your mac. This tutorial is going to touch on many different topics. In order to keep it short, I will not be going into detail on any of the steps. I will include links for further reading for those curious.

In addition, I use z-shell and a mac. All of these steps will work if you are using either z-shell or bash (replace zshrc with bashrc).

Tutorial Outline:

1) Check you have python3 and that it is up to date

2 ) Create a simple example program to print to stdout

3 ) Learn how to make your script executable

4 ) Add the script to a new source path so it is callable in any directory

1) Check you have python3 and that it is up to date

If are new to computer science like me, you may not be aware of where you have installed Python3, or if you have it installed at all. Let’s make sure you have it in the correct directory. This article gives an overview of what we are about to do if you are hesitant.

All of the following steps are going to occur inside your terminal.

Open your terminal and let's get coding!
Open your terminal and let’s get coding!

Let’s check that you have python3 installed. To do this, we are going to call the which function. which will search your PATH variable for the location of python3.

which python3

If you see something similar to:

something/something/python3

You can skip the rest of this step and continue to making your first Command Line tool.

However, if you see python3 not found you will need to follow the steps outlined [here](https://www.python.org/downloads/). I recommend doing a brew install python3 but you can also download the installer here if you are less comfortable with homebrew.

repeat the above steps once you have installed python3 and you should have python3 installed and your PATH variable will now be pointing to the correct directory.

Let’s crack on.

2) Create a simple program to print to stdout

Use your favorite text editor or IDE (I’ll use VIM) to create a simple "script.py". Things have been particularly bleak in 2020 for us in the USA. To pick myself up, I’ll make a simple command line program that encourages the user! Let’s just call it encourage.py. It will 1 argument, the users name and print out a personalized note.

vim encourage.py

Currently, this is a Python script and we can only run it using the function Python3.

python3 encourage.py tim
You're doing great, Tim!

You may already know about aliasing in your shell, and we can go that route by appending an alias command to our shell resource file (zshrc). Note, if you don’t have a ~/.zshrc file, this will create one for you:

echo "alias py='python3'" >> ~/.zshrc

Great, now we can simply call:

py encourage.py tim
You're doing great, Tim!

But, this isn’t what we want. We can do better and remove the ".py" from our file. The ".py" is only important for syntax highlighting in your text editor or IDE. It doesn’t need the file extension because you are giving the plain text file to the function, Python3. You can test this yourself!

# rename your function
mv encourage.py encourage
py encourage tim
You're doing great, Tim!

Great! Now we are on to dropping the "Python3" function altogether and making a fully-fleshed command line program.

3 ) Make your script executable

In the previous section, we learned that the name of our script acts as a command line argument when we call the Python3 function. The function Python3 doesn’t care if your script has a .py or not as it will accept any plain text file you give it. However, if we want to do away with calling Python altogether, then we are going to have to insert the shebang command #!/usr/bin/env python3 at the top of your script:

This tells your shell to interpret the file as Python code when it reads it. Your shell won’t let you execute files by default, so you have to set a permission flag. Run this command in your shell and it will change the permissions of the file so that it is executable.

chmod +x encourage
After running chmod +x encourage our file should now preview like this
After running chmod +x encourage our file should now preview like this

Now we can run our program with just:

./encourage tim
You're doing great, Tim!

How easy was that?

Photo by Sincerely Media on Unsplash
Photo by Sincerely Media on Unsplash

4 ) Add the script to our PATH variable so it is callable in any directory

cp, touch, ls and mkdir’s directory’s are all stored in the PATH variable

You’ll notice that you have to be in the same directory as the function in order to call it, but we want to be able to call our functions from anywhere. To achieve that, we have to update our PATH variable. The PATH variable is loaded into your shell and tells the shell where to look for command line functions. The locations of git, cd, ls and mkdir’s directory’s are all stored in the PATH variable. We have a brand new function encourage that we need to add to the PATH. Let’s first make a new directory in our user directory called bin and put a copy of our program there:

mkdir ~/.local/bin
cp encourage ~/.local/bin/encourage

We then need to append a command for our update our PATH variable in our shell resource file:

echo "PATH=$PATH:~/.local/bin/" >> ~/.zshrc

Now either restart your terminal or run source ~/.zshrc

You’re ready to test out your program! Change to any directory other than the one you’ve been working in and run your code:

encourage tim
You're doing great, Tim!
Photo by Erwan Hesry on Unsplash
Photo by Erwan Hesry on Unsplash

Congratulations! You’re done. Here is all the code in one place:

# Remove old version of python and do a fresh brew install
sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.6
sudo rm -rf "/Applications/Python 3.7"
brew install python3
# Shebang command to add to the top of your script (include the "#")
#!/usr/bin/env python3
# Make the script executable by running this in your terminal
chmod +x script
# Create a local bin and add it to your shell PATH variable
mkdir ~/.local/bin
echo "PATH=$PATH:~/.local/bin/" >> ~/zshrc
# move a copy of your script to the ./local/bin
cp script ~/.local/bin/script
# Restart your terminal or shell
source ~/.zshrc

Related Articles