How to Run Python Scripts? A Step-by-Step Guide with 2 Methods for Beginners!

How to Run Python Scripts? A Step-by-Step Guide with 2 Methods for Beginners! đź’»

After writing Python code, how do you get it to run? It’s actually super simple! Whether it’s the “right-click” method for beginners or the “terminal command” method for professionals, newcomers can learn both in just one minute! Both methods are taught, so choose according to your needs!

🖱️ Method 1: Right-Click (Best for Beginners, No Command Memory Required)

Applicable Scenario: The code is saved as a<span>.py</span> file (for example,<span>test.py</span>), and you want to quickly run it to see the results.

Steps (Example for Windows, Similar for Mac):

Find Your Code File: Locate the<span>.py</span> file you wrote in the folder (for example, located on the “Desktop” as<span>hello.py</span>);

Right-Click to Select “Open With”: First time running: Right-click the file → “Open with” → Select “Python” (if not displayed, look in “More apps”);

Subsequent Runs: Right-click the file → Directly click “Open with Python” (the system will remember the last choice);

View Results: A black window (command prompt) will pop up, showing the results of the code execution!

✨ Example: If<span>hello.py</span> contains<span>print("你好呀~")</span>, the window will display<span>你好呀~</span>, and the window may close automatically after a few seconds (this is normal; to keep the window open, you can add<span>input()</span><span>:</span>

print("你好呀~")
input("Press Enter to close the window...")  # Add this line to wait for you to press Enter before closing

⌨️ Method 2: Terminal Command (Suitable for Advanced Users, Detailed Error Messages)

Applicable Scenario: When the code needs to pass parameters, you want to see complete error messages, or when using editors like PyCharm.

Steps (Common for Windows/Mac):

Open Terminal:Windows: Press “Win+R” → Type<span>cmd</span> → Press Enter (a black command prompt will pop up);Mac: Open “Launchpad → Other → Terminal” (a white window will pop up);Navigate to the Folder Containing the Code:Assuming the code is located in “Desktop” as<span>test.py</span><span>, type in the terminal:</span><pre><code># Windows: Navigate to Desktop (modify the path according to your folder)
cd Desktop
# Mac: Navigate to Desktop
cd ~/Desktop
After typing, press Enter, and the terminal path will change to the path of “Desktop”, for example,<span>C:\Users\YourName\Desktop></span>).Run the Code:Type<span>python filename.py</span><span>, for example:</span><strong><span><span>View Results</span>:</span></strong><strong><span>The terminal will directly display the execution results, and error messages will also be fully displayed (helpful for debugging).</span></strong><h2><span><span>❌ Common Problem Solutions (Don't Panic if It Fails to Run!)</span></span></h2><p><strong><span><span>1. Cannot Find “Open with Python” in Right-Click</span></span></strong></p><p><span>✅Reason: Python is not installed or “Add to PATH” was not checked;</span></p><p><span>✅Solution: Reinstall Python, making sure to check “Add Python to PATH” (refer to the previous installation tutorial).</span></p><p><strong><span><span>2. Terminal Input</span></span><code><span><span>python</span></span> Prompts “Not an Internal Command”

âś…Reason: Python is not added to the system environment variables;

âś…Solution:

Windows: Right-click “This PC” → “Properties” → “Advanced system settings” → “Environment Variables” → Find “Path” → Add Python installation path (for example,<span>C:\Users\YourName\AppData\Local\Programs\Python\Python312</span>);Mac: Type in the terminal<span>echo 'export PATH="/Library/Frameworks/Python.framework/Versions/3.x/bin:$PATH"' >> ~/.zshrc</span> (replace 3.x with your version), then restart the terminal.

3. Window Flashes and Closes Immediately After Running

âś…Solution: Add<span>input("Press Enter to exit")</span><span>, to force the window to stay open.</span>

4. Prompts “SyntaxError: invalid syntax”

âś…Reason: There is a syntax error in the code (for example, a parenthesis is not closed, or indentation is wrong);

âś…Solution: Carefully check the code; the terminal will indicate which line has the error (for example,<span>line 3</span>), focus on that line.

đź’ˇ Quick Summary: How to Choose Between the Two Methods?

For temporarily and quickly running a single file → Use “Open with right-click”, just click once;For writing complex code and needing to debug errors → Use “Terminal command”, as it provides more complete information.

Running a Python script is like “playing a video” — right-clicking is like double-clicking to play, while the terminal command is like opening it with a player; essentially, both are about getting the program to run! Try it a few times, and you’ll find it’s simpler than you think! 🚀

Leave a Comment