Saltar al reproductorSaltar al contenido principalSaltar al pie de página
  • ayer
How to Run Python in Command Prompt | Execute Python Scripts on Windows 11

Learn how to run Python in Command Prompt on Windows 11! This beginner-friendly guide walks you through setting up Python, running Python scripts, and troubleshooting common issues. Whether you're new to programming or need a refresher, this tutorial has got you covered.

---

Steps to Run Python in Command Prompt on Windows 11

#### 1. *Install Python*
Download Python from the official website: [url](url).
Run the installer and ensure the *Add Python to PATH* option is checked.
Choose the recommended installation settings and complete the installation.

#### 2. *Verify Python Installation*
Open Command Prompt and type:
```cmd
python --version
```
If Python is installed correctly, you’ll see the installed version number.

#### 3. *Run Python in Interactive Mode*
Open Command Prompt and type:
```cmd
python
```
You’ll enter Python’s interactive shell.
Test it by typing:
```python
print("Hello, World!")
```
Press *Enter* to see the output.

#### 4. *Run a Python Script*
Write your Python code in a text editor (e.g., Notepad or Visual Studio Code).
Save the file with a `.py` extension (e.g., `script.py`).
Open Command Prompt, navigate to the file’s directory using the `cd` command:
```cmd
cd C:\Users\YourName\Documents
```
Run the script by typing:
```cmd
python script.py
```
The output of your script will be displayed in the Command Prompt.

#### 5. *Troubleshooting Tips*
**Python Not Found**: Ensure Python is added to your PATH environment variable.
**File Not Found**: Double-check the file name and directory path.
**Permission Denied**: Run Command Prompt as an administrator if required.

---

Why Run Python in Command Prompt?
**Ease of Use**: Execute Python scripts quickly without needing an IDE.
**Flexibility**: Perfect for automation and scripting tasks.
**Learning**: A great way to understand how Python interacts with your operating system.

---

Start coding in Python today! If this tutorial was helpful, don’t forget to like, share, and subscribe for more programming guides.

Categoría

📚
Aprendizaje
Transcripción
00:00Hey guys, in this video I'm going to show you how we can install and set up Python on our Windows operating system.
00:05So let's get started and let's see how we can do it.
00:07So first of all, let's check if we already have a Python version installed on our Windows operating system or not.
00:14For that, you can simply open command prompt by typing cmd in the search bar.
00:21And once the command prompt opens, just type Python here and then press enter.
00:27And when you press enter, if it opens this kind of Microsoft Store window, that means Python is not installed on your Windows operating system.
00:38And Windows is opening this Microsoft Store from where you can install Python on your Windows operating system.
00:47But we are not going to install Python from this Windows Store or Microsoft Store.
00:53We are going to install Python from its official website.
00:58So let's close this command prompt and for installing Python, just open your favorite browser and search for Python.
01:06And the first link which will appear here will be from python.org.
01:10So we are going to click on this link.
01:12And here on this page, you can hover over this Downloads section and you can see at the time of making this video, Python 3.13.1 is the latest version.
01:27When you scroll down a little, you can also see this version here under Downloads, which is 3.13.1 at the time of making this video.
01:38So you can either go here and click on this button or the link below.
01:45I'm going to click on this Python 3.13.1 button, which is going to directly start the download of this exe file.
01:55Once this exe file is downloaded, just click on this exe file, which is going to start the installation wizard.
02:01And now you can see this installation wizard for the Python.
02:06Now this window is very important.
02:09Why?
02:10Because you can see two checkboxes here.
02:13One is for adding or using the admin privileges when installing py.exe.
02:22And the other is add python.exe to path.
02:26We are going to check both these checkboxes.
02:29Why?
02:31Because first one is going to add the admin privileges for your Python executable files.
02:38And the other one is going to add the python path to your environment variables.
02:44That means you can open or execute python command from anywhere from your command prompt.
02:52So when you open the command prompt, you will be able to use python command from any folder.
02:58So once you have checked both these checkboxes, in the next step, what I would like to do is I would like to change the location where python will be installed on my Windows operating system.
03:11So by default, you can see this long path where python will be installed.
03:17And I want that python will be installed in my C drive and not on this long path.
03:24So for that, I will choose this customize installation here.
03:29And in the next step, I'm going to leave everything as default in the optional features and then click on next.
03:35And here in the advanced features, I'm going to click on install python 3.13 for all users and then I will also check all these checkboxes here.
03:48Also, I want to change this location to my C drive.
03:53So I don't want to install python in my programs files in my C drive.
03:59I just want this path.
04:01So it will be directly installed in my C folder, right?
04:05And the folder name will be python 3.13 because I will be installing python 3.13 version, right?
04:13So once we have changed the path, I can click on the install button, which is going to give me this kind of warning, which says, do you want to allow this app to make changes on your device?
04:23I'm going to click on yes here.
04:25And once I click on yes, it's going to start the installation process for python.
04:31And after some time, you will see this kind of message which says setup was successful.
04:35So at this point, we can close this window and that means python is successfully installed on our Windows operating system.
04:44Now, let's verify if python is successfully installed and is working on our Windows operating system.
04:51So for that, just click on this Windows icon and then click on all apps here and then scroll down a little and under P, you will see the python option.
05:02So you can see this python 3.13 option.
05:06Just click on this and it's going to show you various different tools.
05:10So when we install python, it installs this idle IDE, which is the graphical user interface using which you can develop your python scripts.
05:22And also it installs the python terminal.
05:25So let's start with this python terminal first.
05:28So I'm going to click on this python 3.13 here, which is going to open this kind of terminal.
05:34And then here you can test your python commands.
05:39So this is called python repl.
05:43And if you want to change the properties of this python repl, for example, if you want to maximize the font size or change the font,
05:55then you can click on this icon here and click on properties here.
06:01And once the property window opens, you can see directly the font option.
06:05So I'm going to increase the font size to, let's say, 24 here.
06:09And then I'm going to click on OK and then click on OK and font size has been increased, right?
06:17So here I can give any python command.
06:22For example, we can evaluate, let's say, what is 5 plus 5, right?
06:26And then press enter and it's going to give us the result, which is 10, right?
06:30If I want to print something, for example, I want to print hello world, I can use this command, which is print, which is an inbuilt function in Python.
06:40And in these double quotes, I can write hello world here and then press enter.
06:47And it's going to print hello world.
06:49So everything is working in this python terminal.
06:53Now let's just open the python idle to see how we can use the python idle.
07:01So once again, click on this windows icon or you can just search for idle IDE.
07:08So just search for IDE here and you will be able to see this result and then click on this idle icon.
07:16And here also you can see similar kind of prompt where you can evaluate your python commands.
07:24For example, adding, subtracting or anything or using any python function or you can print something.
07:33So I'm going to just write print.
07:35And in these parentheses, you can see it gives you more intelligence.
07:40So it gives you the information, what arguments this print function takes and what it does, right?
07:51So I'm going to just add these double quotes and then add hello world here and then close this parenthesis.
08:00And then press enter and it's going to print hello world once again.
08:04So this is also working fine, right?
08:07Let's say you want to create a python file.
08:09You can click on file option here and then you can click on new file option.
08:15It's going to create a new file for you, which is untitled right now.
08:20So we need to give any name to our file.
08:23So I'm going to once again click on file option here and then I'm going to click on save as.
08:29And let's say I want to save my file in my documents folder and I have this test folder here where I want to save my file, right?
08:37And then the file name will be test.py here and then I'm going to save this file, right?
08:43So once I have saved this file, you can see, you can see file name is test.py and it's located in this folder.
08:52Once again, I can just let's say print something here or you can write your script here, which will be your Python script.
09:00And then it will be saved in this file.
09:04The advantage of writing your script into a file is that when you close the file, it's not going to delete your code, right?
09:15You can always execute your file or Python script again and again whenever you want.
09:22So once I'm done, I can save this file and to execute this file, I can click on run and then I can click on run module.
09:32And once I click on run module, it runs my script, which is test.py into this terminal and it prints hello world.
09:41So this is how you can create a file and execute it, right?
09:44So your file is here and it runs your script into this idle shell.
09:50Let me close this idle shell and this file.
09:56And now I want to use the command prompt, which is the default command prompt, which comes with Windows operating system.
10:05So for that, you can just search for CMD and open the command prompt.
10:09And here also, when you type python space hyphen hyphen version here, it's going to give you the Python version.
10:18This is the version we have installed.
10:19And when you give this Python command, so when you just write Python and then press enter, you will see the prompt similar to the idle IDE or Python terminal, which we have seen earlier, right?
10:35Here also, when you type some command, it's going to give you the output.
10:40So this is the Windows command prompt, which comes with Windows, right?
10:45It's not installed by Python installation, but still we are able to use Python in the command prompt.
10:53Also, when you press Ctrl C here, you will see a key interrupt.
10:59So if your program is running in loop and you want to stop it, you can just press Ctrl C to stop that program.
11:06To quit, just write quit here and then give these two parentheses and then press enter and you come out of your Python repl.
11:16Now, we know that we have created this test file in this folder, right?
11:21Using the idle IDE.
11:23So let's say I want to execute this Python script using the command prompt from the command prompt itself.
11:32That means I want to execute this Python script from here itself.
11:36I will just copy the path where my test.py file is there and then in the command prompt, I can just type cd and then paste the path, which I have copied and then press enter, which will redirect me to this directory or folder.
11:54And here I can just type Python and then the name of my file, which is test.py and then press enter.
12:03It's going to execute my Python script.
12:05And in my case, it's printing hello world.
12:08So it prints hello world here.
12:09So this is how also you can execute a Python script or Python file using command prompt.
12:16So this is how you can install and set up Python on your Windows operating system.
12:20I hope you've enjoyed this video and I will see you in the next video.
12:24I hope you've enjoyed this video and I will see you in the next video.

Recomendada