简体   繁体   English

如何使用 cloud-init 和 CDK 在 Amazon Linux 2 上安装 python 3.9

[英]How to install python 3.9 on Amazon Linux 2 with cloud-init and CDK

I'm trying to install Python 3.9 an EC2 instance that uses Amazon Linux 2. I tried following this guide: https://computingforgeeks.com/install-latest-python-on-centos-linux/ , and I was able to install Python3.9 manually on the EC2 instance by SSH'ing in and running the commands. I'm trying to install Python 3.9 an EC2 instance that uses Amazon Linux 2. I tried following this guide: https://computingforgeeks.com/install-latest-python-on-centos-linux/ , and I was able to install通过 SSH 进入并运行命令,在 EC2 实例上手动运行 Python3.9。 I'm now trying to setup the EC2 instance with a UserData script that calls some CloudFormationInit scripts to install dependencies, including Python 3.9, and my script is failing.我现在正在尝试使用 UserData 脚本设置 EC2 实例,该脚本调用一些 CloudFormationInit 脚本来安装依赖项,包括 Python 3.9,但我的脚本失败了。

Here's part of the script that I'm using to install Python 3.9:这是我用来安装 Python 3.9 的脚本的一部分:

    const installPythonString = `
#!/bin/bash
sudo amazon-linux-extras install -y epel
sudo yum -y update
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel -y
gcc --version
sudo yum install wget -y
sudo mkdir -p /opt/python3.9/
sudo chown -R $USER:$USER /opt/python3.9/
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz -P /opt/python3.9
cd /opt/python3.9/
tar xvf Python-3.9.9.tgz
whoami
sudo chown -R $USER:$USER Python-3.9.9
cd Python-3.9.9/
ls -al
pwd
./configure --enable-optimizations
sudo make altinstall
python3.9 --version
pip3.9 --version
`;
    init.addConfig('install_python39', new ec2.InitConfig([
      ec2.InitFile.fromString('/opt/install_python39.sh', installPythonString, {
        mode: '000755',
        owner: 'root',
        group: 'root',
      }),
      ec2.InitCommand.shellCommand('sudo sh install_python39.sh', {
        cwd: '/opt',
        key: 'install_python39',
      }),
]))

I'm getting the following errors when trying to start up the EC2 instance:尝试启动 EC2 实例时出现以下错误:

Python build finished successfully!
...
WARNING: The script pip3.9 is installed in '/usr/local/bin' which is not on PATH.
install_python39.sh: line 21: python3.9: command not found
install_python39.sh: line 22: pip3.9: command not found

Is there an easier way to install Python 3.9 on Amazon Linux 2 using CloudFormationInit?有没有更简单的方法可以使用 CloudFormationInit 在 Amazon Linux 2 上安装 Python 3.9?

Looks like the path to the python is /urs/local/bin which is not in $PATH so the python3.9 command is not found.看起来 python 的路径是/urs/local/bin ,它不在$PATH中,因此python3.9命令。

run the following commands in order.按顺序运行以下命令。

export PATH="/usr/local/bin:$PATH" or echo "export PATH='/usr/local/bin:$PATH' >> ~/.bashrc (if you do this relaunch the ssh session) to save it to bashrc so you don't have to run the export everytime you log in. export PATH="/usr/local/bin:$PATH"echo "export PATH='/usr/local/bin:$PATH' >> ~/.bashrc (如果这样做,请重新启动 ssh 会话)以保存它到 bashrc,这样您就不必每次登录时都运行导出。

python3.9 --version

additionally if you keep having issues, follow this to install python3.9, which is what i used, and everything went flawlessly.另外,如果您仍然遇到问题,请按照安装python3.9,这是我使用的,一切顺利。

if you have python packages that need to be installed, i would recommend creating a requirements.txt and using pip3.9 install -r requirements.txt to install them.如果您有需要安装的 python 软件包,我建议您创建一个 requirements.txt 并使用pip3.9 install -r requirements.txt来安装它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM