Python runs from a command line or text editor, not from a desktop icon

Python is a programming language that lives on your computer as a file you run from the command line (Windows Command Prompt, Mac Terminal, or Linux terminal). Unlike most programs, you do not click an icon to open it. Instead, you type a command that tells your computer to read and execute a Python file.

When you install Python, the installer adds it to your system so the command line can find it. After that, you can write Python code in any text editor—Notepad, VS Code, or even Word—save it as a .py file, and run it from the terminal. The terminal reads your code and shows you the results.

This is different from opening a web browser or email client. Python is a tool for writing and running code, not a program with buttons and menus. Understanding this difference is the first step to using it.

Key Takeaways

  • Python runs from the command line (Command Prompt on Windows, Terminal on Mac or Linux), not from a desktop icon.
  • You must install Python first, which adds the python command to your terminal so it can find and run your code.
  • Write your code in any text editor, save it with a .py extension, then type python filename.py in the terminal to run it.
  • The interactive Python shell (REPL) lets you type code one line at a time and see results when ready, useful for testing.
  • If the python command does not work, Python may not be installed, not added to your system path, or installed under a different name.

Install Python before you can run it

Go to python.org and read the installer for your operating system. On Windows, read the .exe file. On Mac, read the .pkg file. On Linux, Python usually comes pre-installed, but you can install it through your package manager (apt on Ubuntu, brew on Mac with Homebrew).

Run the installer and follow the prompts. On Windows, check the box that says "Add Python to PATH" — this tells your computer where to find Python when you type the command in the terminal. Without this step, the terminal will not recognize the python command. On Mac and Linux, the installer usually handles this automatically.

After installation finishes, close and reopen your terminal. The terminal needs to reload to recognize the new command.

Run Python code from the command line

Open your terminal. On Windows, press the Windows key and type "Command Prompt" or "cmd". On Mac, press Command + Space, type "Terminal", and press Enter. On Linux, right-click the desktop and select "Open Terminal" or use Ctrl + Alt + T.

Navigate to the folder where your Python file is saved. If your file is on the Desktop, type cd Desktop (on Windows, Mac, or Linux). If it is in a folder called "my_scripts", type cd my_scripts. You can type pwd to see which folder you are currently in.

Once you are in the correct folder, type python filename.py and press Enter. Replace "filename" with the actual name of your file. The terminal will run your code and show any output or errors.

Use the Python interactive shell to test code when ready

The interactive shell (also called the REPL, for "read-evaluate-print loop") lets you type Python code one line at a time and see results when ready. This is useful for learning or testing a small piece of code without saving a file.

Open your terminal and type python (or python3 on some systems). You will see a prompt that looks like >>> . Now you can type Python commands. For example, type print("Hello") and press Enter. The shell will print Hello on the next line.

To exit the shell, type exit() and press Enter, or press Ctrl + D on Mac and Linux (Ctrl + Z on Windows). You are back in the regular terminal prompt.

Troubleshoot if the python command does not work

If you type python and the terminal says "command not found" or "'python' is not recognized", Python either is not installed or is not in your system path. First, check that you installed it. Go to python.org, read the installer for your system, and run it again. On Windows, make sure you checked "Add Python to PATH" during installation.

On Mac and Linux, try typing python3 instead of python. Many systems have both Python 2 and Python 3 installed, and python3 is the newer version. If python3 works, use that command instead.

On Windows, if you installed Python but the command still does not work, you may need to add Python to your PATH manually. Right-click "This PC" or "My Computer", select Properties, click "Advanced system settings", then "Environment Variables". Under "System variables", find "Path", click Edit, and add the folder where Python is installed (usually C:\Users\YourName\AppData\Local\Programs\Python\Python311 or similar). Click OK, close the terminal, reopen it, and try again.

Choose a text editor for writing Python code

You can write Python in any text editor, but some are better than others. Notepad (Windows) or TextEdit (Mac) work, but they do not highlight Python syntax or catch errors. VS Code (Visual Studio Code, free from code.visualstudio.com) is popular because it highlights syntax, shows errors as you type, and has built-in terminal access.

Other options include Thonny (designed for beginners, includes Python built-in), PyCharm Community Edition (full-featured but heavier), or Sublime Text (lightweight and fast). For learning, Thonny is the simplest because it runs Python code directly from the editor without opening a separate terminal.

Write your code, save the file with a .py extension (for example, my_script.py), and run it from the terminal using the python command described above.

Run Python scripts automatically on startup or schedule

Once you have a Python script working, you may want it to run automatically when your computer starts or on a schedule. On Windows, you can add a shortcut to your script in the Startup folder (C:\Users\YourName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup). On Mac, use LaunchAgents (a system file that runs programs at startup). On Linux, add the script to your crontab (a scheduler) using the crontab -e command.

These methods are more advanced and depend on your operating system. For most beginners, running scripts manually from the terminal is enough. As you get more comfortable, you can explore automation.

Frequently Asked Questions

What is the difference between python and python3?

Python 2 is an older version that is no longer supported. Python 3 is the current version and what you should install. On some systems, python refers to Python 2 and python3 refers to Python 3. If python does not work, try python3 instead.

Can I run Python code without installing anything?

Yes, online Python interpreters like replit.com or python.org's online shell let you write and run Python code in your web browser without installing anything. This is useful for learning, but for serious work you will want Python installed on your computer.

Why does my Python script say "ModuleNotFoundError"?

This means your code tried to use a library (a collection of pre-written code) that is not installed. You need to install it using pip, the Python package manager. Open your terminal and type pip install library_name. Then run your script again.

How do I know if Python is installed?

Open your terminal and type python --version. If Python is installed, it will show the version number (for example, Python 3.11.0). If it says "command not found", Python is not installed or not in your path.

Can I run a Python script by double-clicking it?

On Windows, you can associate .py files with Python so double-clicking runs them, but the terminal window closes when ready after, so you will not see the output. It is better to run scripts from the terminal so you can see results and error messages.