Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Mac & Linux)
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Use `python3` (not just `python`) on macOS/Linux when Python 2 is still the default `python` command.
Briefing
Python version mix-ups on Mac and Linux usually come down to one thing: the command line is pointing at the wrong Python executable. When “python” lands on Python 2, “python 3” isn’t found, f-strings fail, or pip-installed packages won’t import, the fix is to identify which interpreter the system is actually running and then adjust your PATH (or your editor settings) so the intended version wins.
A common starting point is the difference between “python” and “python 3” on these systems. Many Macs ship with Python 2, so typing `python` may drop into Python 2 even after installing Python 3. Installing Python 3 often creates a separate `python3` command, so checking `python3` can confirm whether the newer interpreter is available. From there, the workflow becomes about locating executables and understanding why the shell chooses one over another.
To see where a command lives, the transcript uses `which python3` and `type python3`. Those commands reveal the executable path—often something like `/usr/local/bin/python3`—and the key reason it works is that directory order matters in the PATH environment variable. PATH is a colon-separated list of directories the shell searches in sequence. If multiple versions exist (for example, one in `/usr/bin` and another in `/usr/local/bin`), the first match in PATH gets executed. The tutorial also demonstrates running Python by full path (e.g., `/usr/local/bin/python3`) to bypass PATH ambiguity.
When PATH doesn’t include a desired install location—such as an Anaconda install—full paths still work. In the example, Anaconda’s Python executables live under an `anaconda/bin` directory, and running `/.../anaconda/bin/python` selects a specific Python version (including Python 3.6). If the goal is to make that version reachable via `python` or `python3`, the directory must be added to PATH ahead of other competing entries.
The transcript then walks through editing `~/.bash_profile` (or `~/.bashrc` on Linux) using Nano to prepend the correct Python bin directory to PATH. It emphasizes not overwriting PATH accidentally: the correct pattern appends the existing `$PATH` using `${PATH}` and then exports the updated variable. After restarting the terminal, `type python3` and `echo $PATH` confirm that the shell now resolves `python3` to the intended location.
To make the workflow smoother, aliases can map `python` to `python3` and `pip` to `pip3`, and those aliases can be made permanent by adding them to the same bash profile. For deeper debugging, the tutorial uses Python’s standard library module `sys` to print `sys.executable`, which reveals the exact interpreter currently running. This becomes crucial for import errors: pip and Python are separate commands, so `pip install Django` may install into a different Python’s `site-packages` than the one being used for imports. Using `pip show <package>` and comparing its “Location” with `sys.executable`’s `site-packages` directory pinpoints mismatches.
Finally, the transcript addresses editor confusion: even if the terminal uses the right Python, editors like Sublime Text may run a different interpreter via build systems. Creating or switching build systems to point at the correct Python executable (including virtual environments) resolves issues like f-strings not working. Virtual environments themselves automatically adjust PATH when activated, so `which python` and `sys.executable` help verify the active environment’s interpreter.
Cornell Notes
On Mac and Linux, Python “version problems” typically mean the shell or your editor is launching a different Python executable than you expect. The fastest way to diagnose is to locate the interpreter (`which python3`, `type python3`) and inspect PATH order (`echo $PATH`), since the first matching directory wins. You can fix resolution by adding the correct Python bin directory to `~/.bash_profile` (or `~/.bashrc`) and restarting the terminal, or by using full paths and aliases (e.g., `alias python=python3`, `alias pip=pip3`). For import failures, confirm that pip installed packages into the same interpreter’s `site-packages` by comparing `sys.executable` with `pip show <package>`’s Location. Editors may require separate build-system configuration to use the same interpreter or virtual environment.
Why does typing `python` sometimes start Python 2 even after installing Python 3 on macOS/Linux?
How does PATH determine which Python executable runs when multiple versions exist?
What’s the difference between using `which` and `type` for locating executables?
How can import errors happen even when `pip install` reports success?
How do aliases help, and why must they be made permanent?
Why might Sublime Text (or another editor) use a different Python than the terminal?
Review Questions
- When `python` runs Python 2 but `python3` runs Python 3, what specific PATH behavior explains the difference?
- How would you verify that a failing `import` is happening under the same interpreter that pip used to install the package?
- What changes are needed in `~/.bash_profile` to prepend a Python bin directory to PATH without overwriting the existing PATH?
Key Points
- 1
Use `python3` (not just `python`) on macOS/Linux when Python 2 is still the default `python` command.
- 2
Confirm the exact interpreter and executable location with `which python3`, `type python3`, and `sys.executable`.
- 3
Treat PATH as an ordered search list: the first directory containing the executable wins.
- 4
Fix wrong interpreter selection by prepending the intended Python bin directory in `~/.bash_profile` (or `~/.bashrc`) and restarting the terminal.
- 5
If `pip install` succeeds but imports fail, compare `pip show <package>`’s Location with the active interpreter’s `site-packages` directory.
- 6
Use aliases (e.g., `alias python=python3`, `alias pip=pip3`) to make command names consistent, and add them to bash profile for permanence.
- 7
Configure editors (like Sublime Text build systems) separately so they use the same Python executable or virtual environment as the terminal.