How do I install and use Python 3.8 on an Amazon EC2 Linux Instance?

Viewed 14232

So I followed a tutorial to install python 3.8 and I'm able to use it by doing the command python3.8, but for some reason the code in my main.py file is still running on python 3.7. If I enter python3.8 main.py, I get errors saying that I don't have modules installed. I don't know how to install my modules on python 3.8 because when I do sudo python3.8 -m pip install pymongo it says sudo: python3.8: command not found. Any help would be appreciated.

4 Answers

Amazon has it's own Linux with extras, the command is:

    sudo amazon-linux-extras install python3.8

taken from https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install-linux.html but after installing 3.7 from there, there is the above update command appearing during the install telling how to upgrade. Ahh the convolution of hosted virtual machines, still easier than trying to get a console command line login on any other Linux on AWS.

It does rebuild the python to 3.8, cleaning up files from 3.7, so boom a clean build of 3.8 thanks to Amazon.

All that being said, the default "python" will still be 2.7 as there is no backward compatibility, and many think getting rid of 2.7 will cause problems.

So what NOT TO DO NEXT as we are done is:

    sudo rm /usr/bin/python  (which is only a link to /usr/bin/python2.7)
    sudo ln -s /usr/bin/python3.8 /usr/bin/python

but usually peeps simply type python3.8 to specifically run the new version.

The tutorial link you have posted makes you install python interpreter in a /opt folder not the /usr/bin or anywhere standard binaries are installed.So a workaround would this be :-

Just add your python interpreter Path to your Environment PATH variable, so that your shell knows where to find python interpreter.

But before that go to /opt folder nd determine your interpreter's full path using pwd command and then execute the following.

type export PATH="$PATH:/opt/python3.8/python and press enter ( for example)

or you can also do this specify the python interpreter path on top of your script

example

#!/opt/python3.8/bin/python
import sys
print(sys.executable)

Now you can use a package available via yum instead of getting from Amazon Extras. This way works for Amazon Linux 2:

yum install -y python38 python38-devel
Related