When working with text editors in python, it can be confusing how to run a python script because there is no “run” button like in IDEs like Eclipse.
Luckily, it isn’t as hard as you think, but we will have to use the terminal.
So let’s assume you have a file and you want to run it. In my case, I have a python script called Experiment3.py and I want to run it.
Opening the terminal
First, we have to open up the terminal. If you don’t know what a terminal is, it’s basically an interface that lets you type commands into a computer so you can do certain functions on the computer. Basically, it is a way to control your computer through written text.
Depending on your operating system, there are different ways of doing it, but for Windows 10, you can search “cmd” on the bottom left and then click “Command Prompt”.
On Mac, there should be a terminal application that you can just click either in the Applications or Utilities folder.
On Windows, here is how the terminal looks like. It should look very similar in Mac as well.
Upon opening the terminal notice, the directory path on the left. Basically, that path (in my case “C:\Users\atul1” tells you what folder you are operating in. Any commands you do will have to act within that folder. The problem is, however, that my script is not in “C:\Users\atul1”.
My script is in fact in the folder “C:\Users\atul1\Buddy Experiments\Experiment3”.
The Magic of “Cd”
To get to that folder, I have to use a command called cd. Cd is a command that allows me to move towards a subdirectory by typing the next immediate folder.
To get to “C:\Users\atul1\Desktop”, I would type cd Desktop.
Notice our directory path has changed so to get from “C:\Users\atul1\Desktop” to “C:\Users\atul1\Buddy Experiments”, I have to type cd “Buddy Experiments” with quotations.
Similarly, to get to the Experiment3 folder I have to type cd Experiment3.
Now, normally you can just combine all of the cd together to “cd Desktop/”Buddy Experiments”/Experiment3” if you want from the beginning.
To run, my actual script, all I have to do is type the command “python Experiment3.py” and then it just works!
Upon running my script, it also types the output in the terminal “Subtree has been loaded…”. If you are using GUIs or matplotlib or other libraries with different outputs, they will pop up as expected.
That’s really it! It’s very simple to run python scripts, so now you can too. If you enjoyed this post, consider subscribing above. Have a nice day!