cyberangles blog

How to Fix Python 2.7 Referencing Python 3.3: Resolving 'site.py' SyntaxError When Using Multiple Python Versions

If you’ve ever encountered a SyntaxError related to site.py when running Python 2.7—especially mentioning invalid syntax like print('...')—you’re likely facing a version conflict: Python 2.7 is incorrectly referencing files from Python 3.3 (or another Python 3.x version). This issue arises due to misconfigured paths, overlapping environment variables, or ambiguous version references, leading Python 2.7 to load Python 3-specific code it can’t parse.

Python 2.7 and Python 3.x have critical syntax differences (e.g., print as a statement vs. a function), so even a single Python 3-style line in site.py (a core module that initializes the Python environment) will crash Python 2.7. In this guide, we’ll break down the root causes and walk through step-by-step solutions to resolve this error, ensuring Python 2.7 runs independently of Python 3.3.

2026-02

Table of Contents#

  1. Understanding the 'site.py' SyntaxError
  2. Common Causes of Version Conflicts
  3. Step-by-Step Solutions
  4. Preventive Measures to Avoid Future Conflicts
  5. Conclusion
  6. References

Understanding the 'site.py' SyntaxError#

What is site.py?#

site.py is a built-in Python module that initializes the runtime environment. It sets up paths for importing modules, configures sys.path (the list of directories Python searches for modules), and loads site-specific configuration. Every Python installation has its own site.py tailored to its version.

Why the SyntaxError Occurs#

Python 2.7 uses Python 2 syntax (e.g., print "Hello" as a statement), while Python 3.x uses print("Hello") as a function. If Python 2.7 accidentally loads Python 3.3’s site.py, it will encounter Python 3 syntax and throw an error like this:

File "/usr/lib/python3.3/site.py", line 176  
    print('Error processing line 1 of ...')  
          ^  
SyntaxError: invalid syntax  

This confirms Python 2.7 is referencing Python 3.3’s site.py instead of its own.

Common Causes of Version Conflicts#

Before fixing the error, let’s identify why Python 2.7 might point to Python 3.3:

  1. Misconfigured PATH Environment Variable: Python 3.3’s installation directory is listed before Python 2.7’s in PATH, so python commands default to Python 3.3.
  2. Ambiguous Shebang Lines: Scripts use #!/usr/bin/env python (which picks the first python in PATH, possibly Python 3.3) instead of explicitly specifying python2.7.
  3. Polluted PYTHONPATH: The PYTHONPATH environment variable includes Python 3.3 directories, forcing Python 2.7 to load modules from there.
  4. Corrupted Python 2.7 Installation: Python 2.7’s site.py is missing or overwritten, causing it to fall back to Python 3.3’s version.
  5. Overlapping Installation Paths: Python 2.7 and 3.3 are installed in overlapping directories (e.g., both in /usr/local/bin), leading to path confusion.

Step-by-Step Solutions#

Follow these steps to resolve the site.py SyntaxError by isolating Python 2.7 from Python 3.3.

1. Verify Python Installations#

First, confirm where Python 2.7 and 3.3 are installed to check for path overlaps.

For Linux/macOS:#

Run these commands to locate each Python version:

# Find Python 2.7 executable  
which python2.7  
# Example output: /usr/bin/python2.7  
 
# Find Python 3.3 executable  
which python3.3  
# Example output: /usr/local/bin/python3.3  

For Windows:#

Use where.exe in Command Prompt:

where python2.7  
where python3.3  

If both versions share the same parent directory (e.g., C:\Python\), this may cause conflicts. Note the paths for later steps.

2. Inspect the PATH Environment Variable#

The PATH variable dictates which python executable runs when you type python in the terminal. If Python 3.3’s path comes before Python 2.7’s, python will default to 3.3, and Python 2.7 may inherit incorrect paths.

Check PATH#

  • Linux/macOS: Run echo $PATH to list directories in order.
  • Windows: Run echo %PATH% in Command Prompt.

Look for Python 3.3 paths (e.g., /usr/local/bin or C:\Python33\) appearing before Python 2.7 paths (e.g., /usr/bin or C:\Python27\).

Fix PATH (Linux/macOS)#

To prioritize Python 2.7, edit your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

  1. Open the file:

    nano ~/.bashrc  
  2. Add Python 2.7’s path before Python 3.3’s path. For example:

    export PATH="/usr/bin/python2.7:$PATH"  # Add Python 2.7 path first  
  3. Save and reload:

    source ~/.bashrc  

Fix PATH (Windows)#

  1. Open Control Panel > System > Advanced System Settings > Environment Variables.
  2. Under "System Variables", select Path and click "Edit".
  3. Move Python 2.7’s path (e.g., C:\Python27\) above Python 3.3’s path (e.g., C:\Python33\).
  4. Click "OK" to save changes.

3. Correct Shebang Lines in Scripts#

Scripts using #!/usr/bin/env python may accidentally run with Python 3.3. Explicitly specify python2.7 in the shebang:

Before (problematic):

#!/usr/bin/env python  # May use Python 3.3  
print "Hello"  # Python 2 syntax (will fail in Python 3)  

After (fixed):

#!/usr/bin/env python2.7  # Explicitly uses Python 2.7  
print "Hello"  

4. Locate Python 2.7’s site.py and Check Paths#

Python 2.7 should load site.py from its own lib directory (e.g., /usr/lib/python2.7/site.py on Linux). Use the site module to verify:

Run Python 2.7 and check its site paths:

python2.7 -m site  

Expected Output (Linux/macOS):

sys.path = [  
    '/home/user',  
    '/usr/lib/python2.7',  
    '/usr/lib/python2.7/plat-linux2',  
    '/usr/lib/python2.7/lib-tk',  
    ...  
]  

If you see Python 3.3 paths (e.g., /usr/lib/python3.3), Python 2.7 is loading from the wrong directory.

5. Fix Misconfigured sys.path or PYTHONPATH#

sys.path (Python’s module search path) is populated from PATH, PYTHONPATH, and default installation directories. If PYTHONPATH includes Python 3.3 paths, Python 2.7 will use them.

Check PYTHONPATH#

Run:

# Linux/macOS  
echo $PYTHONPATH  
 
# Windows  
echo %PYTHONPATH%  

If PYTHONPATH includes Python 3.3 directories (e.g., /usr/local/lib/python3.3/site-packages), clear it:

# Linux/macOS: Temporarily unset PYTHONPATH  
unset PYTHONPATH  
 
# Windows: Temporarily unset PYTHONPATH  
set PYTHONPATH=  

Permanently Remove Python 3.3 from PYTHONPATH:

  • Linux/macOS: Edit ~/.bashrc and remove lines adding Python 3.3 paths to PYTHONPATH.
  • Windows: In "Environment Variables", delete Python 3.3 entries from PYTHONPATH.

6. Reinstall or Repair Python 2.7#

If Python 2.7’s site.py is missing or corrupted, reinstall it:

  1. Download Python 2.7: Use the official Python 2.7 archive (note: Python 2.7 is end-of-life, but this is for legacy use cases).
  2. Reinstall with Default Paths: During installation, ensure "Add python.exe to Path" is checked (Windows) or use default directories (Linux/macOS).
  3. Verify Post-Installation: Run python2.7 -m site again to confirm sys.path only includes Python 2.7 directories.

7. Isolate Versions with Virtual Environments#

The best long-term fix is to isolate Python versions using virtual environments. For Python 2.7, use virtualenv (Python 3’s venv is not compatible):

Step 1: Install virtualenv for Python 2.7#

# Install virtualenv using Python 2.7's pip  
python2.7 -m pip install virtualenv  

Step 2: Create a Python 2.7 Virtual Environment#

virtualenv -p python2.7 my_py27_env  

Step 3: Activate the Environment#

# Linux/macOS  
source my_py27_env/bin/activate  
 
# Windows  
my_py27_env\Scripts\activate  

Now, python will default to Python 2.7 within the environment, avoiding conflicts with Python 3.3.

Preventive Measures to Avoid Future Conflicts#

To prevent version mix-ups:

  • Use Explicit Version Calls: Always run python2.7 or python3.3 instead of python to avoid ambiguity.
  • Isolate with Virtual Environments: Use virtualenv (Python 2.7) or venv (Python 3.x) to keep environments separate.
  • Avoid Global PYTHONPATH: Never set PYTHONPATH globally unless necessary; use virtual environments instead.
  • Clean Up PATH: Keep PATH minimal, with only essential Python directories, and order them by priority (e.g., Python 2.7 first if needed).

Conclusion#

Resolving the site.py SyntaxError in Python 2.7 requires isolating it from Python 3.3 by fixing paths, environment variables, and version references. Start by verifying installations and PATH, then correct shebang lines, PYTHONPATH, and sys.path. For long-term stability, use virtual environments to keep versions separate.

While Python 2.7 is no longer supported, these steps ensure legacy code runs without version conflicts. Always prioritize migrating to Python 3.x for security and compatibility.

References#