Here is a clear, complete, and exam-ready explanation of Setting up PATH and Environment Variables for Python, suitable for BCA, MCA, B.Tech, and competitive exams.
Setting up PATH and Environment Variables
To run Python from any directory in the command prompt (or terminal), we need to set up the PATH environment variable.
This allows the operating system to locate Python easily without specifying its full installation path.
1. What is PATH?
PATH is a system environment variable that stores a list of directory locations where executables (programs) are located.
If Python’s installation folder is added to PATH:
- You can run
pythonorpipfrom any location in the command prompt. - No need to type the full path like:
C:\Users\ABC\AppData\Local\Programs\Python\Python311\python.exe
2. Why is Setting PATH Important?
Without setting PATH:
pythoncommand will NOT work.- System will show:
'python' is not recognized as an internal or external command - You must always navigate to the installation directory to run Python.
With PATH correctly set:
- You can run Python from anywhere.
- pip works from any directory.
- IDEs and tools detect Python automatically.
3. Setting up PATH Automatically during Installation (Windows)
When installing Python on Windows, the easiest method is:
Check the box:
☑ Add Python to PATH
This automatically sets:
- Path to
python.exe - Path to
pip.exe - Environment variable
PYTHONPATH
Recommended for beginners.
4. Setting up PATH Manually (Windows)
Follow these steps if the PATH was not set during installation:
Step 1: Find Python Installation Directory
Typical installation paths:
C:\Users\<username>\AppData\Local\Programs\Python\Python312\
Inside this folder you will find:
python.exeScripts\(contains pip.exe)
Step 2: Open Environment Variables Settings
- Right-click This PC → Properties.
- Click Advanced system settings.
- Click Environment Variables.
Step 3: Edit the PATH Variable
In the System Variables section:
- Select Path → Click Edit.
- Click New.
- Add the following two paths:
Example:
C:\Users\<username>\AppData\Local\Programs\Python\Python312\
C:\Users\<username>\AppData\Local\Programs\Python\Python312\Scripts\
Step 4: Save and Close
Click:
- OK → OK → OK
Restart Command Prompt (if open).
Step 5: Verify the PATH
Open Command Prompt and type:
python --version
pip --version
If Python is in PATH correctly, versions will display.
5. Setting up PATH on macOS
macOS usually installs Python 3 with the command python3.
To check:
python3 --version
To set the PATH manually:
Step 1: Find Python Wherabouts
Usually installed in:
/Library/Frameworks/Python.framework/Versions/3.xx/bin
Step 2: Edit Shell Configuration
Open the terminal and type:
For zsh (latest macOS versions):
nano ~/.zshrc
For bash:
nano ~/.bash_profile
Step 3: Add the path
Example:
export PATH="/Library/Frameworks/Python.framework/Versions/3.12/bin:$PATH"
Step 4: Save and Apply
source ~/.zshrc
Check:
python3 --version
6. Setting PATH on Linux (Ubuntu/Debian)
Python comes preinstalled on Linux.
To verify:
python3 --version
If you installed a different version manually, add it to PATH:
Step 1: Open bash profile
nano ~/.bashrc
Step 2: Add path
export PATH="/usr/local/bin/python3.12:$PATH"
Step 3: Apply changes
source ~/.bashrc
7. What is PYTHONPATH?
PYTHONPATH is another environment variable used to tell Python where to look for modules and packages.
You modify PYTHONPATH when:
- Using custom modules not placed in default directories
- Working on advanced projects
Example:
PYTHONPATH = C:\myprojects\mymodules\
Not required for beginners.
8. PATH vs PYTHONPATH (Difference)
| PATH | PYTHONPATH |
|---|---|
| Used by OS | Used by Python interpreter |
| Helps run python/pip commands | Helps import modules |
| Points to python.exe | Points to module directories |
9. Summary
- PATH allows system-wide access to Python and pip.
- Set PATH automatically by checking Add Python to PATH.
- You can also configure it manually via Environment Variables.
- PYTHONPATH is used only for custom module locations.
