askvity

How Do I Add Python to a Path After Installation?

Published in Python Configuration 3 mins read

To add Python to your system's PATH environment variable after installation, follow these steps, which will allow you to run Python from any command prompt or terminal window:

1. Find Your Python Installation Directory

First, you need to locate where Python is installed on your computer. Common locations include:

  • Windows: C:\PythonXX\ (where XX is the Python version, e.g., C:\Python39\) or C:\Users\[YourUsername]\AppData\Local\Programs\Python\PythonXX\
  • macOS: /Library/Frameworks/Python.framework/Versions/XX/ (usually, XX refers to the version number)
  • Linux: /usr/bin/python3 (often a symbolic link), you can find the actual location by running which python3 in your terminal. Check /usr/local/bin and /opt/python also.

Important: You'll also want to add the Scripts subdirectory (e.g., C:\Python39\Scripts\) to your PATH, as this is where pip and other executable scripts are located.

2. Access System Environment Variables

The method for accessing system environment variables differs based on your operating system:

  • Windows:

    1. Search for "environment variables" in the Start Menu and select "Edit the system environment variables."
    2. Click the "Environment Variables..." button.
  • macOS:

    1. macOS usually doesn't provide a GUI to edit environment variables easily. You'll likely need to modify your shell's configuration file (e.g., .bash_profile, .zshrc, .bashrc).
  • Linux:

    1. Similarly to macOS, you'll generally modify shell configuration files (e.g., .bashrc, .zshrc).

3. Locate the "Path" Variable

In the System variables section (Windows) or in the configuration file (macOS/Linux), look for a variable named "Path" (Windows) or "PATH" (macOS/Linux). Note the capitalization difference!

4. Edit the "Path" Variable

  • Windows:

    1. Select the "Path" variable and click "Edit...".
    2. Click "New" and add the Python installation directory (e.g., C:\Python39\).
    3. Click "New" again and add the Scripts directory (e.g., C:\Python39\Scripts\).
    4. Click "OK" on all windows.
  • macOS / Linux:

    1. Open your shell configuration file (e.g., nano ~/.zshrc, nano ~/.bashrc).
    2. Add the following line, replacing /path/to/python with the actual path to your Python directory and /path/to/python/scripts with the path to the scripts folder:
    export PATH="/path/to/python:/path/to/python/scripts:$PATH"

    Make sure there are no spaces around the = sign. Using $PATH appends your new paths before the existing paths.
    3. Save the file (Ctrl+X, then Y, then Enter in nano).
    4. Run source ~/.zshrc or source ~/.bashrc (depending on the file you edited) to apply the changes to your current shell session, or close and reopen your terminal.

5. Apply Changes

  • Windows: The changes should take effect immediately for new command prompt windows. You may need to restart your computer for the changes to apply to all applications.

  • macOS / Linux: As mentioned above, run source ~/.zshrc (or the appropriate file) or restart your terminal for the changes to take effect.

6. Verify Python Path

Open a new command prompt or terminal window and type:

python --version

or

python3 --version

If Python is correctly added to your path, it will display the Python version. You can also type pip --version to verify pip is accessible.

Related Articles