4

tl;dr

I accidentally deleted my Python installation’s site.py file. Now, when trying to start Python, it complains,

ImportError: Couldn't find the real 'site' module

I tried downloading a site.py from setuptools but this results in an infinite recursion in the __boot method around the statement imp.load_module('site',stream,path,descr) (line 37).

Is there a way to fix this without reinstalling Python? What is the site.py file supposed to do?

Some background information:

This is on a Linux server with a custom Python installation in my home directory (~/nfs, to be precise). There are other Python installations on the server (not mine – it’s a mess!) but $PATH and $PYTHONPATH are set up in such a way as to find my installation first.

As to why I deleted the site.py file: I tried executing a setuptools setup.py script and the script told me to delete the file because it “was not created by setuptools”. I foolishly complied.

I suspect that this original error message was caused by the fact that the setuptools implementation is not mine.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • As for setuptools; presumably it has it's *own* site.py module within the setuptools package.. – Martijn Pieters Jul 10 '12 at 10:13
  • @Martijn It does, that was the one I copied there and which recurses infinitely. Unfortunately, I cannot even install my own setuptools since that yields the same (original) error. – Konrad Rudolph Jul 10 '12 at 10:15
  • 1
    Note: You do not need a `$PYTHONPATH` env var *usually*. Your own python will find it's own path, but if there are other Python installations listed in `$PYTHONPATH` things tend to get ugly fast. – Martijn Pieters Jul 10 '12 at 10:19
  • @Martijn Yes I know, unfortunately I need it to manage modules that are unrelated to my installation. But I hope to get rid of it one of these days. – Konrad Rudolph Jul 10 '12 at 11:40

1 Answers1

2

The site module sets up your python module search path and some other things. It is somewhat crucial to the normal operation of python.

You can download a new copy from the python source repository:

For other python versions, generally the URL is http://hg.python.org/cpython/file/*major*.*minor*/Lib/site.py for the correct tagged version, then select the raw link in the left-hand menu.

If you installed python from a linux distribution package on Ubuntu or Debian, then this file has been customized and you'll need to re-install the appropriate python-minimal package.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343