Get AI summaries of any video or article — Sign up free
Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Mac & Linux) thumbnail

Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Mac & Linux)

Corey Schafer·
5 min read

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.

TL;DR

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?

These systems often ship with Python 2, and the command name `python` may still point to that older executable. Installing Python 3 frequently creates a separate command like `python3`. Checking `python3` (and using `type python3` / `which python3`) reveals which interpreter is actually available and where it lives.

How does PATH determine which Python executable runs when multiple versions exist?

PATH is a colon-separated list of directories the shell searches in order. When a command like `python3` is typed, the shell checks each directory in PATH sequentially and uses the first match it finds. That means if one Python executable appears earlier in PATH (e.g., `/usr/local/bin`) than another (e.g., `/usr/bin`), the earlier one wins.

What’s the difference between using `which` and `type` for locating executables?

Both help locate what the shell will run. `which python3` prints the resolved path, while `type python3` can also indicate when something is a shell alias or “hashed” command and then shows the location. The transcript prefers `type` because it works better when aliases are involved.

How can import errors happen even when `pip install` reports success?

pip and Python can point to different interpreters. If PATH causes `pip` to install into one Python’s `site-packages` but imports run under another Python, imports fail. The transcript’s fix is to compare `sys.executable` (the interpreter currently running) with `pip show <package>`’s Location (where pip installed the package). If those locations don’t match, the mismatch explains the import error.

How do aliases help, and why must they be made permanent?

Aliases like `alias python=python3` and `alias pip=pip3` redirect the `python` and `pip` commands to the Python 3 equivalents. However, aliases defined in a terminal session disappear after restarting the terminal, so they must be added to `~/.bash_profile` (or `~/.bashrc` on Linux) to persist.

Why might Sublime Text (or another editor) use a different Python than the terminal?

Editors often use their own configuration mechanism. In Sublime Text, build systems can specify a Python executable path, independent of the terminal’s PATH. If the build system points to a different interpreter (e.g., Python 3.6 instead of the intended version), features like f-strings may fail. Creating or switching build systems to point at the correct Python executable or virtual environment resolves this.

Review Questions

  1. When `python` runs Python 2 but `python3` runs Python 3, what specific PATH behavior explains the difference?
  2. How would you verify that a failing `import` is happening under the same interpreter that pip used to install the package?
  3. What changes are needed in `~/.bash_profile` to prepend a Python bin directory to PATH without overwriting the existing PATH?

Key Points

  1. 1

    Use `python3` (not just `python`) on macOS/Linux when Python 2 is still the default `python` command.

  2. 2

    Confirm the exact interpreter and executable location with `which python3`, `type python3`, and `sys.executable`.

  3. 3

    Treat PATH as an ordered search list: the first directory containing the executable wins.

  4. 4

    Fix wrong interpreter selection by prepending the intended Python bin directory in `~/.bash_profile` (or `~/.bashrc`) and restarting the terminal.

  5. 5

    If `pip install` succeeds but imports fail, compare `pip show <package>`’s Location with the active interpreter’s `site-packages` directory.

  6. 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. 7

    Configure editors (like Sublime Text build systems) separately so they use the same Python executable or virtual environment as the terminal.

Highlights

PATH order—not just installed versions—determines which Python executable runs when you type `python` or `python3`.
`sys.executable` is a reliable way to confirm which interpreter is actually executing your code, even inside virtual environments.
Import errors after `pip install` often come from pip and Python targeting different interpreters; `pip show` plus `sys.executable` exposes the mismatch.
Aliases can redirect `python` and `pip` to Python 3 equivalents, but they must be saved in `~/.bash_profile` to persist.
Editors may ignore terminal PATH; Sublime Text build systems can point to a different Python executable unless explicitly updated.

Topics

Mentioned