Repair Python3 pip installation on WSL - python-3.x

I accidentally deleted a directory ~/.local/lib/python3.5/site_packages/pip in my Ubuntu 16.04 installation on WSL.
After this I get errors when running pip3 like
File "/home/harper/.local/bin/pip", line 7, in <module>
from pip._internal.cli.main import main
File "/home/harper/.local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
Now I tried to repair this by uninstalling pip3 and python3 and following both packages with apt
sudo apt update
sudo apt upgrade
sudo apt uninstall python3-pip python3
sudo apt install python3 python3-pip
But the command pip3 --version still shows the error message above.
What is the best way to do a clean reinstall of Python3? Is there any cache, probably the ~/.local/lib/python3.5/site_packages/__pycache__ directory that can be cleared or erased?

You still have old pip script in your $PATH which apt cannot override. Remove /home/harper/.local/bin/pip and rehash $PATH with hash -r pip.

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

Command 'python' not found

I am trying to install python 3.8 on ubuntu 18.04.
python ––version
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
python ––version and it gives me "sudo apt install python3
sudo apt install python
sudo apt install python-minimal. You also have python3 installed, you can run 'python3' instead.
How do I install?
Instead of typing "python" in the terminal to start your environment, try with "python3". (Same goes for "pip" and "pip3"). Also, you could create an alias in your .bashrc file to access the Python3 command just by typing Python. If you would like, I can show you how to do it.
Go to your root directory in your terminal, and open the file called ".bashrc" with your favorite editor. To open in VSCode for example, just enter code .bashrc. At the end of the file, add a line that says alias python="python3". Assuming the python3 command worked, you will now be able to enter python just like you could python3!

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

I'm getting a "command not found" error when trying to install "xgboost" in an Ubuntu 16.04 Virtual Machine

I'm trying to install xgboost in an Ubuntu 16.04 virtual machine.
I'm following this guide and ran this command:
cmake ..
I got this error:
-bash: cmake: command not found
What am I doing wrong and how can I get rid of this error?
Try this:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
sudo apt-get install cmake
UPDATE:
Or you can just use pip:
sudo pip3 install xgboost
It appears you're getting this error because you don't have the cmake command installed, you can fix this by running:
sudo apt install cmake
I don't know if you've missed this but you can also install xgboost using pip (or in your case pip3), like this:
pip3 install xgboost
NOTE: depending on how your user/project is set up you might need to use the sudo command to grant pip3 write privileges to certain (root) directories, if this is the case, you'd use:
sudo pip3 install xgboost
NOTE: if you don't have pip3 installed, you can install it using:
sudo apt install python3 python3-pip
Good luck.

How do I get Pip to work with Python3 on Ubuntu 12.04

I'm running Ubuntu 12.04 and do not have the option of upgrading / using something else. It has Python3.2 on it. I have found out that pip doesn't come automatically with < 3.4. How can I get pip to work with Python3 on this machine?
I tried downloading a copy of get-pip.py and running it with python3.2, but I kept getting an error message about how < 3.4 support was dropped.
I spent a little bit getting this all to work so I wanted to make a detailed post.
First I found out I could get Python3.4 onto my Ubuntu 12.04 machine. To do this run:
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python3.4
Source: Is There An Easy Way To Install Python 34 On
Next, I removed Python3.2 (I only wanted to have a single Python3.x version on my machine) by running:
sudo apt-get remove python3.2
sudo apt-get remove python3.2-minimal
Then, to get pip I ran:
sudo curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
sudo python3.4 get-pip.py
Source: How To Install Pip On Ubuntu 12 04 LTS
At this point Python3.4 and pip can both be ran. Example:
python3.4 main.py
pip3 install requests
At this point I wanted to be able to just use python3 to run python3.4 - trying just python3 kept telling me I had to install python-minimal. Doing this reinstalled python3.2 for me which I did not want. Instead, I created an alias by doing the following:
vim ~/.bashrc # Open the file for editing
Add: alias python3=python3.4 to the file.
. ~/.bashrc # Make the changes apply to the current session
Source: How Do I Create A Permanent Bash Alias

Resources