Skip to content

Setting Up Path and Environment Variables for Python

To use Python effectively from the command line or terminal, you need to set up the system’s PATH and environment variables. Below is a guide for configuring these variables on Windows, macOS, and Linux.


1. What is the PATH Environment Variable?

  • The PATH is an environment variable that specifies directories where executable programs are located.
  • Adding Python to PATH allows you to run python commands from any command prompt or terminal without needing to specify the full installation directory.

2. Setting Up PATH in Windows

Automatically (During Installation)

  1. During Python installation, check the box “Add Python to PATH” on the setup screen.
  2. This step automatically configures the PATH and sets up Python.

Manually

If you forgot to check “Add Python to PATH,” you can add it manually:

  1. Find Python Installation Path:
    1. Locate where Python is installed (e.g., C:\Python39 or C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python39).
  2. Add to PATH:
    1. Open Start Menu and search for Environment Variables.
    1. Click Edit the system environment variables.
    1. In the System Properties window, click Environment Variables.
    1. Under System Variables, select the variable Path and click Edit.
    1. Click New and add the Python installation directory (e.g., C:\Python39) and the Scripts directory (e.g., C:\Python39\Scripts).
  3. Verify PATH Setup:
    1. Open Command Prompt and type:

python –version

pip –version


3. Setting Up PATH in macOS

Automatically (During Installation)

If you installed Python using the official installer, PATH is often configured automatically.

Manually

  1. Locate Python Installation Path:
    1. Python is often installed in /usr/local/bin or /Library/Frameworks/Python.framework/Versions/3.x/bin.
  2. Edit Shell Configuration File:
    1. Open the terminal and edit the shell configuration file:
      1. For zsh (default shell in macOS 10.15+):

nano ~/.zshrc

  • For bash:

nano ~/.bash_profile

  • Add the following line:

export PATH=”/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.x/bin:$PATH”

  • Apply Changes:
    • Save the file and run:

source ~/.zshrc

# or

source ~/.bash_profile

  • Verify PATH Setup:
    • Type:

python3 –version

pip3 –version


4. Setting Up PATH in Linux

Automatically (Package Manager Installations)

  • If you installed Python using a package manager (apt, yum, etc.), PATH is often configured automatically.

Manually

  1. Locate Python Installation Path:
    1. Common paths: /usr/bin/python3, /usr/local/bin/python3.
  2. Edit Shell Configuration File:
    1. Open the terminal and edit:

nano ~/.bashrc

# or

nano ~/.zshrc

  • Add the following line:

export PATH=”/usr/local/bin:/usr/bin:$PATH”

  • Apply Changes:
    • Save the file and run:

source ~/.bashrc

# or

source ~/.zshrc

  • Verify PATH Setup:
    • Run:

python3 –version

pip3 –version


5. Setting Environment Variables

In some cases, additional environment variables may need to be set for specific Python features.

Windows

  1. Go to Environment Variables (as described above).
  2. Under System Variables, click New and add:
    1. Variable Name: PYTHONPATH
    1. Variable Value: Path to the Python libraries or modules (e.g., C:\Python39\Lib).

macOS/Linux

  1. Add the variable in the shell configuration file:

export PYTHONPATH=”/path/to/python/libraries”

  • Apply changes with:

source ~/.bashrc


6. Testing the Configuration

  1. Open a new terminal or command prompt.
  2. Run the following commands to ensure Python and pip are accessible globally:

python –version

pip –version

  • Execute a small Python script:

python -c “print(‘Python is set up correctly!’)”


Conclusion

Configuring the PATH and environment variables ensures Python is easily accessible for development tasks. It simplifies running Python commands and helps avoid issues with package installations or module imports. Properly setting up these variables is crucial for a smooth Python programming experience.