Adding Python to PATH: Understanding What PATH Is & Adding Python to PATH on Windows

Real Python · Beginner ·🛠️ AI Tools & Apps ·2mo ago

Key Takeaways

This video teaches how to add Python to the system PATH environment variable on Windows, covering the concept of PATH and troubleshooting Python installation issues

Full Transcript

Welcome to how to add Python to path. My name is Arianne and I'll be your instructor for this course. You're probably here because you've already installed Python, but when you try typing Python into the command line, it doesn't work the way you want it to. Maybe you're getting an error message that the Python command wasn't found, or it's opening up the Windows Store, or maybe it's running the wrong version of Python. Now, fixing these errors may involve adding Python to path. So, in this course, you'll learn what the path environment variable is and how your operating system uses it to find the Python command. But, if the Python command isn't found, it might not actually be a problem with path, but instead a problem of using the wrong command. Therefore, I will also discuss what other commands you should try before trying to edit path. Finally, if you do need to modify path, you'll learn how to add Python to it. But, you'll also see how to reorder and remove items if you need to solve edge cases that are a bit more complex. Please note that there are different lessons for whether you're working on Windows, macOS, or Linux. And you only need to watch the lessons specific to your operating system. Here's an overview of how the lessons are broken down. So, after this overview, you'll learn all about path and its syntax, and this lesson is relevant to all operating systems. The next three lessons are all related to Windows. You'll learn about the different ways to run Python on Windows, then how to add Python to path, and finally, how to work with the path environment variable from the command line. Then, lessons six and seven show how to add Python to path in Mac and Linux respectively. And in lesson eight, you learn how to manage path on Mac and Linux at the same time because the process is quite similar. So, if you're on Mac, you'll want to watch lessons six and eight. And if you're on Linux, you'll want to watch lessons seven and eight. Note that for the Linux lessons, I'll be demonstrating in a virtual machine running Ubuntu. And finally, there's a short summary of what's covered in the course. Now, let's get started learning about what exactly the path environment variable is. And you should start with this lesson regardless of what operating system you're working in. What is the path environment variable? Just like your Python programs can have variables to store different values, your operating system also keeps track of its own variables, which we call environment variables. You might have a user one to store the currently logged in user, or home that holds the user's home directory. Path is an environment variable that stores a big string, which contains a list of paths to folders. Those folders contain executables like python.exe, and these are basically programs that are run when a command with the same name is invoked. So, what does path look like? On my Mac, I can type this command, echo dollar sign path, into the command line or terminal, and view the contents of path. And then I get this big wall of text. And it can look overwhelming, but it's really just a long list of strings of paths. Since I'm on a Mac, my folder paths are separated by colons, and if this were Windows, it would use semicolons as the delimiter. Now, let's make it easier to read by turning those colons into new line characters using this little command here. Now, you can more easily see that it's just a bunch of paths to folders, including ones called like python.framework, or user/bin. So, some important notes about path are, again, it uses colons or semicolons as a delimiter depending on your operating system. All of the paths must be for directories, and you shouldn't have files or executables being pointed to directly. Also, subdirectories inside of the path directories are not searched. So, you can't just add your root directory to path and have it look everywhere. And what happens when you run Python in the command line? Your terminal will search the folders inside of path in order and look for an executable called Python or python.exe. If it's found, it will run it, and if it's not found in any of the paths, then it returns an error. So, you might get something like this, command not found Python, or on Windows, it might open up the Windows Store and prompt you to install Python. So, what are some of the potential problems you might come across when trying to run Python? One is that Python's directory is not even on path, so you would get command not found, and then you'd have to add it to path. It could be that the order of directories on path is wrong, so when you use the Python command, it actually runs a different version than what you want it to because that's what was found first. It could be that you have a corrupted executable on path like maybe during the installation process, your computer shut down, and python.exe doesn't work for some reason. And if that was the first one to be found, then it's going to fail. So, maybe you need to remove that path from your path environment variable. And finally, it's possible that the Python executable is not on path, but some other equivalent command is. So, this is a common point of confusion for beginner Python developers because tutorials will often use the basic Python command. But, confusingly, depending on your operating system and how you installed Python, it's often the case that on Unix systems, you should actually be using the Python 3 command, and on Windows, you should be using the Pi command. And these are on your path, just not the basic Python one. So, in the next lesson, you'll see how you can run Python on Windows if the basic Python command doesn't work. Starting out with Python on Windows can be a frustrating experience. Until Python 3.14, if you were to install Python with the official python.org installer, and then you use the default settings and click install now, you wouldn't be able to use the Python command from PowerShell. Note that there's this little checkbox at the bottom here that says add python.exe to path, and a lot of people don't think to check it. Partly it's because they don't know what that means. They maybe don't know what path is, and they don't know that you won't be able to follow along with tutorials exactly if you don't click that box. You install with the default settings, you go to PowerShell, try to type Python in, it's not found, Python 3, Python 3.13, none of those will work. It might give you an error, or it might open up the Windows Store instead of running the Python console like you'd expect. And most tutorials are going to use one of those commands in their examples, usually Python 3. So, you would think that just running the default installer and then following a tutorial, it'll just work, but it doesn't. So, why not? And why not make it the default to have that checkbox clicked? There's some historical reasons for that, and that's because there are some differences with how path works on Windows versus how it works on Mac or Linux. On Mac and Linux, path is updated automatically, and you can get the Python 3 command just by following the installer. So, that's what most tutorials assume. But with Windows, there's a max character limit on how long path can be. There's also a security risk involved in updating path, and there's kind of two different paths. There's a user-specific one and a system-wide one. So, for all of these reasons, it was decided long ago that users would have to opt in to adding Python to path as a little safeguard to show that they understood what it was and that there are risks involved with it. So, if you haven't already added Python to path, what are your options? One is that you can uninstall Python and reinstall it with that checkbox checked, or you can manually add Python to path. For both of these options, you should watch the next two lessons so you know how to manage path on Windows. But, those aren't your only options. You could run the Python executable directly, though it's a bit hard to find. It's installed in kind of hidden location by default. Or, the best way is to use Pi Launcher. So, this is using the Pi command, and this is a special program created for Windows so that you can run Python without editing path. So, to run the Pi Launcher instead of the Python command or Python 3 command, which gives this error, you can instead use the Pi command. Running just py, Pi, is going to run 3.13 for me because that's what I have installed on my machine, and it opens up the Python console. Okay, if you had a file, then you could use Pi and then the file name to run the file. And then if you had multiple versions of Python, you can use Pi then {dash} the version number, so Pi {dash} 3.13 will open up 3.13 for me. And if I had 3.11 installed, then I can use pi -3.11. Since I don't have 3.11 installed, I get this different error, no suitable Python runtime found. If, however, pi gives you the same error as before, saying that it's not a valid command, then you probably don't have any version of Python installed, and you should do that first. So, this was the recommended way to run Python on versions 3.3 to 3.13. As of 3.14, though, they've released a new application called the Python install manager, and it kind of replaces the pi launcher. It uses the same pi command, but it also allows you to install different versions of Python on your machine through the command line. So, as of Python 3.16, this install manager is going to be the way to manage Python on your machine, and the standalone installer won't even be around anymore. Using the Python install manager, you can also create an alias so that the Python command works, but there's a couple of additional steps. Some other options are downloading Python through the Windows store or Anaconda. Those will give you the Python command out of the box, and then some more advanced options are using pi env for Windows or uv, and these are a little more advanced. They have more capabilities, but also require you to learn some other commands. That's it. You may not need to add Python to path if you can just use the pi command, but if you do want to add Python to path, then watch this next lesson, and we'll also discuss how to reorder or remove items from path if you need to troubleshoot some problems with it. Now, let's add Python to path on Windows. This will let you use the Python command from the command line. First, you're going to need to know where your Python executable was saved to. So, you're looking for the folder that contains python.exe. When you use the default settings in the official installer, the location it's going to be saved to is here inside of your user folder, app data, local, programs, Python, and then Python 3x, where [snorts] 3x is the version of Python that you installed. So, I have Python 3.13, and I'd be looking for Python 313. Some other locations you could try would be in the C drive, Python 3x, or inside of program files, and those would only be for if you installed Python for all users on your computer. So, to verify the Python location, you can open up the file explorer and try to run the python.exe file and make sure it runs the Python console. We're going to try it with the default location. So, you can open up your file explorer and go to C drive, users, your username, and then app data. If you don't see app data, it's because it's a hidden folder, so you can go to view, show, and then make sure hidden items is checked. You can also make sure that file name extensions is checked so that when we find Python, it's going to say python.exe. So, go into app data, local, programs, and here is Python. Now, there's also the Python launcher folder where the pi command can be found, and then Python 313, which is what we're looking for. And double-click python.exe, and it opens up the Python console. So, that's the file we're looking for, looking to run when we run the Python command, and the folder, you can copy the path by going to the top where the breadcrumbs are here, and copying that path. So, that's what we're going to be adding to path. Now, if you weren't able to find your python.exe at one of these locations, don't worry, at the end of this lesson, I'll show you how to search your computer and a few different methods for finding it. For now, since I think that's a bit of an edge case, I'm just going to assume you found it now, and we'll look at how to actually add it to your environment variables. Windows has a special GUI for editing environment variables. To find it, you can go search for environment variables, and you should find a match that says edit the system environment variables, and it will open up the system properties here, and you can click the environment variables button. Or, depending on which one you clicked, it might just open this one directly. Now, in environment variables, on the top, there's going to be the user variables for your specific user, and then on the bottom, there's the system-wide variables for all users. You can click the path on the top user variable one and edit or double-click, and then you can edit the environment variable, and each path is going to have its own line. So, click new, and paste the path that we found earlier, and then you can move it to the top so that it's the first one that's searched. Now, in this view, if you have other problems related to your path environment variable, you can use this window to delete things, edit things, or reorder things. So, now that it's there, let's click okay, okay, okay, and you won't be able to use the Python command just yet. It's still not going to be found, but if you open up a new terminal, PowerShell view, now the Python command is located. If you weren't able to find python.exe at any of those previous locations, here are a few other things you can try. If you have pi launcher installed, there is a list paths option that will print out the locations of any Python versions that were found. So, you can try pi -- list paths, and for each version that it finds, it will point to where that file actually lives on your computer. And make sure when you're adding to path, you're just adding the folder, not the file itself. Another option is using the file explorer to search. So, navigate to the drive or location you want to search in. I'm going to look in local disk C, and on the top right, there's a search bar, and you can look up python.exe, but note this does take a while. On my computer, it takes about a minute and a half to see all of the results. Another option you might choose is to download another application that searches your computer faster. I've got everything installed. The download link for it is at voidtools.com/downloads, and this is what it looks like, and I can search for python.exe, and you can see it gives me those search results super fast. If you don't see any results, or you don't see any files being searched at all, then you can go to tools, options, folders, and make sure that there's actually a folder here that it's searching in. I've got my C drive, you might need to add yours. And then you have to figure out what file you actually are looking for. It should match python.exe exactly, and the folder location should look like it's at the root location of a Python program, like this one in Python 3.13, and not this one inside of Windows apps, Microsoft, and then a bunch of random numbers and letters. And then you can also double-click on this file, make sure it opens up python.exe, and you found your Python location. Okay, so now that you know how to modify path, in the next lesson, you'll learn how to view path and other environment variables directly from the command line.

Original Description

Download your free Python Cheat Sheet here: https://realpython.com/cheatsheet Free Python Skill Test with instant level + learning plan: https://realpython.com/skill-test Want to learn faster? Become a Python Expert with unlimited access to 5,000+ tutorials, videos, and exercises: https://realpython.com/start This is a preview of the video course, "Adding Python to PATH". You may need to add Python to PATH if you’ve installed Python, but typing python on the command line doesn’t seem to work. You might see a message saying that python isn’t recognized, or you might end up running the wrong version of Python. A common fix for these problems is adding Python to the PATH environment variable. In this video course, you’ll learn how to add Python to PATH. You’ll also learn what PATH is and why it’s essential for tools like the command line to be able to find your Python installation. This is a portion of the complete course, which you can find here: https://realpython.com/courses/adding-python-to-path/ The rest of the course covers: - Working With PATH on Windows in the Command Line - Adding Python to PATH on macOS - Adding Python to PATH on Linux - Managing PATH on Mac and Linux - Adding Python to PATH (Quiz) 🐍 Become a Python expert with real-world tutorials, on-demand courses, interactive quizzes, and 24/7 access to a community of experts at https://realpython.com ▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰ 🐍 Start Here → https://realpython.com/start 🗺️ Guided Learning Paths → https://realpython.com/learning-paths 🎧 Real Python Podcast → https://realpython.com/podcast 📚 Python Books → https://realpython.com/books 📖 Python Reference → https://realpython.com/ref 🧑‍💻 Quizzes & Exercises → https://realpython.com/quizzes 🎓 Live Courses: https://realpython.com/live ⭐️ Reviews & Learner Stories: https://realpython.com/learner-stories ▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Real Python · Real Python · 0 of 60

← Previous Next →
1 A better Python REPL – bpython vs python interpreter
A better Python REPL – bpython vs python interpreter
Real Python
2 Introducing large-type.com – A Utility Website
Introducing large-type.com – A Utility Website
Real Python
3 Reading Hacker News Without Wasting Tons of Time
Reading Hacker News Without Wasting Tons of Time
Real Python
4 Forward References and Python 3 Type Hints
Forward References and Python 3 Type Hints
Real Python
5 Using Sublime Text as your Git Editor
Using Sublime Text as your Git Editor
Real Python
6 Python Code Linting and Auto-Complete for Sublime Text
Python Code Linting and Auto-Complete for Sublime Text
Real Python
7 Make your Python Code More Readable with Custom Exceptions
Make your Python Code More Readable with Custom Exceptions
Real Python
8 Write Better Tests with Sublime Text's Split Layout Feature
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
9 How to Use Sublime Text from the Command Line
How to Use Sublime Text from the Command Line
Real Python
10 Rename Variables with Multiple Selection in Sublime Text
Rename Variables with Multiple Selection in Sublime Text
Real Python
11 Sublime Text Settings for Writing PEP 8 Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
12 Write Cleaner Python with Sublime Text's Indent Guides
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
13 Sublime Text Whitespace Settings for Python Development
Sublime Text Whitespace Settings for Python Development
Real Python
14 Function Argument Unpacking in Python
Function Argument Unpacking in Python
Real Python
15 Python Code Review: Debugging and Refactoring "Conway's Game of Life" +  Automated Tests
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
16 Using "get()" to Return a Default Value from a Python Dict
Using "get()" to Return a Default Value from a Python Dict
Real Python
17 A Python Shorthand for Swapping Two Variables
A Python Shorthand for Swapping Two Variables
Real Python
18 Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
19 Click & Jump to Test Failures from the Command Line (iTerm2)
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
20 Setting up Sublime Text for Python Developers
Setting up Sublime Text for Python Developers
Real Python
21 Sublime Text + Python Guide Overview
Sublime Text + Python Guide Overview
Real Python
22 Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
23 Type-Checking Python Programs With Type Hints and mypy
Type-Checking Python Programs With Type Hints and mypy
Real Python
24 A Shorthand for Merging Dictionaries in Python 3.5+
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
25 Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
26 My Python Code Looks Ugly and Confusing – Help!
My Python Code Looks Ugly and Confusing – Help!
Real Python
27 Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
28 Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
29 Programmer Portfolio – Example and Walkthrough
Programmer Portfolio – Example and Walkthrough
Real Python
30 How to Get Your 1st Speaking Gig at a Tech Conference
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
31 How to Build Your Public Speaking Skills as a Developer
How to Build Your Public Speaking Skills as a Developer
Real Python
32 The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
33 Setting up Sublime Text for Python Developers – Lesson #1
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
34 Cool New Features in Python 3.6
Cool New Features in Python 3.6
Real Python
35 "is" vs "==" in Python – What's the Difference? (And When to Use Each)
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
36 Emulating switch/case Statements in Python with Dictionaries
Emulating switch/case Statements in Python with Dictionaries
Real Python
37 Python Function Argument Unpacking Tutorial (* and ** Operators)
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
38 What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
39 A Crazy Python Dictionary Expression ?!
A Crazy Python Dictionary Expression ?!
Real Python
40 String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
41 Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
42 Optional Arguments in Python With *args and **kwargs
Optional Arguments in Python With *args and **kwargs
Real Python
43 Python Context Managers and the "with" Statement (__enter__ & __exit__)
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
44 Installing Python Packages with pip and virtualenv / venv
Installing Python Packages with pip and virtualenv / venv
Real Python
45 "For Each" Loops in Python with enumerate() and range()
"For Each" Loops in Python with enumerate() and range()
Real Python
46 Python Code Review: LibreOffice Automation and the Python Standard Library
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
47 Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
48 Python Tutorial: List Comprehensions Step-By-Step
Python Tutorial: List Comprehensions Step-By-Step
Real Python
49 Leveraging Python's Implicit "return None" Statements
Leveraging Python's Implicit "return None" Statements
Real Python
50 What's the meaning of underscores (_ & __) in Python variable names?
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
51 Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
52 Writing automated tests for Python command-line apps and scripts
Writing automated tests for Python command-line apps and scripts
Real Python
53 How to find great Python packages on PyPI, the Python Package Repository
How to find great Python packages on PyPI, the Python Package Repository
Real Python
54 Immutable vs Mutable Objects in Python
Immutable vs Mutable Objects in Python
Real Python
55 PyPI vs Warehouse, the Next-Generation Python Package Repository
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
56 pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
57 My Experience at PyCon 2017 in Portland
My Experience at PyCon 2017 in Portland
Real Python
58 Pylint Tutorial – How to Write Clean Python
Pylint Tutorial – How to Write Clean Python
Real Python
59 "Reverse a List in Python" Tutorial: Three Methods & How-to Demos
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
60 Python Refactoring: "while True" Infinite Loops & The "input" Function
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →