How to run Python from CMD in Windows 10

Introduction

To run Python from CMD in Windows it’s required to add it’s absolute location to the PATH environment variable. The PATH environment variable holds location of directories to look for when you execute a program in the command prompt.

When you type and execute a command in CMD that is not in the PATH environment variable you get the following error.

how to run python from cmd in windows 10
Windows CMD

Step 1: Locate your Python installation

Before adding Python to PATH in Windows it’s necessary to find the absolute location of your Python installation.

To find the location of Python in Windows 10 press the Win key and search for Python. Then with a right click go to Open file location.

Then locate the Python executable and with a right click go to Properties. Look for the Target and copy the text inside it.

Python 3.7 properties

Remove the python.exe and you have the absolute path of your Python installation.

C:\Users\User\AppData\Local\Programs\Python\Python37\

There is a subdirectory named Scripts inside the Python37 or whatever yours is called. The Scripts subdirectory has important scripts that help you work with Python. For example you may want to use pip from the Windows CMD to install the Python packages for your project. In this case you need to add the absolute path of the Scripts subdirectory to the PATH environment variable too.

So we need to add two absolute paths to the Windows PATH.

C:\Users\User\AppData\Local\Programs\Python\Python37\
C:\Users\User\AppData\Local\Programs\Python\Python37\Scripts\

Step 2: Add Python to PATH

Now that you have the absolute path of your Python installation you can add it to the PATH. Windows lets you edit the PATH environment variable to fit your needs.

Press the Win key and search for Environment variables. Click on Edit the system environment variables and the following shows up.

Edit windows environment variables

Click on the Environment Variables. Then select Path and click on Edit as below.

Click on New.

Copy and paste the first path.

Click on New again and copy and paste the second path.

Then click on OK. OK again.

Step 3: How to run Python from CMD in Windows 10

To run Python from CMD in Windows 10 type:

python
how to run python from cmd
How to run Python from CMD in Windows 10

To run pip from CMD type:

pip

pip in Windows 10

Step 4: How to run a Python script from CMD

To run a Python script from CMD in Windows 10 type the name of the Python executable you put to PATH and specify the absolute path of your script after it.

python C:\Users\User\Desktop\test.py

how to run a python script from cmd

Final thoughts

Through this article you learned how to run Python from CMD in Windows 10. You also learned how to execute a Python script from the command prompt.

Leave comment

Your email address will not be published. Required fields are marked with *.