No module named 'distutils.util' - python-3.x

I'm working on ubuntu 16.0.4 LTS and While creating a virtual environment in python 3.8 I'm getting No module named 'distutils.util'
I'm using the repository: sudo add-apt-repository ppa:deadsnakes/ppa
I have tried the following but didn't work
sudo apt install python3-distutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python-distutils
I have tried the solution from this link
sudo apt install python3.8-distutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python3.8-distutils
E: Couldn't find any package by glob 'python3.8-distutils'
E: Couldn't find any package by regex 'python3.8-distutils'

Make sure to replace 3.10 which is version of python with appropriate version.
Installing Python3.10
Update system
$ sudo apt-get update -y && sudo apt-get full-upgrade -y
Helps to add ppa repositories
$ sudo apt install software-properties-common -y
Adding deadsnake repository
$ sudo apt-add-repository ppa:deadsnakes/ppa
Installing python
$ sudo apt-get install python3.10
$ sudo apt-get install python3.10-dev
$ sudo apt-get install python3.10-tk
Add Python 3.8 & Python 3.10 to update-alternatives
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
Update Python 3 for point to Python 3.10
$ sudo update-alternatives --config python3
Installing distutils.util
$ sudo apt-get install python3.10-distutils
Still using python3.10 -m pip some_command might result in error to fix it use
$ curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10

Related

Cannot connect to EC2 ubuntu 18.04 instance after upgrading to Python3.9

I am using EC2 Ubuntu 18.04 VM.
Due to CVE-2021-3177, Python needs to be upgraded to the latest version of Python3.9 which would be 3.9.9 currently.
I did that using the deadsnakes option as per the steps mentioned below:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install python3.9
sudo apt-get update
sudo apt upgrade -y
The above ensures that Python3.9.9 is now available. But now python3.6 & python3.9 is available. So next we will use the update-alternatives command to make python3.9 as the default version.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
Now that alternatives are defined, we will switch to Option 2 as the default option i.e. Python3.9
sudo update-alternatives --config python3
Once done, the following command would point to the latest version.
sudo python3 -V
However, if you use the sudo apt update command, you will see an error stating that
Traceback (most recent call last):
File "/usr/lib/cnf-update-db", line 8, in <module>
from CommandNotFound.db.creator import DbCreator
File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
To fix this we will have to add a link using the following command
cd /usr/lib/python3/dist-packages/
sudo ln -s apt-pkg.cpython-{36m,39m}-x86_64-linux-gnu.so
Also below is optional, I tried with and without the following commands
apt purge python3-apt
apt install python3-apt
sudo apt install python3.9-distutils python3.9-dev
Once done following command will now not result in any errors
sudo apt update
This means that the issue is fixed.
But for some reason, I cannot connect with the machine afterwards or if I create an AMI using this I cannot connect to the launched instance using PUTTY or SCP.
The same issue persists with Ubuntu-20.x too.
Appreciate your help.
After upgrading Python, there are issues with the following Python modules that cloud-init depends on, which in turn prevents EC2 from being able to correctly configure your newly booted EC2 instance using cloud-init, and which is why it is inaccessible:
setuptools
urllib3
requests
jinja2
netifaces
You can debug this issue by going to your EC2 instance in the AWS Web Console and clicking:
Actions -> Monitor and troubleshoot -> Get system log
Sometimes it takes a while to update, so click the refresh button until your logs appear. It is easier to read the logs if you download them. This is what helped me solve the issues that I was having.
The following steps resolved the issue for me on Ubuntu 18.04 LTS:
For Ubuntu 20.04 LTS, change the 36m in the symbolic links to 38.
# Add deadsnakes ppa repository
sudo add-apt-repository ppa:deadsnakes/ppa
# Install new python version
sudo apt update
sudo apt install python3.10
# Fix broken apt_inst after python upgrade
sudo ln -s /usr/lib/python3/dist-packages/apt_inst.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_inst.so
# Fix broken apt_pkg after python upgrade
sudo ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
# Make installed python version an alternative with a priority of 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
# Make upgraded python version an alternative with a priority of 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
# Reinstall python3-apt
sudo apt remove --purge python3-apt
sudo apt autoclean
sudo apt install python3-apt
# Install required packages
sudo apt install \
build-essential \
python3.10-distutils \
python3.10-venv \
libpython3.10-dev
# Install latest pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python3.10 get-pip.py
# Upgrade outdated python libraries that break cloud-init
sudo -i
pip3 install --upgrade setuptools
pip3 install --upgrade urllib3
pip3 install --upgrade requests
pip3 install --upgrade jinja2
pip3 install --upgrade netifaces
pip3 install --upgrade --ignore-installed pyyaml
exit
# Upgrade cloud-init to latest version
sudo apt install --only-upgrade cloud-init
If you use Ansible, it is also affected by the upgrade.
Ansible can be fixed as follows:
Edit /usr/lib/python3/dist-packages/apt/package.py and change the following line:
from collections import Mapping, Sequence
to:
from collections.abc import Mapping, Sequence
It would be useful if the deadsnakes repository could provide an update for python3-apt (eg. python3.10-apt) to solve this issue.
Reference:
https://cloudbytes.dev/snippets/upgrade-python-to-latest-version-on-ubuntu-linux

How to make an autoinstall command script for Debian

So I was working on a project that need some libraries . so I decided to made an .sh script to just install all at once but I don't know why it fails . I was searching about it , but just found how to create installer like .deb , etc
here are the commands lines that I use
install.sh
#!/bin/sh
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pip python3-dev
sudo apt-get install build-essential cmake git unzip pkg-config libopenblas-dev liblapack-dev
sudo apt-get install python-numpy python-scipy python-matplotlib python aml
sudo apt-get install libhdf5-serial-dev python-h5py
sudo apt-get install graphviz
sudo apt-get install python-opencv
sudo apt install python-sklearn
sudo apt install python3-sklearn
pip3 install matplotlib
pip3 install pydot-ng
pip3 install tensorflow
pip3 install keras
pip3 install scikit-learn
using
bash install.sh
and I got this , I think that I'm doing just a few things wrong , I think
E: The update command takes no arguments
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package
Reading package lists... Done
Building dependency tree
Reading state information... Done
............
Can someone help me please
Your shebang at the beginning of your script is for a boot script
You're using:
#!/bin/sh
When this script should call the bash environment with:
#!/bin/bash
That should solve your problem.
As sergio states these can be done in one liners like:
#!/bin/bash
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y python3-pip python3-dev build-essential cmake git unzip pkg-config libopenblas-dev liblapack-dev python-numpy python-scipy python-matplotlib python aml libhdf5-serial-dev python-h5py graphviz python-opencv python-sklearn python3-sklearn
sudo pip3 install matplotlib pydot-ng tensorflow keras scikit-learn
At the very least utilize an array for more efficient bash programming like this:
#!/bin/bash
sudo apt-get update && sudo apt-get upgrade -y
aptDepends=(
python3-pip
python3-dev
build-essential
cmake
git
unzip
pkg-config
libopenblas-dev
liblapack-dev
python-numpy
python-scipy
python-matplotlib
python
aml
libhdf5-serial-dev
python-h5py
graphviz
python-opencv
python-sklearn
python3-sklearn
)
pipDepends=(
matplotlib
pydot-ng
tensorflow
keras
scikit-learn
)
sudo apt-get install -y "${aptDepends[#]}" && sudo pip3 install -y "${pipDepends[#]}"

Pip is not linking to my upgraded Python version. Package python3-pip is not available, but is referred to by another package

Having issues linking pip to python 3.7. Both pip and pip3 been installed but linking to Python 2.7.
I have tried to reinstall Python3 and then pip no progress.
I get the following message:
Package python3-pip is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
$ python --version
Python 3.7.4
$ pip -V
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
pip 19.2.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
$ pip3 -V
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
pip 19.2.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
$ sudo apt-get install python3-pip
[sudo] password for TarikDev:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package python3-pip is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'python3-pip' has no installation candidate
First use this command
sudo apt-get update
Two packages will be downloaded, of size 16 MB. Then:
sudo apt-get install python2-pip
I tried this and it worked for me:
Step1: sudo apt-get update.
Step2: sudo apt-get install python3-pip
Then check for pip version by typing:
$ pip -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
go to your sources.list file with the following steps:
cd /etc/apt
sudo nano sources.list
Now edit this line in this file
deb http://http.kali.org/kali kali-rolling main non-free contrib
Now press CTRL+X key and then press y to save the buffer and then press enter.
Now try sudo apt-get update
Now type the command sudo apt-get install python3-pip
First, update the system using the update command
sudo apt-get update
Install pip package to the machine,
sudo apt-get install python2-pip python3-pip
If you are still facing challenges to install pip then try the following commands:
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py
pip -V

Setting python3 PYTHONHOME

I installed python3 and pip3 using:
RUN apt install -y build-essential libssl-dev libffi-dev python3-dev
RUN apt install -y python3-pip
For some reason site-packages are showing up like this:
root#5d6f29f1d4e9:/usr/local/lib/python2.7# ls -a
. .. dist-packages site-packages
root#5d6f29f1d4e9:/usr/local/lib/python3.6# ls -a
. .. dist-packages
My only guess is that I should set PYTHONHOME to point to 3.x instead of 2.x? e.g.:
ENV PYTHONHOME /usr/local/lib/python3.6
(note I am root user in a docker container so just need one python version and I won't need virtualenv / venv).
Nah I don't think this was it. I did this:
RUN apt install -y build-essential libssl-dev libffi-dev python3-dev
RUN apt install -y python3-pip
ENV PYTHONHOME /usr/local/lib/python3.6
RUN pip3 install awscli
and it then failed with some unnecessary error when installing awscli, so setting this env var doesn't seem like the right thing to do.

Cant install python3-pyqt5 on ubuntu 17

I`m trying to install python3-pyqt5, but still getting the same error. I tried reinstalling ubuntu, updating and upgrading my apt-get and still no clue whats wrong.
ubuntu#ubuntu:~$ sudo apt-get install python3-pyqt5
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python3-pyqt5
I found that I just needed to type:
sudo add-apt-repository universe
then
sudo apt-get update
And everything was working fine.
I had the same problem. After doing some reading, I was able to install pyqt5 using this workaround:
Install python3-setuptools:
$ sudo apt-get install python3-setuptools
Install pip:
$ sudo easy_install3 pip
Finally, install pyqt5 using pip:
$ pip3 install --user pyqt5

Resources