windows 多个版本的 Python 导入站点错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6471388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:10:50  来源:igfitidea点击:

Python import-site error with multiple versions

pythonwindowswindows-7installerpython-2.6

提问by jmite

I'm using a shared machine. I'm trying to install Python 2.6 for a project that is using Pysco (unsupported on 2.7), and there is an existing install of 2.7 on the machine that I cannot remove without breaking other people's projects.

我正在使用共享机器。我正在尝试为使用 Pysco 的项目安装 Python 2.6(在 2.7 上不受支持),并且机器上已有 2.7 的现有安装,我无法在不破坏其他人的项目的情况下将其删除。

I'm using 32-bit binaries of Python on Windows 7.

我在 Windows 7 上使用 Python 的 32 位二进制文​​件。

When I try to start Python 2.6, I get this error:

当我尝试启动 Python 2.6 时,出现此错误:

'import-site' failed; use -v for traceback

It looks like Python 2.6 is trying to import modules from Python 2.7, which is causing the failure (see output below). Does anybody know what might be causing this, and how it could be fixed? Thanks!

看起来 Python 2.6 正在尝试从 Python 2.7 导入模块,这导致了失败(请参见下面的输出)。有谁知道可能导致这种情况的原因,以及如何解决?谢谢!

Running with the -v option gives the following output

使用 -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
# C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py
import os # precompiled from C:\Python27\Lib\os.pyc
import errno # builtin
import nt # builtin
# C:\Python27\Lib\ntpath.pyc matches C:\Python27\Lib\ntpath.py
import ntpath # precompiled from C:\Python27\Lib\ntpath.pyc
# C:\Python27\Lib\stat.pyc matches C:\Python27\Lib\stat.py
import stat # precompiled from C:\Python27\Lib\stat.pyc
# C:\Python27\Lib\genericpath.pyc matches C:\Python27\Lib\genericpath.py
import genericpath # precompiled from C:\Python27\Lib\genericpath.pyc
# C:\Python27\Lib\warnings.pyc matches C:\Python27\Lib\warnings.py
import warnings # precompiled from C:\Python27\Lib\warnings.pyc
# C:\Python27\Lib\linecache.pyc matches C:\Python27\Lib\linecache.py
import linecache # precompiled from C:\Python27\Lib\linecache.pyc
# C:\Python27\Lib\types.pyc matches C:\Python27\Lib\types.py
import types # precompiled from C:\Python27\Lib\types.pyc
# C:\Python27\Lib\UserDict.pyc matches C:\Python27\Lib\UserDict.py
import UserDict # precompiled from C:\Python27\Lib\UserDict.pyc
# C:\Python27\Lib\_abcoll.pyc matches C:\Python27\Lib\_abcoll.py
import _abcoll # precompiled from C:\Python27\Lib\_abcoll.pyc
# C:\Python27\Lib\abc.pyc matches C:\Python27\Lib\abc.py
import abc # precompiled from C:\Python27\Lib\abc.pyc
# C:\Python27\Lib\_weakrefset.pyc matches C:\Python27\Lib\_weakrefset.py
import _weakrefset # precompiled from C:\Python27\Lib\_weakrefset.pyc
import _weakref # builtin
'import site' failed; traceback:
Traceback (most recent call last):
  File "C:\Python27\Lib\site.py", line 62, in <module>
    import os
  File "C:\Python27\Lib\os.py", line 398, in <module>
    import UserDict
  File "C:\Python27\Lib\UserDict.py", line 84, in <module>
    _abcoll.MutableMapping.register(IterableUserDict)
  File "C:\Python27\Lib\abc.py", line 109, in register
    if issubclass(subclass, cls):
  File "C:\Python27\Lib\abc.py", line 151, in __subclasscheck__
    if subclass in cls._abc_cache:
  File "C:\Python27\Lib\_weakrefset.py", line 69, in __contains__
    return ref(item) in self.data
TypeError: cannot create weak reference to 'classobj' object

回答by Jarek Potiuk

Try to use virtualenv. It is used to separate many instances of python and it's libraries - you can have as many virtual environments as possible: python 2.5 , 2.6 , 2.7, whatever - with any combinations of libraries - so you can have for example five python 2.6 instances with different sets of libraries configured.

尝试使用 virtualenv。它用于分离 Python 的许多实例及其库 - 您可以拥有尽可能多的虚拟环境:python 2.5 、 2.6 、 2.7 等等 - 具有库的任意组合 - 因此您可以拥有例如五个不同的 python 2.6 实例配置的库集。

Start from http://pypi.python.org/pypi/virtualenv

http://pypi.python.org/pypi/virtualenv开始

回答by mightyoscar

I know this is an old post, but I recently solved the same problem. It's simply a matter of updating the environment path to include the new version.
Python 2.6 'import site' failed error with 2.7 installed

我知道这是一个旧帖子,但我最近解决了同样的问题。只需更新环境路径以包含新版本即可。
安装了 2.7 的 Python 2.6“导入站点”失败错误