Python 3.3을 어떻게 설치합니까?


103

공식 사이트에서 Python 3.3을 다운로드했지만 설치 방법을 모릅니다.

우분투 12.04를 사용하고 있습니다.


전체 OS를 업그레이드하지 않고 애플리케이션을 업데이트 할 수없는 이유는 무엇입니까? 왜 사용할 수 없는지 설명 합니다. 한마디로 : 다른 패키지는 이전 버전 (여전히 유지 관리 됨) 버전에 의존합니다. 그리고이 사이트가 어떻게 작동하는지에 대한 개인적인 생각을 유지하거나 충분한 평판을 얻은 후에 게시 할 수있는 메타에 게시하십시오 . 그러나 먼저 : 사이트 작동 방식에 대한 FAQ 입니다.
— gertvdijk

답변:


113

Python 3.3은 Ubuntu 12.04가 릴리스 된 후 몇 개월이 지난 2012 년 9 월 29 일 에 릴리스되었습니다. python3.3패키지 로 우분투 12.10에 포함되어 있습니다.

리포지토리에없는 Ubuntu 버전에 Python 3.3을 설치하려면 다음 옵션이 있습니다.

PPA 사용

Felix Krull이 관리 하는 Old 및 New Python 버전이 포함 된 PPA가 있습니다 . 설치 지침 은 Luper Rouch의 답변 을 참조하십시오 .

소스에서 파이썬 컴파일

이것은 매우 쉽고 시스템 파이썬 인터프리터 (많은 우분투 자체 프로그램에서 사용)를 망칠 필요없이 여러 파이썬 버전을 가질 수 있습니다. 내 dev 컴퓨터에는 문자 그대로 2.4에서 3.2까지의 수십 가지 Python 버전이 행복하게 살고 /opt있습니다.

파이썬을 컴파일하려면 C 컴파일러와 다른 것들이 필요합니다

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

파이썬 다운로드 및 컴파일 :

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

그리고 이것입니다. 이제 당신이 할 수 있는 다른 설정으로 컴파일 파이썬 3.3의 몇 사본을 가지고, 말하자면, 파이썬 버전, 심지어 알파 나 ... 아니 그 많은 사람들이 필요로하는 :)하지만

pyenv 사용

pyenv 라는 소프트웨어가있어 절차를 자동화하는 데 도움이 될 수 있습니다. 본질적으로하는 일은 소스에서 Python을 컴파일하여 홈 디렉토리에 설치하는 것입니다. 그 목표는 여러 Python 버전을 관리하는 데 도움이되는 것입니다.


1
설치 후이 대체 Python 설치를 어떻게 사용 합니까? shebang 라인 (실행 가능 비트 세트) 이있는 .py파일 이 있다고 가정 하면, 파일을 모두 수정하지 않고이 #!/usr/bin/env python설치를 사용하려면 어떻게 /opt/python3.3해야합니까? 또는 심지어 시스템에 설치된 것.
— gertvdijk

1
@gertvdijk : 요점은 기본 인터프리터를 대체하는 것이 아닙니다. 그렇게하면 계정에서 실행 된 모든 Python 앱은 Software Center 요법과 같은 Ubuntu 앱을 포함하여 Python 3.3을 사용합니다. 우리는 그것을 원하지 않습니다. 스크립트를 실행하려면 다음을 사용하십시오 py myscript.py( py연습이 끝나면 생성 된 심볼릭 링크). 또한 일반적으로 프로젝트에 virtualenv 또는 buildout을 사용합니다.
— Sergey

1
@gertvdijk virtualenv를 사용하여 여러 파이썬 환경을 관리 가능하게 유지할 수 있습니다.
— flup

@gertvdijk python 3.x와 python 2.x가 호환되지 않는다는 것을 알고 있습니까? 파이썬 3.3에서 모든 기존 스크립트를 가리 키면 아마도 깨질 것입니다. 새 파이썬 스크립트를 #으로 간단히 요약하십시오! /opt/python3.3 및 올바른 인터프리터를 실행할 때 해결됩니다.
— Tony Martin

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. 원하는 파이썬 패키지를 설치하십시오 (예 : 병)

    pip install bottle

즐겨!


sudo apt-get build-dep python3.2? 아마 install:)
— Stam Kaly

1
@StamKaly : 아뇨. build-dep패키지가 아니며 apt-get 동사 와 같은 동사install 입니다. " 요청한 소스 패키지를 빌드하는 데 필요한 모든 패키지 설치 "
— MestreLion

35

deadsnakes PPA는 이전 및 새 파이썬 버전의 패키지가 있습니다 :

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'을 입력하면 설치되지 않았으며 우분투 저장소에서 설치할 수 있습니다.
— kris

아 .. 나는 실행 파일이 python3.3 (또는 pythonX.Y, 어떤 버전의 python이 설치되어 있는지 :-)이라고 본다.
— kris

ppa : fkrull / deadsnakes가 보관됩니다. 대신 ppa : deadsnakes / ppa를 사용하십시오.
— codefool

10

우분투 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

우분투 18.04 :

Python3은 OS와 함께 기본으로 제공되며 특별히 설치하지 않으면 Python2.7을 사용할 수 없습니다.

세 가지 패키지 이름에서 선택 : python, python-minimal, python-all. 기본값은 최소입니다. 이 단어는 우분투 리포지토리에 추가 항목을 포함하거나하지 않도록 플래그입니다. 하위 패키지가 포함되어 있고 포함되어 있지 않은 것을 정확히 보려면 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

2
Python 3.3은 Ubuntu 12.10 이상의 기본 리포지토리에서만 사용할 수 있습니다. OP는 12.04를 사용하고 있습니다
— Lenna

한 컴퓨터에서 두 가지 이상의 버전의 파이썬을 사용할 수있게하려면 (OS가 선택한 기본 2.7 및 3.2 이외) 각각의 새 버전의 파이썬은 자체 가상 환경에 있어야합니다 ( virtualenv). Google 검색 : "virtualv를 사용하여 Python 버전 분리" 어떤 종류의 컨테이너를 사용하지 않으면 파이썬은 컴퓨터 전체에서 거대한 똥을 가져 와서 구석 구석을 차지하고 John Cleeseian 방식으로 서로 싸우기 때문에 미로 문제에 노출됩니다.
— Eric Leschinski


1

패키지가 아닌 Python 버전의 모든 다운로드, 컴파일 및 설치를 자동화하는 스크립트를 작성했습니다. 이 스크립트는 /opt패키지 관리자 및 시스템 버전의 Python과 는 별도로 Python 버전을 설치합니다 .

심지어 대부분의 우분투 버전에 대한 종속성도 가져옵니다. 현재 지원되는 모든 Ubuntu 버전 (10.04, 12.04, 12.10 및 13.04) 및 다른 버전에서 작동해야합니다.

아래에 포함 시키고 마스터 위치 인 Github 저장소 에도 게시했습니다 .

스크립트는 예를 들어 텍스트 편집기에 복사하여 텍스트 편집기에 저장 한 후 build_python실행 파일 ( chmod u+x build_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

사용하기 쉽고 또 다른 장점은 필요한 파이썬 버전을 설치할 수 있다는 것입니다. 모드에 대한 자세한 내용 은 해당 문서 를 참조 하십시오


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
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.