【问题标题】:Installing Python3.7 from source从源码安装 Python3.7
【发布时间】:2021-02-26 05:10:25
【问题描述】:

我需要使用Python3.7,所以我按照这些说明安装了它

curPath=${pwd}
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
sudo tar xzf Python-3.7.9.tgz
cd Python-3.7.9
sudo ./configure --enable-optimizations
cd ${pwd}

然后我尝试运行 python -V 并得到了

Command 'python3.7' not found, did you mean:

  command 'python2.7' from deb python2.7 (2.7.18-1~20.04)
  command 'python3.9' from deb python3.9 (3.9.0-5~20.04)
  command 'python3.8' from deb python3.8 (3.8.5-1~20.04.2)

Try: sudo apt install <deb name>

我也尝试运行whereis python 并得到了

whereis python
python: /usr/bin/python3.8 /usr/bin/python3.8-config /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.9 /etc/python3.8 /usr/local/lib/python3.8 /usr/include/python3.8

【问题讨论】:

  • 如果它适用于 18.04,为什么要使用 20.04。你也可以在 AWS 上使用 18.04 吗?
  • @Marcin 实际上我认为它不能在 Azure 上运行,Azure Ubuntu 刚刚安装了 python3.7,当我在 Azure 上尝试 whereis python3.7 时,/usr/src 里面的那个没有'不出现
  • 您运行了配置。你运行 make 了吗?
  • @snapshoe 是对的。另外,“configure”和“make”不需要“sudo”。只有最终的“make install”可以。
  • 嗨。这个问题是怎么解决的?

标签: python amazon-web-services ubuntu


【解决方案1】:

我调整了您的脚本,使其在 Ubuntu 20.04 实例上用作 UserData 脚本:

#!/bin/bash

apt update
apt install -y build-essential zlib1g-dev

curPath=${pwd}
cd /usr/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar xzf Python-3.7.9.tgz
cd Python-3.7.9
#./configure --enable-optimizations
./configure 
make
make install
cd ${pwd}

【讨论】:

    【解决方案2】:

    有了这个脚本,我可以在 ubuntu 18 上安装 python3.7

    curPath=${pwd}
    sudo apt-get update
    sudo apt-get install gcc
    sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev wget liblzma-dev lzma
    cd /usr/src
    sudo wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
    sudo tar xzf Python-3.7.9.tgz
    cd Python-3.7.9
    sudo ./configure --enable-optimizations
    sudo make
    sudo make altinstall
    cd ${pwd}
    

    但你可能应该只用pyenv 安装它,这里有一个脚本会在linux 上安装pyenv,然后安装python 3.7.9。这种方法发生的速度快很多,第一次安装大约需要一个小时。

    sudo apt-get install gcc build-essential libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev liblzma-dev lzma
    
    curl https://pyenv.run | bash
    
    bashrc="${HOME}/.bashrc"
    bash_profile="${HOME}/.bash_profile"
    profile="${HOME}/.profile"
    
    checkAndAppend () {
        # Checks if some text is in a file, and if it isn't, it appends that text at the end of the file
        # $1 - The text to insert into a file
        # $2 - A file to insert text into
        if ! grep -q $2 <<< "${1}" ; then 
            echo $1 >> $2
        fi 2>/dev/null
    }
    
    checkAndAppend 'export PYENV_ROOT="$HOME/.pyenv"' $profile
    checkAndAppend 'export PATH="$PYENV_ROOT/bin:$PATH"' $profile
    checkAndAppend 'eval "$(pyenv init --path)"' $profile
    checkAndAppend 'eval "$(pyenv init -)"' $bashrc
    checkAndAppend 'eval "$(pyenv virtualenv-init -)"' $bashrc
    checkAndAppend 'source $HOME/.profile' $bash_profile
    
    RED='\033[0;31m'
    printf $RED'
    If your $HOME/.profile sources $HOME/.bashrc, then these lines must be inserted into $HOME/.profile before that
    
        export PYENV_ROOT="$HOME/.pyenv"
        export PATH="$PYENV_ROOT/bin:$PATH"
        eval "$(pyenv init --path)"
    
    '
    
    source $profile
    source $bashrc
    source $bash_profile
    
    pyenv install 3.7.9
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-22
      • 2015-06-29
      • 2016-11-02
      • 2013-06-04
      • 2023-04-09
      • 1970-01-01
      • 2019-01-14
      相关资源
      最近更新 更多