如何安装Python 3.3?

我已经从官方网站下载了Python 3.3,但不知道如何安装。
我正在使用Ubuntu 12.04。

为什么我不能在不升级整个操作系统的情况下更新应用程序?这篇文章解释了为什么不可行。简而言之:其他软件包依赖于一个较旧(但仍在维护!)的版本。请将您对该网站运作方式的个人想法保留或者在元社区上发表,只要您获得足够的声望积分即可发帖。但首先,请查看常见问题以了解该网站的运作方式。 - gertvdijk
8个回答

Python 3.3在2012年9月29日发布,比Ubuntu 12.04发布几个月后。尽管如此,它被包含在Ubuntu 12.10中,作为python3.3软件包。
如果你想在没有Python 3.3的Ubuntu版本上安装它,你有以下几个选择:
使用PPA
有一个由Felix Krull维护的PPA,其中包含旧版和新版Python版本。请参考Luper Rouch的回答获取安装说明。
从源代码编译Python
这非常简单,可以让你拥有多个Python版本,而不会干扰系统Python解释器(很多Ubuntu自带程序使用该解释器)。在我的开发机上,我有从2.4到3.2的几十个不同的Python版本,它们都在/opt目录下和谐共存。
我们需要C编译器和其他工具来编译Python。
sudo apt-get install build-essential

为了让Python具备SQLite支持,需要安装SQLite库。

sudo apt-get install libsqlite3-dev
sudo apt-get install sqlite3 # for the command-line client
sudo apt-get install bzip2 libbz2-dev

下载并编译Python:
wget http://www.python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
tar xJf ./Python-3.3.5.tar.xz
cd ./Python-3.3.5
./configure --prefix=/opt/python3.3
make && sudo make install

一些很好的操作可以通过创建符号链接来安装一个 py 命令。
mkdir ~/bin
ln -s /opt/python3.3/bin/python3.3 ~/bin/py

或者,您可以安装一个名为py的bash别名代替:
echo 'alias py="/opt/python3.3/bin/python3.3"' >> .bashrc

这就是它。现在你可以拥有任何版本的Python,甚至是alpha版,或者说,编译几个带有不同设置的Python 3.3副本...虽然并不是很多人需要那样做 :)
使用pyenv
有一个叫做pyenv的软件可能会帮助你自动化这个过程 - 它的基本功能是从源代码编译Python,并将其安装在你的主目录中。它的目标是帮助你管理多个Python版本。

1安装后,如何使用这个另类的Python安装呢?假设我有一些带有#!/usr/bin/env python的shebang行的.py文件(可执行权限已设置),我要怎样让它们在 /opt/python3.3 这个安装路径下运行,而不需要修改所有文件呢?甚至那些系统安装的文件也是如此。 - gertvdijk
1@gertvdijk:整个重点是不要替换默认的解释器-如果这样做,那么从您的帐户运行的每个Python应用程序都将使用Python 3.3,包括Ubuntu应用程序,如软件中心等。我们不希望那样。要运行脚本,只需使用py myscript.py(其中py是我们在练习结束时创建的符号链接)。我通常还会为我的项目使用virtualenv或buildout。 - Sergey
1@gertvdijk 您可以使用virtualenv来管理多个Python环境。 - flup
@gertvdijk 你知道Python 3.x和Python 2.x是不兼容的吗?如果你把所有现有的脚本指向Python 3.3,它们很可能会出错。只需在你的新Python脚本中加入 #! /opt/python3.3 ,当你运行时,正确的解释器将被调用。 - Tony Martin
@TonyMartin 是的,我完全知道这个,并只是假装不知道而已。我使用Python开发东西。:) 我的评论只是将这个也加入到回答中的一种方式。有些人可能期望它被替换掉。 - gertvdijk
@gertvdijk 那我会把评论留给同样的读者们。 :-) - Tony Martin
1创建一个名为~/bin的目录 使用符号链接将/opt/python3.3/bin/python链接到~/bin/py这个路径对我来说不起作用。我发现/opt/python3.3/bin/python应该是/opt/python3.3/bin/python3,但仍然显示py: command not found。有什么建议吗? - user37089
如果你之前没有~/bin,需要注销然后再登录,目录才会被添加到路径中。 - Sergey
在Ubuntu 13上,它是/opt/python3.3/bin/python3.3 - uvasal
这些说明也适用于在Ubuntu 10.04上的Python 2.7.5。 - Nick
@Nick:是的,只需要进行一些小的修改(更改路径中的版本号),它们应该适用于任何Python版本。 - Sergey
@Nick 基于Sergey的回答和我自己的经验,我在下面的答案中编写了一个脚本,可以自动化整个过程,并且对任何Python版本都应该很有用。 - user76204
在构建Python3.3之后,我们可以通过mkvirtualenv -p /opt/python3.3/bin/python3.3命令创建一个Python3.3的虚拟环境。 - Satoru.Logic
Pythobrew已被弃用,建议使用pyenv代替。详见此链接 - jgomo3
根据我的经验,从源代码下载Python并进行编译是最佳选择。 - airstrike
非常好的回答 (+1)。评论中已经提到了,但我想明确指出,如果用户还没有 ~/bin 目录的话,应该将其添加到 PATH 中(当然要确保它放在正确的位置)。 - Marc van Dongen
@MarcvanDongen:上次我检查的时候,Ubuntu会在登录时自动将~/bin添加到PATH中(如果存在)。所以你只需要注销然后重新登录就可以了,它就会被添加进去。我在几年前的评论中提到过这一点 :) - Sergey
@Sergey 谢谢。对于普通用户来说,它确实似乎是这样的。(我刚试过了。)就个人而言,我觉得这太令人惊讶了:这本不应该发生。请注意,我确实有一个 ${HOME}/bin 目录,但我认为操作系统不应该将这样的目录放在用户的路径中。 - Marc van Dongen
现代版本的Python已经内置了pip,所以如果你也想要它,你应该运行sudo apt-get install libssl-dev,否则Python将在没有pip的情况下编译。 - monitorius
我个人不会选择/opt/作为安装目录,因为它应该是用于自包含的第三方应用程序。应该使用/usr/local,而且这也是./configure的默认设置,那么为什么不保留默认设置呢? - PlasmaBinturong
适用于Debian吗? - user694353

这是我在Ubuntu 12.04上安装Python 3.3的步骤:

1. 安装依赖项: ```bash sudo apt-get build-dep python3.2 sudo apt-get install libreadline-dev libncurses5-dev libssl1.0.0 tk8.5-dev zlib1g-dev liblzma-dev ```
2. 下载Python 3.3.0: ```bash wget http://python.org/ftp/python/3.3.0/Python-3.3.0.tgz ```
3. 解压缩: ```bash tar xvfz Python-3.3.0.tgz ```
4. 配置和安装: ```bash cd Python-3.3.0 ./configure --prefix=/opt/python3.3 make sudo make install ```
5. 测试是否成功: ```bash /opt/python3.3/bin/python3 ```
你应该看到类似的东西:
Python 3.3.0 (default, Jan 31 2013, 18:37:42) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

一些额外的有用的东西...你可以在家里创建一个虚拟环境,只需按需激活Python 3.3。
  1. 在家中创建一个虚拟环境:

    /opt/python3.3/bin/pyvenv ~/py33
    
  2. 激活虚拟环境:

    source ~/py33/bin/activate
    
  3. 安装分发工具:

    wget http://python-distribute.org/distribute_setup.py
    python distribute_setup.py
    
  4. 安装pip:

    easy_install pip
    
  5. 安装任何你想要的Python包(例如bottle):

    pip install bottle
    

请享受!


sudo apt-get build-dep python3.2?你可能忘记了中间的install :) - Stam Kaly
1@StamKaly :不对。build-dep不是一个包,它是一个apt-get动词(就像install)。它的意思是“_安装所有构建所需的源代码包_”。 - MestreLion

deadsnakes PPA有适用于旧版和新版Python的软件包:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.3

是的,那确实安装了一些东西,但我该如何调用它呢?输入'python'会给我Python 2解释器,而输入'python3'则提示说它没有安装,可以从Ubuntu仓库中安装。 - kris
啊,我看到可执行文件被称为python3.3(或者pythonX.Y,根据安装的Python版本而定 :-) - kris
ppa:fkrull/deadsnakes 已经存档了。请使用 ppa:deadsnakes/ppa 代替。 - codefool

Ubuntu 14.04及更早版本:

Python2.7是默认安装的,在Ubuntu上使用软件包管理器在常规Python之上安装Python3,Ubuntu可以同时处理2.7和3.2版本而无需使用虚拟环境:

sudo apt-get install python3
python3 --version
Python 3.2.3
python --version
Python 2.2.3

Ubuntu 18.04:

Ubuntu 18.04预设安装了Python3,而Python2.7只有在特定安装时才可用。

可以选择三个软件包名称: pythonpython-minimalpython-all。默认为minimal(最精简版)。这些词只是向Ubuntu软件源发出的指令,决定是否包含额外的内容。要确切了解包含哪些子软件包和不包含哪些子软件包,请查看以下链接的子软件包:https://packages.ubuntu.com/bionic/python

sudo apt install python-minimal
python --version

或者尝试升级Python3:
sudo apt install python3-minimal
python --version

为了尝试强制使用特定版本,您可以尝试传递一个版本参数:

sudo apt-get install python 3.3.3

1如何将Python3版本从3.2.3更新到3.3.5? - fIwJlxSzApHEZIl
2Python 3.3只能从Ubuntu 12.10及更高版本的默认软件源中获取。OP正在使用12.04版本。 - Lenna
如果你想在一台电脑上拥有超过两个版本的Python(除了操作系统默认选择的2.7和3.2之外),那么每个新版本的Python应该位于自己的虚拟环境(virtualenv)中。谷歌搜索:“使用virtualenv隔离Python版本”。如果你不使用某种容器,那么你将面临一系列问题的迷宫,因为Python会在你的电脑上到处乱拉,占据每一个角落,并以约翰·克里斯式的方式相互争斗。 - Eric Leschinski


警告:Pythonbrew已被弃用,推荐使用pyenv。更新的说明请点击这里

你也可以使用类似于pythonbrew的工具:

curl -kL http://xrl.us/pythonbrewinstall | bash    
echo "[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc" >> ~/.bashrc    
pythonbrew install 3.3

使用起来非常简单,而且另一个好处是可以安装您所需的任何Python版本。请查看他们的文档以获取更多详细信息。

我已经编写了一个脚本,用于自动化下载、编译和安装非包管理器的Python版本。该脚本会将Python版本安装在/opt目录下,远离包管理器和系统版本的Python。
对于大多数Ubuntu版本,该脚本甚至可以获取依赖项。它应该适用于所有当前支持的Ubuntu版本(10.04、12.04、12.10和13.04),可能也适用于其他版本。
我在下面附上了脚本,并且还在我的Github存储库中发布了它,那里是主要位置。
脚本应该被复制并保存到文本编辑器中,例如build_python,并且需要设置为可执行(chmod u+x build_python),然后可以使用两个参数运行,其中第一个参数必须始终是Python分支,第二个参数必须始终是Python版本。
请参阅python.org以获取您想要编译的版本的列表。
以下是脚本使用的几个示例:
  1. 在稳定版本中,检查清单后,可以运行以下命令:

    ./build_python '3.3.2' '3.3.2'
    
  2. 在开发版本中,列表中的两个参数不同,可以运行以下命令:

    ./build_python '3.4.0' '3.4.0a1'
    
以下是脚本的主要内容(此处无语法高亮显示。如需查看,请访问我的Github页面):
#!/usr/bin/env bash

# by mik, aka Exactus29, https://github.com/Exactus29
# 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

##########

# a script to compile the latest stable version of Python and place in /opt

(( $# == 2 )) || { printf "Please provide a version branch (e.g. 3.4.0) and a version release (e.g. 3.4.0a1) in that order.\n"
                   printf "The official site is python.org, see the ftp server at: http://python.org/ftp/python.\n" >&2 ; exit 1; }

# a splew of variables, so that just the version number can be given on the cmd line
# and then then the script can do the rest, including verifying the packages using gpg

# need different branch and version as sometimes the two are different, particularly for dev releases
py_branch="$1"
py_version="$2"
shift 2

# check if install target already exists in /opt, and exit so user can decide what to do
if [[ -d /opt/python-${py_version} ]]; then 
    printf "Target directory for the build already exists, please rename or remove.\n" >&2
    exit 1
else 
    :
fi

# use tar.bz2 as that is what most of the older releases used, i.e. in case user tries to build an older release
py_url="http://python.org/ftp/python/${py_branch}/Python-${py_version}.tar.bz2"
py_asc="http://python.org/ftp/python/${py_branch}/Python-${py_version}.tar.bz2.asc"
py_dir="$HOME/src/python_build" # checked to exist later, etc

# first check if user requested file exists on server
wget --spider ${py_url} >/dev/null 2>&1
(( $? > 0 )) && printf "No such version, version ${py_version} does not exist\n" >&2 && exit 1


# now very important before we do anything else, to check if asc file exists, as it  doesn't for some downloads
# if we don't check and it doesn't exist it causes the script to exit

wget --spider ${py_asc} >/dev/null 2>&1
# set a flag re whether asc file exists, so can check later and avoid problems
(( $? > 0 )) && no_asc=1 || no_asc=0

# set up more variables
py_tarbz2="${py_url##*/}"
(( no_asc == 0 )) && py_tarbz2_asc="${py_asc##*/}" # only set this if there is an asc file
py_folder="${py_tarbz2%.*.*}"
py_gpg_key="" 

# check other build dependencies are installed, beyond build-dep, sqlite support, readline, ncurses, build-essential 
dependencies_check() {

    local installed=()
    local to_be_installed=()
    local dependencies_list=(build-essential wget libreadline-dev libncurses5-dev libssl1.0.0 tk8.5-dev zlib1g-dev liblzma-dev
libsqlite3-dev sqlite3 bzip2 libbz2-dev)    

    for package in "${dependencies_list[@]}"; do 
        if grep -iq '^ii' < <(dpkg -l "$package"); then
            installed+=("$package")
        else 
            to_be_installed+=("$package")
        fi
    done 2>/dev/null

    if (( ${#to_be_installed[@]} > 0 )); then
        printf "If you have recently elevated your privileges with sudo, you will not see a " 
        printf "prompt here, before the apt-get update and install of packages occurs.\n" 
        sleep 2
        sudo -p "We need to install some dependencies, please enter your password: " apt-get update && sudo apt-get -y install "${to_be_installed[@]}"
        return 0
    else 
        printf "\nNothing to install, proceeding.\n"
        return 0
    fi

}

# tailor build-dep to new python version we want to build, basically either 2x or 3x versions
# should work with at least lucid/precise/quantal/raring/saucy, the currently supported versions
if (( ${py_branch:0:1} == 3 )) && grep -iq 'precise' /etc/lsb-release 2>/dev/null; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python3.2 && dependencies_check
elif (( ${py_branch:0:1} == 3 )) && grep -Eiq '(raring|quantal|saucy)' /etc/lsb-release 2>/dev/null; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python3.3 && dependencies_check
elif [[ ${py_branch:0:3} == 2.7 ]] && grep -iq 'lucid' /etc/lsb-release 2>/dev/null; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python2.6 && dependencies_check
elif [[ ${py_branch:0:3} == 2.7 ]]; then
    sudo -p "Please provide your password to install dependencies: " apt-get build-dep python2.7 && dependencies_check
else
    printf "\nProceeding, but make sure you have the correct build deps installed.\n\n"
    sleep 2        
fi

# dir checks
if [[ -d $HOME/src ]]; then 
    cd $HOME/src || exit 1
else
    mkdir $HOME/src && cd $HOME/src
fi

if [[ -d ${py_dir} ]]; then
    mv "${py_dir}" "${py_dir}_old_$(date '+%F_%H_%M_%S')"
    mkdir "${py_dir##*/}" && cd "${py_dir##*/}"
else
    mkdir "${py_dir##*/}" && cd "${py_dir##*/}"
fi

# finally, download python 
printf "\nNow downloading version ${py_version} from branch ${py_branch} ....."
wget "${py_url}" -P "${py_dir}" >/dev/null 2>&1
(( $? == 0 )) && printf "Done.\n"
# only download asc if it exists, set flag earlier
(( no_asc == 0 )) && wget "${py_asc}" -P "${py_dir}" >/dev/null 2>&1

# gpg tests

gpg_test() {
    # if error returned, extract gpg key from error message
    py_gpg_key="$(gpg --verify "${py_tarbz2_asc}" "${py_tarbz2}" 2>&1 | awk '{ print $NF }' | grep -v found)"

    # now check with gpg_key (should be Python release signing key)
    printf "\nReceiving keys.. "
    gpg --recv-keys "${py_gpg_key}" >/dev/null 2>&1
    (( $? > 0)) && printf "Key could not be received\n" || printf "Done.\n"

    printf "\nVerifying download... "
    gpg --verify "${py_tarbz2_asc}" "${py_tarbz2}" >/dev/null 2>&1
    (( $? > 0 )) && printf "The download could not be verified.\n" || printf "Done.\n"

}

if (( no_asc == 0 )); then
    gpg --verify "${py_tarbz2_asc}" "${py_tarbz2}" >/dev/null 2>&1
    if (( $? > 0 )); then 
        gpg_test 
    else
        printf "\nDownload verified\n\n"
    fi
else
    printf "\nProceeding even though asc file is not available for gpg to verify download\n\n"
    sleep 1
fi

# unpack and cd to the python folder
printf "Unpacking archive...."
tar xvjf "${py_folder}.tar.bz2" >/dev/null 2>&1
(( $? == 0 )) && printf "Done.\n" || { printf "Problems occured when unpacking, exiting\n" >&2; exit 1; }
cd "${py_folder}" || exit 1

# tailor the build to your machine here with configure and make

printf "\nNow for the configure (default prefix is /opt/python-${py_version})...."
sleep 2
./configure --prefix=/opt/python-${py_version} >/dev/null 2>&1
# as configure and make will exit anyway on error, no need to add || alternatives to the tests below
(( $? == 0 )) && printf "Done.\n\n"  
sleep 1

printf "\nNow for the compile. (If necessary, please add your own specifications to the make command line and run the script again)\n"
printf "\nPlease wait for the compile to finish: it may take a while...."
make >/dev/null 2>&1
(( $? == 0 )) && printf "Done.\n\n"

printf "\nWe are installing with make install into /opt, instead of using checkinstall.\n"
sudo make install >/dev/null 2>&1
installcode=$?
(( $installcode == 0 )) && printf "\n${py_version} succesfully installed in /opt/python-${py_version}\n\n"

if [[ -d $HOME/bin ]]; then
    ln -s /opt/python-${py_version}/bin/python${py_version:0:3} ~/bin/py-${py_version}
    (( $? == 0 )) && printf "\nSymlink created, run py-${py_version} in the terminal to launch the interpreter\n"
else
    mkdir $HOME/bin && ln -s /opt/python-${py_version}/bin/python${py_version:0:3} ~/bin/py-${py_version}
    (( $? == 0 )) && printf "\nSymlink created, run py-${py_version} in the terminal to launch the interpreter\n"
    printf "\nHowever, you will not be able to call py-${py_version} until you have logged out and in again, as bin will not"
    printf " have been added to your path just as $HOME/bin is created.\nn"
fi

# important info re setting up pyvenv re distribute tools and pip etc
cat <<extra_info

            See also a program called pyvenv with your installation in /opt, 
            with which you can create a virtual environment and use tools
            such as pip, etc. See the official documentation at:
            http://docs.python.org/3.3/using/scripts.html#pyvenv-creating-virtual-environments

extra_info

sleep 2 
exit ${installcode}

这是我所遵循的步骤:
wget http://python.org/ftp/python/3.3.2/Python-3.3.2.tar.bz2
tar -xvjf ./Python-3.3.2.tar.bz2
cd ./Python-3.3.2
./configure --prefix=/opt/python3.3
make && make install
mkdir ~/bin
ln -s /opt/python3.3/bin/python ~/bin/py
echo 'alias py="/opt/python3.3/bin/python3"' >> .bashrc