尝试使用pip将Python 2.7.0升级到Python 2.7.3:SyntaxError;在python2和python3之间困惑?

3 投票
2 回答
3644 浏览
提问于 2025-04-17 13:40

我在一个虚拟环境中安装了Python 2.7.0,路径是/local/gerrit/python2.7。现在我想把它升级到Python 2.7.3。我试着用pip来升级,但似乎它对python2和python3搞混了:

$ pip install --upgrade 'python>=2.7,<2.7.99'
Downloading/unpacking python>=2.7,<2.7.99 from http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
  Running setup.py egg_info for package python
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/local/gerrit/python2.7/build/python/setup.py", line 1804
        exec(f.read(), globals(), fficonfig)
    SyntaxError: unqualified exec is not allowed in function 'configure_ctypes' it contains a nested function with free variables
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/local/gerrit/python2.7/build/python/setup.py", line 1804

    exec(f.read(), globals(), fficonfig)

SyntaxError: unqualified exec is not allowed in function 'configure_ctypes' it contains a nested function with free variables

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /local/gerrit/python2.7/build/python
Storing complete log in /storage4/home/gerrit/.pip/pip.log

看起来/local/gerrit/python2.7/build/python/setup.py这个文件实际上是用python3的语法写的,可能出现了什么问题。我的判断对吗?我该怎么处理这个问题,才能不重新安装Python和它的所有库呢?

2 个回答

0

另一个选择是 pyenv,这是一个可以替代pythonz的工具。它的好处是,不需要依赖于Python本身。

1

你可能需要使用一些工具,比如 pythonz,这个工具可以帮助你在同一台电脑上管理多个Python版本。

当你使用像virtualenv这样的工具时,你需要指定你安装的那个Python版本的路径,这样就可以创建一个使用特定Python版本的虚拟环境了。

virtualenv -p /path/to/python

撰写回答