如何安装Python 3.3?


103

我已经从官方网站下载了Python 3.3,但不知道如何安装它。

我正在使用Ubuntu 12.04


为什么不升级整个操作系统就不能更新应用程序?解释为什么它不可用。简而言之:其他软件包都依赖较旧的版本(仍在维护!)。并且请对本网站的工作方式保持个人看法,或将其发布到meta上,一旦获得足够的声誉点就可以发布。但首先:关于网站如何运作的常见问题解答
gertvdijk

Answers:


113

Python 3.3已于Ubuntu 12.04发布数月后于 2012年9月29日发布。它作为python3.3软件包包含在Ubuntu 12.10中

如果要在未在其存储库中包含Python 3.3的Ubuntu版本上安装Python 3.3,则可以使用以下选项:

使用PPA

有一个PPA,其中包含Felix Krull维护的旧Python版本和新Python版本。有关安装说明,请参见Luper Rouch的答案

从源代码编译Python

这非常容易,它允许您拥有多个Python版本,而不会弄乱系统python解释器(很多Ubuntu自己的程序都使用它)。在我的开发机器上,我实际上有几十种不同的Python版本,从2.4到3.2都愉快地生活在/opt

我们需要C编译器和其他东西来编译Python

sudo apt-get install build-essential

需要安装SQLite库才能使Python具有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

或者,您可以安装名为的bash别名py

echo 'alias py="/opt/python3.3/bin/python3.3"' >> .bashrc

就是这样。现在,您可以使用任何 Python版本,甚至是alpha版本,或者说,可以使用不同的设置来编译Python 3.3的一些副本,虽然不是很多人需要它:)

使用pyenv

有一个叫做pyenv的软件可以帮助您自动执行该过程-本质上是从源代码编译Python,然后将其安装在主目录中。它的目标是帮助您管理多个Python版本。


1
安装后,将如何使用该替代Python安装?假设我有一些.py带有#!/usr/bin/env pythonshebang行的文件(可执行位已设置),我将如何使它们在/opt/python3.3不修改所有文件的情况下使用此安装?甚至系统安装的。
gertvdijk

1
@gertvdijk:重点不是替换默认的解释器-如果这样做,则从您的帐户运行的每个 python应用程序都将使用Python 3.3,包括Ubuntu应用程序,例如Software Center等。我们不想要那个。要运行脚本,只需使用py myscript.pypy我们在练习结束时创建的符号链接在哪里)。我通常也为我的项目使用virtualenv或buildout。
谢尔盖(Sergey)2013年

1
@gertvdijk您可以使用virtualenv使多个python环境保持可管理性。
flup

@gertvdijk您知道python 3.x和python 2.x不兼容吗?如果将所有现有脚本指向python 3.3,它们可能会损坏。只需将新的python脚本设置为#!/opt/python3.3和正确的解释器将在您运行时得到解决。
托尼·马丁

1
mkdir ~/bin ln -s /opt/python3.3/bin/python ~/bin/py对我不起作用。我发现/opt/python3.3/bin/python应该是/opt/python3.3/bin/python3,但仍然可以py: command not found。有什么建议么。

43

这是我在Ubuntu 12.04上安装Python 3.3的操作:

  1. 安装依赖项:

    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:

    wget http://python.org/ftp/python/3.3.0/Python-3.3.0.tgz
  3. 提取:

    tar xvfz Python-3.3.0.tgz
  4. 配置和安装:

    cd Python-3.3.0
    ./configure --prefix=/opt/python3.3
    make  
    sudo make install
  5. 测试是否有效:

    /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. 激活virtualenv:

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

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

    easy_install pip
  5. 安装任何您想要的python软件包(例如,瓶子)

    pip install bottle

请享用!


sudo apt-get build-dep python3.2?您可能install在这之间忘记了:)
Stam Kaly

1
@StamKaly:不。build-dep不是包,而是apt-get 动词(如install)。这意味着“ 安装构建所需的源软件包所需的所有软件包
MestreLion

35

deadsnakes PPA对新老Python版本的package:

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仓库中安装。
克里斯

啊..我看到可执行文件称为python3.3(或pythonX.Y,无论安装了什么版本的python :-)
kris

ppa:fkrull / deadsnakes已存档。请改用ppa:deadsnakes / ppa。
codefool

10

Ubuntu 14.04及更早版本:

Python2.7是默认的,使用软件包管理器在Ubuntu上的常规python之上安装python3,Ubuntu可以在没有virtualenv的情况下同时处理2.7和3.2:

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

Ubuntu 18.04:

操作系统默认提供Python3,除非专门安装,否则Python2.7不可用。

三个包名可供选择:pythonpython-minimalpython-all。默认值为最小。这些单词只是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?
anon58192932 2014年

2
Python 3.3仅可从Ubuntu 12.10及更高版本中的默认存储库中获得。OP正在使用12.04
Lenna

如果您希望在一台计算机上使用两个以上版本的python(操作系统为您选择的默认2.7和3.2除外),则每个新版本的python都应位于其自己的虚拟环境(virtualenv)中。Google搜索:“使用virtualenv隔离python版本”。如果您不使用某种容器,那么您将面临迷宫般的麻烦,因为python在您的计算机上占据了巨大的垃圾,占据了每个角落和缝隙,并且它们以John Cleeseian的方式相互斗争。
埃里克·莱斯钦斯基


1

我编写了一个脚本来自动化非软件包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}

1

警告:不赞成使用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版本。请参阅他们的文档以获取模式详细信息


0

这是我遵循的步骤:

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.