ubuntu升级python_ubuntu升级python版本

本文介绍了解决Python中出现的AttributeError:'module'objecthasnoattribute'OrderedDict'错误的方法,包括更新Python版本到2.7及以后,安装pythonbrew来管理多个Python版本,以及如何使用pythonbrew安装和切换Python版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运行发现错误:

AttributeError: 'module' object has no attribute

'OrderedDict'

google发现是因为python版本老了的原因(python 2.7之后带有OrderedDict)

# python -V

Python 2.6.6

# curl -kL https://raw.github.com/utahta/pythonbrew/master/pythonbrew-install | bash

完成之后 会提示:

Please add the following line to the end of your ~/.bashrc::

[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc

#gedit ~/.bashrc

在最后一行添加上述语句:

[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc

重新打开Terminal

# pythonbrew install 2.7.2

# pythonbrew switch 2.7.2

Switched to Python-2.7.2

# python -V

Python 2.7.2

参见:

安装¶

建议使用如下语句下载安装pythonbrew:

$ curl -kL http://xrl.us/pythonbrewinstall | bash

pythonbrew就会安装到 ~/.pythonbrew 。

然后在您的 ~/.bashhrc 文件追加下面这一行:

$ [[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc

设置PYTHONBREW_ROOT环境变量可以pythonbrew安装到指定目录:

$ export PYTHONBREW_ROOT=/path/to/pythonbrew

$ curl -kLO http://xrl.us/pythonbrewinstall

$ chmod +x pythonbrewinstall

$ ./pythonbrewinstall

系统层(多用户环境)安装¶

在root用户环境下运行安装脚本,将自动将pythonbrew安装到 `` /usr/local/pythonbrew``

,并为系统下的每个用户进行配置。

安装完成后,非root用户在使用pythonbrew就不必调用sudo,而是直接运行

sudosudopybrew 即可:

$ sudopybrew install -n -v -j2 2.7.2

用法¶

一般用法是:

pythonbrew command [options]

安装不同版本的python:

pythonbrew install 2.7.2

pythonbrew install --verbose 2.7.2

pythonbrew install --test 2.7.2

pythonbrew install --test --force 2.7.2

pythonbrew install --configure="CC=gcc_4.1" 2.7.2

pythonbrew install --no-setuptools 2.7.2

pythonbrew install http://www.python.org/ftp/python/2.7/Python-2.7.2.tgz

pythonbrew install /path/to/Python-2.7.2.tgz

pythonbrew install /path/to/Python-2.7.2

pythonbrew install 2.7.2 3.2

永久性地使用某个特定版本的python(即每次登录shell都使用某个版本的python):

pythonbrew switch 2.7.2

pythonbrew switch 3.2

临时性地切换使用某个特定版本的python(即在当前shell中使用某个版本的python):

pythonbrew use 2.7.2

可以使用系统内所有版本/某个指定版本的python运行某个python文件:

pythonbrew py test.py

pythonbrew py -v test.py # 显示详细的输出结果

pythonbrew py -p 2.7.2 -p 3.2 test.py # 使用指定版本的python

列出系统内所有已安装的各个版本的python:

pythonbrew list

列出pythonbrew可以安装哪些版本的python:

pythonbrew list -k

删除某个特定版本的python:

pythonbrew uninstall 2.7.2

pythonbrew uninstall 2.7.2 3.2

清理陈旧的源码目录和档案包:

pythonbrew cleanup

升级到pythonbrew到最新版本:

pythonbrew update

pythonbrew update --master

pythonbrew update --develop

禁用pythonbrew(即切换回原始环境):

pythonbrew off

创建或移除指向某个python版本的符号链接(在您的$PATH中的某个目录):

pythonbrew symlink # 为已安装的各个版本的python都创建一个符号链接,形如"py2.7.2"。

pythonbrew symlink -p 2.7.2

pythonbrew symlink pip #创建指向bin目录下某个指定脚本的符号链接。

pythonbrew symlink -r # 移除符号链接。

pythonbrew symlink -v foo # 创建指向bin目录下某个指定的隔离环境的符号链接。

在当前或是某个特定版本的python中运行 buildout

pythonbrew buildout

pythonbrew buildout -p 2.6.6

创建python隔离环境(借助virtualenv)

pythonbrew venv init

pythonbrew venv create proj

pythonbrew venv list

pythonbrew venv use proj

pythonbrew venv delete proj

pythonbrew venv rename proj proj2

查看版本

pythonbrew version

命令¶

install

构建并安装某个给定版本的python,并自动安装setuptools和pip。

switch

永久切换到某个特定版本的python做为默认版本。

use

在当前shell下使用某个特定版本的python。

py

使用所有版本/某个特定版本运行一个python文件。

list

列出所有已安装的python版本。

list -k

列出所有可安装的python版本。

uninstall

删除某个特定版本的python。

cleanup

移除陈旧的源码目录和档案包。

update

升级pythonbrew到最新版本。

off

禁用pythonbrew。

symlink

创建或移除指向某个python版本的符号链接(在您的$PATH中的某个目录)。

buildout

在当前或是某个特定版本的python中运行 buildout 。

venv

创建python隔离环境(借助virtualenv)。

version

查看版本。

查看更多细节,可以运行:

$ pythonbrew help

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值