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\
) orC:\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 runningwhich 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:
- Search for "environment variables" in the Start Menu and select "Edit the system environment variables."
- Click the "Environment Variables..." button.
-
macOS:
- 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
).
- 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.,
-
Linux:
- Similarly to macOS, you'll generally modify shell configuration files (e.g.,
.bashrc
,.zshrc
).
- Similarly to macOS, you'll generally modify shell configuration files (e.g.,
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:
- Select the "Path" variable and click "Edit...".
- Click "New" and add the Python installation directory (e.g.,
C:\Python39\
). - Click "New" again and add the
Scripts
directory (e.g.,C:\Python39\Scripts\
). - Click "OK" on all windows.
-
macOS / Linux:
- Open your shell configuration file (e.g.,
nano ~/.zshrc
,nano ~/.bashrc
). - 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 innano
).
4. Runsource ~/.zshrc
orsource ~/.bashrc
(depending on the file you edited) to apply the changes to your current shell session, or close and reopen your terminal. - Open your shell configuration file (e.g.,
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.