{"id":99391,"date":"2026-01-23T17:00:48","date_gmt":"2026-01-23T11:30:48","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=99391"},"modified":"2026-09-09T20:49:15","modified_gmt":"2026-09-09T15:19:15","slug":"how-to-run-python-file-in-terminal","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-run-python-file-in-terminal\/","title":{"rendered":"How to Run Python File (Windows, Mac, Linux, IDEs)"},"content":{"rendered":"\n<p>To run a Python file, you just need to open a terminal or command prompt, navigate to the folder where your file is saved, and type a simple command. That&#8217;s the entire idea behind how to run Python file, no complicated setup, no extra tools required, just a few keystrokes and your code comes to life.<\/p>\n\n\n\n<p>But here&#8217;s the catch, the exact steps look a little different depending on whether you&#8217;re on Windows, Mac, or Linux, or if you prefer working inside a code editor like VS Code or PyCharm. This guide walks you through every version of that process, so no matter what setup you&#8217;re using, you&#8217;ll know exactly what to type and where.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">Learn&nbsp;<strong>how to run Python file<\/strong>&nbsp;using a simple command in Terminal, Command Prompt, or PowerShell; no advanced setup required.<\/span><\/li>\n\n\n\n<li>Covers <strong>step-by-step instructions for Windows, Mac, and Linux<\/strong>, since each system handles it slightly differently.<\/li>\n\n\n\n<li>Includes how to run Python files directly inside <strong>code editors like VS Code<\/strong>, for a more visual, click and run experience.<\/li>\n\n\n\n<li>Fixes for <strong>common errors like &#8220;Python is not recognized&#8221;<\/strong> and &#8220;ModuleNotFoundError&#8221; so you&#8217;re not stuck troubleshooting alone.<\/li>\n\n\n\n<li>A quick, practical guide you can follow along with, whether you&#8217;re <strong>running your first script<\/strong> or just need a refresher.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n  <strong style=\"font-size: 22px; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/strong> <br \/><br \/>\n  <span>\n    <strong style=\"color: #110053;\">Python<\/strong> was created by <strong style=\"color: #110053;\">Guido van Rossum<\/strong> and first released in <strong style=\"color: #110053;\">1991<\/strong>; it was named after the comedy show <strong style=\"color: #110053;\"><i>Monty Python&#8217;s Flying Circus<\/i><\/strong>, not the snake.\n  <\/span>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Does It Mean to Run a Python File<\/strong><\/h2>\n\n\n\n<p>When you write <a href=\"https:\/\/www.guvi.in\/blog\/what-is-python-used-for\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> code, you save it in a file with a <code>.py<\/code> extension, like <code>app.py<\/code> or <code>script.py<\/code>. That file just sits there on your computer; it does nothing on its own. <\/p>\n\n\n\n<p>Running the file means telling your computer to actually read that code, line by line, and carry out whatever instructions you wrote, print something, do a calculation, open a webpage, whatever your script is built to do.<\/p>\n\n\n\n<p>Think of it like a recipe. Writing the code is like writing down the recipe; running it is like actually cooking the meal. Until you run it, it&#8217;s just text sitting in a file, not doing any real work yet.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Turn Your Python Skills Into a Data Science Career. Knowing how to run a script is just the beginning. <\/em><\/strong><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><em><strong>The HCL GUVI&#8217;s&nbsp;<\/strong><\/em><a href=\"https:\/\/www.guvi.in\/zen-class\/data-science-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=how-to-run-python-file\" target=\"_blank\" rel=\"noopener\"><em>Advanced Data Science &amp; Generative AI Course<\/em><\/a><em><strong>&nbsp;helps you build real machine learning models, work on AI-powered projects, and gain skills companies actively hire for, with live mentor sessions, hands-on projects, and placem<\/strong><\/em><\/span><strong><em><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">ent support to help you land your first data science role<\/span>. Enroll now and start your Data Science journey today!<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prerequisites Before You Start<\/strong><\/h2>\n\n\n\n<p>Before you jump into how to run Python file on your system, check a couple of things first.<\/p>\n\n\n\n<p>Skipping these is the number one reason people get stuck. Not because the process is hard, but because Python isn&#8217;t installed properly or isn&#8217;t set up right.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a. Checking if Python Is Installed<\/strong><\/h3>\n\n\n\n<p>Open your terminal or command prompt. Type <code>python --version<\/code>.<\/p>\n\n\n\n<p>If Python is installed, you&#8217;ll see something like <code>Python 3.12.0<\/code>. If you see an error instead, Python isn&#8217;t installed or isn&#8217;t added to your system PATH.<\/p>\n\n\n\n<p>On Mac and Linux, try <code>python3 --version<\/code> instead. These systems often use <code>python3<\/code> as the default command name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b. Installing Python (Windows, Mac, Linux)<\/strong><\/h3>\n\n\n\n<p>If Python isn&#8217;t installed, go to the <a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">official P<\/a><a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">ython website<\/a>. Download the latest version for your operating system.<\/p>\n\n\n\n<p>Windows users should check the box that says &#8220;Add Python to PATH&#8221; during installation. This one step avoids a lot of headaches later.<\/p>\n\n\n\n<p>Mac users can install Python through Homebrew using <code>brew install python3<\/code>. Most Linux distributions come with Python pre-installed, or you can install it with <code>sudo apt install python3<\/code> on Ubuntu.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>c. Verifying Your Python Version<\/strong><\/h3>\n\n\n\n<p>Once installed, confirm it worked. Run <code>python --version<\/code> or <code>python3 --version<\/code> again.<\/p>\n\n\n\n<p>This quick check saves you from confusing errors later when you try running a Python file for the first time. If the version number shows up correctly, you&#8217;re ready to move on.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Also Read:<\/strong> <em><a href=\"https:\/\/www.guvi.in\/blog\/how-to-check-python-version-in-cmd\/\" target=\"_blank\" rel=\"noreferrer noopener\">Check Python Version in CMD, Terminal &amp; VS Code<\/a><\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Run a Python File in Windows<\/strong><\/h2>\n\n\n\n<p>Windows users have a couple of options here: Command Prompt or PowerShell. Both work fine, pick whichever one you&#8217;re comfortable with.<\/p>\n\n\n\n<p>Here&#8217;s how to run Python file on Windows, step by step:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a. Using Command Prompt (CMD)<\/strong><\/h3>\n\n\n\n<p>Open Command Prompt. You can search for <a href=\"https:\/\/www.guvi.in\/blog\/command-line-interface-explained\/\" target=\"_blank\" rel=\"noreferrer noopener\">&#8220;cmd&#8221;<\/a> in the Start menu.<\/p>\n\n\n\n<p>Navigate to the folder where your file is saved using the <code>cd<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd C:\\Users\\YourName\\Documents\\Projects<\/code><\/pre>\n\n\n\n<p>Then run your file with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python filename.py<\/code><\/pre>\n\n\n\n<p>Press Enter. Your script runs and the output shows up right there in the same window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b. Using PowerShell<\/strong><\/h3>\n\n\n\n<p>PowerShell works almost the same way. Open it from the Start menu, then navigate to your file&#8217;s folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd C:\\Users\\YourName\\Documents\\Projects<\/code><\/pre>\n\n\n\n<p>Run the file with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python filename.py<\/code><\/pre>\n\n\n\n<p>Some setups may require <code>python3<\/code> or <code>py<\/code> instead of <code>python<\/code>, depending on how Python was installed on your machine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>c. Fixing &#8220;Python Is Not Recognized&#8221; Error<\/strong><\/h3>\n\n\n\n<p>This is the most common issue Windows users run into. It shows up like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'python' is not recognized as an internal or external command<\/code><\/pre>\n\n\n\n<p>It means Python isn&#8217;t added to your system PATH. To fix it, reinstall Python from the <a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">official Python website<\/a> and make sure you check the box that says &#8220;Add Python to PATH&#8221; during setup.<\/p>\n\n\n\n<p>If Python is already installed and you still see this error, search for &#8220;Environment Variables&#8221; in the Start menu, edit the PATH variable, and manually add the folder where Python is installed.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Run a Python File on Mac<\/strong><\/h2>\n\n\n\n<p>Running a Python file on Mac is straightforward once Terminal is open.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a. Using Terminal<\/strong><\/h3>\n\n\n\n<p>Open Terminal from Spotlight search or your Applications folder.<\/p>\n\n\n\n<p>Navigate to the folder where you saved your file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/Users\/YourName\/Documents\/Projects<\/code><\/pre>\n\n\n\n<p>Then run your file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 filename.py<\/code><\/pre>\n\n\n\n<p>Press Enter. Your output shows up right in Terminal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b. Python vs Python3 Command on Mac<\/strong><\/h3>\n\n\n\n<p>Macs come with Python pre installed, but it&#8217;s usually an older version meant for the system itself, not for your own projects. Because of this, typing just <code>python<\/code> often doesn&#8217;t work or points to the wrong version.<\/p>\n\n\n\n<p>That&#8217;s why <code>python3<\/code> is the command you&#8217;ll use most of the time on Mac. If you installed Python yourself from python.org or Homebrew, <code>python3<\/code> is the safest way to run your file correctly, this is essentially how to run Python file on a modern Mac setup.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Run a Python File on Linux<\/strong><\/h2>\n\n\n\n<p>Linux works a lot like Mac here, since both are built on similar terminal systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a. Using Terminal<\/strong><\/h3>\n\n\n\n<p>Open your terminal application, this varies slightly by distribution but is usually easy to find in your applications menu.<\/p>\n\n\n\n<p>Navigate to your file&#8217;s folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/home\/yourname\/projects<\/code><\/pre>\n\n\n\n<p>Run the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 filename.py<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b. Giving Execute Permission (chmod +x)<\/strong><\/h3>\n\n\n\n<p>Sometimes you&#8217;ll want to run a Python file directly without typing <code>python3<\/code> before it every time. For that, the file needs execute permission first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x filename.py<\/code><\/pre>\n\n\n\n<p>This command tells Linux the file can be run as a program, not just opened or edited.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>c. Running with .\/filename.py<\/strong><\/h3>\n\n\n\n<p>Once the file has execute permission, and as long as the file starts with a shebang line like <code>#!\/usr\/bin\/env python3<\/code> at the very top, you can run it directly like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/filename.py<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Run a Python File Using Code Editors and IDEs<\/strong><\/h2>\n\n\n\n<p>If typing commands in a terminal isn&#8217;t your thing, code editors and IDEs make running Python files a lot more visual and beginner-friendly. Popular options include VS Code, PyCharm, Jupyter Notebook, and Sublime Text, but VS Code is by far the most widely used, so let&#8217;s focus on that here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Running a Python File in VS Code<\/strong><\/h3>\n\n\n\n<p>First, make sure you have the official Python extension installed in VS Code. You&#8217;ll find it in the Extensions panel; just search for &#8220;Python&#8221; and install the one published by Microsoft.<\/p>\n\n\n\n<p>Once that&#8217;s set up, open your <code>.py<\/code> file inside VS Code. You&#8217;ll notice a small play button (a triangle icon) in the top right corner of the editor window. Click it, and VS Code runs your file instantly, showing the output in a built-in terminal panel at the bottom of the screen.<\/p>\n\n\n\n<p>If you prefer typing commands, VS Code&#8217;s built-in terminal works exactly like your regular terminal or command prompt. <\/p>\n\n\n\n<p>Just open it with `Ctrl + (backtick) on Windows or Cmd + on Mac, then type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python filename.py<\/code><\/pre>\n\n\n\n<p>Or on Mac and Linux:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 filename.py<\/code><\/pre>\n\n\n\n<p>Either method works fine, this is really just how to run Python file inside an editor instead of a separate terminal window. <\/p>\n\n\n\n<p>The play button is quicker for beginners, while the built-in terminal gives you more control if your script needs input or command-line arguments.<\/p>\n\n\n\n<p><strong><em>(The play button (\u25b7) in VS Code sits in the top right corner and is called &#8220;Run Python File.&#8221; Click it to run your script instantly in the built in terminal below.)<\/em><\/strong><\/p>\n\n\n\n<p>VS Code also shows errors directly in the editor with red underlines, so if something&#8217;s wrong with your code, you&#8217;ll often spot it before you even hit run.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors While Running Python Files and How to Fix Them<\/strong><\/h2>\n\n\n\n<p>Even when you know exactly how to run Python file, a few common errors tend to trip people up. Here are the ones you&#8217;ll run into most often.<\/p>\n\n\n\n<ul>\n<li><strong>Python is not recognized as an internal or external command<\/strong>: This means Python isn&#8217;t added to your system PATH on Windows. Reinstall Python and check the &#8220;Add Python to PATH&#8221; box during setup.<\/li>\n\n\n\n<li><strong>No such file or directory<\/strong>: You&#8217;re likely in the wrong folder. Use <code>cd<\/code> to navigate to the exact location where your <code>.py<\/code> file is saved before running it.<\/li>\n\n\n\n<li><strong>SyntaxError in your code<\/strong>: This usually points to a typo, missing colon, or incorrect indentation somewhere in your script. Python will tell you the exact line number, so start there.<\/li>\n\n\n\n<li><strong>ModuleNotFoundError<\/strong>: This shows up when your script uses a library that isn&#8217;t installed yet. Install it using <code>pip install modulename<\/code> and try running the file again.<\/li>\n\n\n\n<li><strong>Permission denied (Mac\/Linux)<\/strong>: This happens when you try running a file directly with <code>.\/filename.py<\/code> , but it doesn&#8217;t have execute permission yet. Fix it with <code>chmod +x filename.py<\/code> before running it again.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Running a Python file really comes down to picking the method that fits how you work, whether that&#8217;s a quick terminal command, Windows-specific tools like CMD or PowerShell, or a code editor like VS Code. Once you know how to run Python file the way that suits your setup, it becomes second nature within a few tries, no need to overthink it every time.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1788963716482\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1. How do I run Python file for the first time?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Open your terminal, navigate to the file&#8217;s folder, type <code>python filename.py<\/code>, and press Enter. That&#8217;s the basic way to run a Python file.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788963724141\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. Why does &#8220;python is not recognized&#8221; show up when I run my file?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python isn&#8217;t added to your system PATH. Reinstall Python and check &#8220;Add Python to PATH&#8221; during setup, then try running Python file again.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788963724804\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. Can I run a Python file without using the terminal?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, code editors like VS Code let you run Python file with a single click using the built-in play button.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788963731088\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. Do I need to use python3 instead of python?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>On Mac and Linux, yes, most systems default to <code>python3<\/code>. That&#8217;s simply how to run Python file correctly on those platforms.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788963785013\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">5. What&#8217;s the difference between running a file and importing it?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Running executes the file directly. Importing brings its code into another file to reuse functions; it doesn&#8217;t run the file&#8217;s main output on its own.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788966565817\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">6. Why does my Python file close immediately after running?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>This usually happens on Windows when double-clicking a <code>.py<\/code> file. The window opens and closes too fast to see output. Run it from the terminal instead, using <code>python filename.py<\/code> to see the results properly.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788967116865\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">7. Is there a difference between running a Python file and a Python script?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, both terms mean the same thing. Whether you call it a file or a script, how to run Python file stays exactly the same process.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>To run a Python file, you just need to open a terminal or command prompt, navigate to the folder where your file is saved, and type a simple command. That&#8217;s the entire idea behind how to run Python file, no complicated setup, no extra tools required, just a few keystrokes and your code comes to [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":102835,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717],"tags":[],"views":"1854","authorinfo":{"name":"Abhishek Pati","url":"https:\/\/www.guvi.in\/blog\/author\/abhishek-pati\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/01\/How-to-Run-Python-File-in-Terminal-300x116.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/99391"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=99391"}],"version-history":[{"count":16,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/99391\/revisions"}],"predecessor-version":[{"id":138247,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/99391\/revisions\/138247"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/102835"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=99391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=99391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=99391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}