How to use Python in Command Prompt – with examples

  • Reading time:17 mins read

How to use Python in Command Prompt?

Whereas Python code is usually written in a text editor and executed in the command prompt, Python scripts can also be written and executed in the command line.

In this article we will run Python scripts using the Windows command prompt, however, most of these commands can also work on macOS with or without small modifications.



Before getting started we need to make sure that the Python installation is properly set up. If not then we need to revisit the Python installation and properly set up the path in the system variables.

Setting up environmental variables allows us to add additional directories where Python finds its packages and modules.

These variables are essential for Python to successfully run. The only way to confirm that is by running the command below.  



So to get started with running Python programs we just need to type the word python, this will allow us to enter into the Python interactive shell where we can actually type Python commands.

Python interactive console allows developers to run commands and try out code without creating any file.  



Now we can proceed to run actual python scripts. For instance, we can do a simple print function as shown below.



This is quite handy, especially when doing basic math since we will be able to get the answers right away.


Running Python in Command Prompt

On the other hand what if we had a python file already created and your intention is to run it. However, in this case, we have not created the file therefore we will opt out of the Python interactive shell by running the command below.  



We can now proceed to create a simple notepad program that prints out a string by running the code below in the command prompt.



This will open the notepad and prompt us to confirm that we want to create a file named ‘’. We need to click yes to proceed. In the new notepad window, we are going to write a simple print function that will output a string.

Once that is done we now need to save that file as ‘’ under a directory that we can easily navigate to.


python in command prompt


Now that we have successfully created a python file, once again using Python in command prompt we need to navigate to the directory where we have saved the file ‘’.

In order to run the Python file that we initially created, we will simply type in the word ‘python’ followed by the name of the python file which is ‘’.  



This is one of the most common ways of running Python in command prompt. Alternatively,  we can also run the file by just typing the name of the file together with the .py extension.

This is because we have Python properly installed and the Path properly configured, therefore Python will recognize the file and run it.  



Running Python in command prompt is not limited to only simple functions, we can also run slightly more complicated code.


python in command prompt


Now under the same Notepad window that we initially created, we are going to create a function that will take an argument ‘name’ and using f string returns the argument printed alongside the word Hello.  


def hello(name):

   print(f'Hello {name}')


Now using the command line once again if we run the file above by typing its name and the .py extension we will definitely get no output.  



Now, this is because using this method does not get the function hello called and thus is not executed.

Now to get around this we need to type in the command ‘python -i’, this is going to opt into the python interactive shell together with our file already loaded up.  



Since the file is now already loaded up we have access to the function, therefore we can now execute the function by typing the name of the function.  



In order to get the actual output, we have to call the function and pass in arguments as well.  



Now the advantage of executing Python files using this method is the flexibility to choose and execute bits of a program that we want.

For instance, we can execute some functions or some instances of a class that we want leaving out those that we don’t want to be executed.

This is how to use Python in Command Prompt in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.  

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.