Ova

How to add Python in Environment Variables?

Published in Python Environment Variables 6 mins read

Adding Python to your environment variables is crucial for executing Python scripts from any directory in your command line and for helping Python locate custom modules you've developed or installed. This process typically involves two main environment variables: the Path variable and the PYTHONPATH variable.

Understanding Key Environment Variables

Before diving into the steps, it's essential to distinguish between the two primary environment variables related to Python:

  • Path (or PATH on Linux/macOS): This system variable lists directories where the operating system should look for executable files when you type a command. Adding Python's installation directory (containing python.exe) and its Scripts directory (containing pip.exe and other executables) to Path allows you to run python, pip, or any Python-installed command directly from any command prompt window.
  • PYTHONPATH: This variable tells Python where to look for modules and packages in addition to its standard library paths. It's especially useful for development, allowing you to include custom module directories without installing them globally or modifying Python's internal search paths.
Variable Name Purpose Example Use Case
Path Allows running Python executables (python, pip) from anywhere. Typing python my_script.py in Command Prompt or Terminal.
PYTHONPATH Specifies additional directories for Python to find modules. Importing a custom module located at C:\MyPythonProjects\my_lib.

How to Add Python to Environment Variables on Windows

The most common way to add Python to your environment variables on Windows is through the graphical user interface (GUI) or via the Command Prompt for more specific needs like setting PYTHONPATH.

1. Adding Python to the Path Variable (GUI Method)

This is the recommended method for making Python globally accessible from your command line.

  1. Search for Environment Variables: Type "environment variables" in the Windows search bar and select "Edit the system environment variables."
  2. Open Environment Variables Dialog: In the System Properties window, click the "Environment Variables..." button.
  3. Edit System Path:
    • Under the "System variables" section, find and select the variable named Path.
    • Click "Edit...".
  4. Add Python Paths:
    • In the "Edit environment variable" dialog box, click "New" and add the full path to your Python installation directory. For example, if Python 3.9 is installed, you might add:
      • C:\Python39
    • Click "New" again and add the path to the Python Scripts directory, which contains pip:
      • C:\Python39\Scripts
      • Note: Replace Python39 with your actual Python version and installation directory (e.g., Python310, Python311).
  5. Confirm Changes: Click "OK" on all open dialog boxes (Edit Environment Variable, Environment Variables, System Properties) to save the changes.
  6. Verify Installation: Open a new Command Prompt or PowerShell window (existing windows won't have the updated Path). Type python --version and pip --version. You should see the installed Python and pip versions. If not, double-check your paths.

2. Defining the PYTHONPATH Variable

This is useful for telling Python where to find custom modules you've written or downloaded.

A. Using the GUI Method (Recommended for most users)

  1. Open Environment Variables: Follow steps 1 and 2 from the "Adding Python to the Path Variable" section above.
  2. Create/Edit PYTHONPATH Variable:
    • Under "System variables" (for all users) or "User variables" (for your current user account), click "New...".
    • For Variable name, type: PYTHONPATH
    • For Variable value, enter the full path(s) to your custom module directories, separated by semicolons (;). For example:
      • C:\MyProjects\PythonModules;D:\SharedLibraries
    • If PYTHONPATH already exists, select it and click "Edit...", then add your new paths.
  3. Confirm Changes: Click "OK" on all open dialog boxes to save.

B. Using Command Prompt (setx) for Permanent PYTHONPATH

You can also set PYTHONPATH permanently from the Command Prompt using the setx command. This method directly relates to the concept of permanent environment variables, as mentioned in the internal references.

  • For a System-wide PYTHONPATH (all users):
    Open Command Prompt as an administrator and type:
    setx /M PYTHONPATH "C:\path\to\your\custom_modules;C:\another\custom\directory"
    • Example using the reference's format: If you needed to point to Python's own library directory (though typically PYTHONPATH is for additional custom paths), it might look like:
      setx /M PYTHONPATH "C:\Python27\Lib"
      • Important Note: While the above C:\Python27\Lib example demonstrates the setx /M command for PYTHONPATH for a specific Python version, PYTHONPATH is usually set to directories containing your own custom Python modules rather than the standard library. Python already knows where its core library is located.
  • For a User-specific PYTHONPATH (current user only):
    Open a regular Command Prompt and type:
    setx PYTHONPATH "C:\path\to\your\custom_modules;C:\another\custom\directory"
    • After using setx, you'll need to open a new Command Prompt window for the changes to take effect.

How to Add Python to Environment Variables on Linux/macOS

On Unix-like systems (Linux, macOS), environment variables are typically managed by modifying shell configuration files.

1. Adding Python to the PATH Variable

  1. Open Shell Configuration File: Open your terminal and edit your shell's configuration file. Common files include:
    • ~/.bashrc (for Bash shell)
    • ~/.bash_profile (for Bash, often sourced by .bashrc)
    • ~/.zshrc (for Zsh shell, default on recent macOS versions)
    • Use a text editor like nano or vi:
      nano ~/.bashrc

      or

      nano ~/.zshrc
  2. Add Python Path: Add the following line to the end of the file, replacing /usr/local/bin/python with your actual Python executable path if it's different (e.g., /usr/bin/python3, or a path from a virtual environment):
    export PATH="/usr/local/bin/python:$PATH"
    • If you're using pyenv or miniconda/anaconda, they handle PATH management automatically.
  3. Save and Exit: Save the file (Ctrl+O, Enter in nano) and exit (Ctrl+X in nano).
  4. Source the File: Apply the changes to your current terminal session by sourcing the file:
    source ~/.bashrc

    or

    source ~/.zshrc
  5. Verify: Type python --version or python3 --version and which python or which python3.

2. Defining the PYTHONPATH Variable

  1. Open Shell Configuration File: Same as step 1 for PATH (e.g., nano ~/.bashrc).
  2. Add PYTHONPATH: Add the following line to the end of the file, replacing /path/to/your/custom_modules with your actual directory:
    export PYTHONPATH="/path/to/your/custom_modules:$PYTHONPATH"
    • You can add multiple paths separated by colons (:).
  3. Save and Exit: Save and exit the file.
  4. Source the File: Apply changes:
    source ~/.bashrc

    or

    source ~/.zshrc
  5. Verify: Type echo $PYTHONPATH to see the set variable.

Practical Tips and Verification

  • New Terminal: Always open a new Command Prompt or Terminal window after making changes to environment variables. Existing windows might not reflect the updates.
  • Virtual Environments: For project-specific dependencies, it's highly recommended to use Python virtual environments. These create isolated Python installations, preventing conflicts between project requirements and removing the need to modify system-wide PYTHONPATH for specific projects.
  • Checking Variables:
    • Windows:
      • echo %PATH%
      • echo %PYTHONPATH%
      • where python
    • Linux/macOS:
      • echo $PATH
      • echo $PYTHONPATH
      • which python
  • Prioritization: In the Path variable, the order of directories matters. The system searches them from left to right. If you have multiple Python installations, placing your preferred version's path earlier in the list ensures it's found first.

By correctly configuring these environment variables, you ensure a smoother and more efficient Python development workflow.