Python 2.6 'import site' failed error with

2019-09-14 19:44发布

问题:

I have Python 2.7 operating correctly on Windows. I installed 2.6 in another folder. However, when I run 2.6 in IDLE or PowerShell, it does not recognize basic commands and cannot import installed libraries. Whenever I launch 2.6, the first thing that comes up is:

'import site' failed; use -v for traceback

I can't find anything on this error aside from some mentions with specific libraries. Trying again using -v suggests 2.6 is pulling libraries from 2.7 which is causing some of the errors.

It then launches, but doesn't recognize installed libraries. For example:

import numpy

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy

Similarly, simple commands like quit() or help result in:

Traceback (most recent call last): File "", line 1, in NameError: name 'help' is not defined

I have tried reinstalling 2.6 and double-checked the registries are pointing to the right places. Virtualenv can't seem to run it either, so I'd like a solution that doesn't involve it...for now.

EDIT: Running 2.6 with -v starts off like this:

PS C:\python26> python26 -v

installing zipimport hook  
import zipimport # builtin  
installed zipimport hook  
C:\Python27\Lib\site.pyc matches >C:\Python27\Lib\site.py  
import site # precompiled from >C:\Python27\Lib\site.pyc  

For some reason, zipimport is pulling from 2.7, not 2.6. I found a text_zipimport.py file, but it's beyond me.

回答1:

Took a while, but I did solve this. When I installed 2.6, it did not add anything to the environment path. I'm working in windows, so here's what I did:

  1. Start -> search for "environment"
  2. Select "Edit environment variables for your account"
  3. Click the variable Path and click Edit
  4. Add "C:\Python26;C:\Python26\Scripts;" to the path
  5. Ok

After that it loads properly.