pip3 SSL Error while installing new package - python

I am using Kali Linux, and I am trying to install some requirements of one packet for test purposes, but I keep getting the below error. I have tried many solutions but they are all not working and the error still persists
# pip3 install -r requirements.txt
or
pip3 install name_of_package-py

Generally, the only command you would need is:
pip install name_of_package
if it doesn't work you could try:
sudo pip install name_of_package
sudo should work, but you have to be careful regarding the package because with sudo you could end up installing a package with some malware in it. Therefore be certain of the reliability of the package.
also depending on the version you are using could be pip or pip3

Related

Correspondence between Python and pip versions

I have to install Tensorflow and the object detection API on a Jetson NANO.
I generally manage everything with virtual environments, but for a bunch of reasons this time I had to install everything in the base environment, and I assume I might've messed
something up.
The process starts, but it's getting stuck on solving some dependencies issues, and after running for the whole night it returns an error regarding the space left on the device (I assume it's downloading multiple versions of different packages and running out of space). So, I'm wondering if I'm installing them correctly.
I tried running the command:
python3 -m pip3 install --use-feature=2020-resolver .
but I'm getting the error:
/usr/bin/python3: No module named pip3
If I run simply
python3 -m pip install --use-feature=2020-resolver .
it goes, but as I was saying it gets stuck, so I was wondering if it is installing the files in the right locations.
I tried checking the outputs of:
which python3
which pip3
and I got
/usr/bin/python3
/usr/local/bin/pip3
So, the command pip3 does exist, and refers to /usr/local/bin/pip3...at this point, I don't understand why it returns the error when trying to use python3 -m pip3 install.
As a matter of fact, I used pip3 for all the previous steps in the object detection API installation, like:
sudo pip3 install -U pip testresources setuptools==49.6.0
sudo pip3 install -U numpy==1.16.1 future==0.18.2 mock==3.0.5 h5py==2.10.0 keras_preprocessing==1.1.1 keras_applications==1.0.8 gast==0.2.2 futures protobuf pybind11
sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v45 tensorflow
(I generally DON'T use sudo with pip, just following the official instructions for the Jetson NANO)

Pip doesn't require root user to install but requires root user to upgrade

I'm confused by the intended pip usage. Pip comes installed with Python, which is great, but I get the following warnings when new versions come out:
WARNING: You are using pip version 21.1.1; however, version 21.1.3 is available.
You should consider upgrading via the '/usr/local/opt/python#3.8/bin/python3.8 -m pip install --upgrade pip' command.
I follow the instructions to install it using the command they gave. But then it uninstalls my existing pip and is not able to install the new version.
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.1.1
Uninstalling pip-21.1.1:
ERROR: Could not install packages due to an OSError: Cannot move the non-empty directory '/usr/local/lib/python3.8/site-packages/pip-21.1.1.dist-info/': Lacking write permission to '/usr/local/lib/python3.8/site-packages/pip-21.1.1.dist-info/'.
The pip command is now unrecognized, and the official documentation for upgrading pip suggests running:
python -m pip install -U pip
which gives the same permission error.
I Google this error, and found that the community highly advises to not sudo from these questions (this and this). They also advised pip3 install --upgrade pip --user which also gave the same error. The common consensus is to only install pip packages inside virtual environments, but I'm hesitant to have pip completely uninstalled.
So I got pip to install using sudo, but it's unclear whether I've inadvertently affected (or will affect future) system-wide installations, or how I'd check for these.
I don't understand why installing pip inside /usr/local/ requires sudo, and whether I should only be using pip exclusively inside virtual environments and never outside it
pip can be installed with sudo, into a folder that you don't have permissions to write to. However, it can install packages outside of that folder (and thus, into a folder you have write permissions). However, it is recommended that you don't install pip into a root folder, and instead install it into your home directory.
The command to install pip as root is
sudo apt-get install pip
It should then prompt you for your password. I recommend using sudo whenever you install something.

pip 10 and apt: how to avoid "Cannot uninstall X" errors for distutils packages

I am dealing with a legacy Dockerfile. Here is a very simplified version of what I am dealing with:
FROM ubuntu:14.04
RUN apt-get -y update && apt-get -y install \
python-pip \
python-numpy # ...and many other packages
RUN pip install -U pip
RUN pip install -r /tmp/requirements1.txt # includes e.g., numpy==1.13.0
RUN pip install -r /tmp/requirements2.txt
RUN pip install -r /tmp/requirements3.txt
First, several packages are installed using apt, and then several packages are installed using pip. pip version 10 has been released, and part of the release is this new restriction:
Removed support for uninstalling projects which have been installed using distutils. distutils installed projects do not include metadata indicating what files belong to that install and thus it is impossible to actually uninstall them rather than just remove the metadata saying they've been installed while leaving all of the actual files behind.
This leads to the following problem in my setup. For example, first apt installs python-numpy. Later pip tries to install a newer version of numpy from e.g., /tmp/requirements1.txt, and tries to uninstall the older version, but because of the new restriction, it cannot remove this version:
Installing collected packages: numpy
Found existing installation: numpy 1.8.2
Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
Now I know at this point there are several solutions.
I could not install python-numpy through apt. However, this causes issues because python-numpy installs a few different packages as requirements, and I do not know if another part of the system relies on these packages. And in reality, there are several apt packages installed through the Dockerfile, and each one I remove seems to reveal another Cannot uninstall X error, and removes a number of other packages along with it, that our app may or may not rely on.
I could also use the --ignore-installed option when I try to pip install things that have already been installed through apt, but then again I have the same problem of every --ignore-installed argument revealing yet another thing that needs to be ignored.
I could pin pip at an older version that does not have this restriction, but I don't want to be stuck using an outdated version of pip forever.
I have been going around in circles trying to come up with a good solution that involves minimal changes to this legacy Dockerfile, and allows the app we deploy with that file to continue to function as it has been. Any suggestions as to how I can safely get around this problem of pip 10 not being able to install newer versions of distutils packages? Thank you!
UPDATE:
I did not realize that --ignore-installed could be used without a package as an argument to ignore all installed packages. I am considering whether or not this might be a good option for me, and have asked about it here.
This is the solution I ended up going with, and our apps have been running in production without any issues for close to a month with this fix in place:
All I had to do was to add
--ignore-installed
to the pip install lines in my dockerfile that were raising errors. Using the same dockerfile example from my original question, the fixed dockerfile would look something like:
FROM ubuntu:14.04
RUN apt-get -y update && apt-get -y install \
python-pip \
python-numpy # ...and many other packages
RUN pip install -U pip
RUN pip install -r /tmp/requirements1.txt --ignore-installed # don't try to uninstall existing packages, e.g., numpy
RUN pip install -r /tmp/requirements2.txt
RUN pip install -r /tmp/requirements3.txt
The documentation I could find for --ignore-installed was unclear in my opinion (pip install --help simply says "Ignore the installed packages (reinstalling instead)."), and I asked about the potential dangers of this flag here, but have yet to get satisfying answer. However, if there are any negative side effects, our production environment has yet to see the effects of them, and I think the risk is low/none (at least that has been our experience). I was able to confirm that in our case, when this flag was used, the existing installation was not uninstalled, but that the newer installation was always used.
Update:
I wanted to highlight this answer by #ivan_pozdeev. He provides some information that this answer does not include, and he also outlines some potential side-effects of my solution.
This is what worked for me--
pip install --ignore-installed <Your package name>
or
sudo pip install --ignore-installed <Your package name>
or (inside jupyter notebook)
import sys
!{sys.executable} -m pip install --ignore-installed <Your package name>
For windows
write
conda update --all
pip install --upgrade <Your package name>
OR
conda update --all
pip install <Your package name>
OR
pip install wrapt --upgrade --ignore-installed
pip install <Your package name>
from ERROR: Cannot uninstall 'wrapt'. during upgrade
You can just remove numpy manually but keep the other dependencies installed by apt. Then use pip as before to install the latest version of numpy.
#Manually remove just numpy installed by distutils
RUN rm /usr/lib/python2.7/dist-packages/numpy-1.8.2.egg-info
RUN rm -r /usr/lib/python2.7/dist-packages/numpy
RUN pip install -U pip
RUN pip install -r /tmp/requirements1.txt
The location of numpy should be the same. But if you want to confirm the location you can run the container without running the requirements.txt files and issue the following commands in the python console inside the container.
>>> import numpy
>>> print numpy.__file__
/usr/lib/python2.7/dist-packages/numpy/__init__.pyc

What is the difference between pip install and sudo pip install?

I tried installing Flask and a few packages using sudo in a virtual environment, but on trying to import Flask, it'll throw up an ImportError. On installing the same packages with pip install though it works fine.
So what's the difference between these methods? I tried this on Ubuntu.
Also, where does pip install these packages? Looking through Stack Overflow I could only find questions that answer how to list packages installed by pip, but not where to find them (in context to the virtual environment)
pip install
Will run pip install as the current user
sudo pip install
Will run pip install with the security privileges of another user, root for example.
You normally need to use sudo to install a package on a system.
You may want to read linux-101-introduction-to-sudo

Installing Python Package from Github Using PIP

I've seen it documented that you can install a Github hosting Python package using pip via:
sudo pip install -e git+git://github.com/myuser/myproject.git#egg=myproject
However, this appears to install the package to the current working directory, which is almost never where is should be.
How do you instruct pip to install it into the standard Python package directory (e.g. on Ubuntu this is /usr/local/lib/python2.6/dist-packages)?
The -e flag tells pip to install it as "editable", i.e. keep the source around. Drop the -e flag and it should do about what you expect.
sudo pip install git+git://github.com/myuser/myproject.git#egg=myproject
If that doesn't work try using https instead of git.
sudo pip install git+https://github.com/myuser/myproject.git#egg=myproject
For Python 3 make sure you have python3-pip installed (and of course git installed):
The syntax just changed to:
sudo pip3 install git+git://github.com/someuser/someproject.git

Categories

Resources