Unable to install Pip |Python [duplicate] - python

This question already has an answer here:
How to install pip in Python 2.7 on macOS Big Sur?
(1 answer)
Closed 1 year ago.
User#Admins-MacBook-Pro ~ % pip install xq
Traceback (most recent call last):
File "/usr/local/bin/pip", line 11, in <module>
load_entry_point('pip==21.1.2', 'console_scripts', 'pip')()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Library/Python/2.7/site-packages/pip-21.1.2-py2.7.egg/pip/__init__.py", line 1, in <module>
from typing import List, Optional
ImportError: No module named typing
If you get above error pip install xq .Try with below command :
sudo python3 -m pip install --upgrade --force-reinstall pip
brew install python --> install the latest Python.
ls -l /usr/local/bin/python* --> List all Python versions installed on your system.
ln -s -f /usr/local/bin/python[your-latest-version-just-installed] /usr/local/bin/python --> Change default Python version to the latest version.
E.g: ln -s -f /usr/local/bin/python3.9 /usr/local/bin/python
Restart terminal.
python --version --> Check Python version default again.
Ref: https://dev.to/malwarebo/how-to-set-python3-as-a-default-python-version-on-mac-4jjf
Like it if it helps you

try the following in your mac terminal:
/usr/local/opt/python#3.9/bin/python3.9 -m pip install --upgrade pip
then:
pip install typing

From the docs:
For package maintainers, it is preferred to use
typing;python_version<"3.5" if your package requires it to support
earlier Python versions. This will avoid shadowing the stdlib typing
module when your package is installed via pip install -t . on Python
3.5 or later.
xq might need python3 for smooth running, did not check it though. Maybe you can elaborate a little of your expectation in the question

Related

Pip broken after upgrade. Cannot unsinstall or upgrade Pip or Python

I recently had tried to run a pip command and noticed my pip version was considerably out of date so upgraded it. This seems to have completely broken my pip install and I cannot figure out how to reset it.
$ pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
You are using pip version 8.1.1, however version 22.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/f3/77/23152f90de45957b59591c34dcb39b78194eb67d088d4f8799e9aa9726c4/pip-22.1-py3-none-any.whl (2.1MB)
100% |████████████████████████████████| 2.2MB 505kB/s
Installing collected packages: pip
Successfully installed pip-8.1.1
You are using pip version 8.1.1, however version 22.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ pip3 --version
Traceback (most recent call last):
File "/usr/bin/pip3", line 11, in <module>
sys.exit(main())
File "/home/myname/.local/lib/python3.5/site-packages/pip/__init__.py", line 11, in main
from pip._internal.utils.entrypoints import _wrapper
File "/home/myname/.local/lib/python3.5/site-packages/pip/_internal/utils/entrypoints.py", line 12
f"pip{sys.version_info.major}",
^
SyntaxError: invalid syntax
This seems to come from this: https://github.com/pypa/pip/issues/9526
I can't seem to uninstall pip:
$ python3 -m pip uninstall pip
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/myname/.local/lib/python3.5/site-packages/pip/__main__.py", line 29, in <module>
from pip._internal.cli.main import main as _main
File "/home/myname/.local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 57
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
I'm on python3.5 and can't seem to upgrade that either:
$ sudo apt-get install python3.6
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python3.6
E: Couldn't find any package by glob 'python3.6'
E: Couldn't find any package by regex 'python3.6'
I'm found a number of stackoverflow answers suggesting to use alternative repositories but they're not working either.
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python3.6
E: Couldn't find any package by glob 'python3.6'
E: Couldn't find any package by regex 'python3.6'
I've tried 3.6, 3.7, 3.8, 3.9 and 3.10 with the same results.
I've also found some stack overflow answers suggesting to edit /usr/bin/pip3
Originally it looks like this:
from pip import main
if __name__ == '__main__':
sys.exit(main())
I've tried this:
from pip._internal import __main__
if __name__ == '__main__':
sys.exit(main())
and this:
I've tried this:
from pip._internal import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
But neither have resolved the issue.
To be sure i'm not messed up with a local version i've tried all of the above using /usr/bin/pip3 --version rather than just pip3 --version with the same results.
Now i'm at a bit of a loss how to get a working version of Python and Pip.
Suggestion from question:
$ sudo python3.5 -m easy_install pip
Searching for pip
Best match: pip 22.1
Adding pip 22.1 to easy-install.pth file
Installing pip3 script to /usr/local/bin
Installing pip3.10 script to /usr/local/bin
Installing pip script to /usr/local/bin
Using /home/myname/.local/lib/python3.5/site-packages
Processing dependencies for pip
Finished processing dependencies for pip
$ ls -lh /usr/local/bin/*pip*
-rwxr-xr-x 1 root root 361 May 12 19:49 /usr/local/bin/pip
-rwxr-xr-x 1 root root 363 May 12 19:49 /usr/local/bin/pip3
-rwxr-xr-x 1 root root 369 May 12 19:49 /usr/local/bin/pip3.10
All 3 return the same thing:
$ /usr/local/bin/pip --version
$ /usr/local/bin/pip3 --version
$ /usr/local/bin/pip3.10 --version
Traceback (most recent call last):
File "/usr/local/bin/pip", line 11, in <module>
load_entry_point('pip==22.1', 'console_scripts', 'pip')()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 561, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2291, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2297, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/home/myname/.local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 57
sys.stderr.write(f"ERROR: {exc}")
^
I've now installed Python3.6 using "method two" from here: https://askubuntu.com/questions/1152640/apt-install-doesnt-install-python3-6 i.e. manual install
I've then upgraded pip using:
sudo python3.6 -m pip install pip --upgrade
Now pip --version and pip3 --version return an error but directly accessing the bin works:
$ /usr/local/bin/pip --version
pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
The aliases are currently pointing to:
$ which pip
/home/sortitoutsi/.local/bin/pip
$ which pip3
/home/sortitoutsi/.local/bin/pip3
It seems that other things are now broken, such as aws.
$ aws --help
Traceback (most recent call last):
File "/usr/local/bin/aws", line 19, in <module>
import awscli.clidriver
File "/usr/local/lib/python2.7/dist-packages/awscli/clidriver.py", line 17, in <module>
import botocore.session
File "/usr/local/lib/python2.7/dist-packages/botocore/session.py", line 29, in <module>
import botocore.configloader
File "/usr/local/lib/python2.7/dist-packages/botocore/configloader.py", line 19, in <module>
from botocore.compat import six
File "/usr/local/lib/python2.7/dist-packages/botocore/compat.py", line 26, in <module>
from dateutil.tz import tzlocal
File "/usr/local/lib/python2.7/dist-packages/dateutil/tz/__init__.py", line 1, in <module>
from .tz import *
File "/usr/local/lib/python2.7/dist-packages/dateutil/tz/tz.py", line 16, in <module>
from six import string_types, PY3
ImportError: No module named six
Simply reinstalling aws sudo python -m pip install awscli seemed to have fixed the above. I'm guessing a sudo apt-get update at some point forced an upgrade of awscli after finding an updated Python installation.
Do not try and install Python versions and packages (from another ppa) that are not supported by your OS. Your system may break.
Your attempted Pip installation is, luckily, in a local directory: $HOME/.local/. Check if there is anything else installed in $HOME/.local/bin/ and $HOME/.local/lib/python3.5/site-packages/. If nothing (or nothing that seems important), rename that directory:
mv ~/.local ~/.local_aside
Now, you should have the system Pip back again.
You may lose some other packages that you installed earlier (which would now be in ~/.local_aside, but possibly, you can reinstall those. Either with a system packages (e.g., python3-numpy), or again with pip3 install numpy and the like. Though pip may have trouble finding a correct NumPy version, since Python 3.5 is not supported anymore (the system package should be fine).
If you want to use a more recent version of Python, install a local (user-only) one. My preferred way of doing that is to use pyenv, which requires some set up (installing the necessary build packages). If you use pyenv, carefully read the installation instructions for your case. Once you get it going, you can quickly install any version of Python, and between minor versions, they won't even get in the way (that is, python3.9 -m pip install numpy will be a separate installation from python3.10 -m pip install numpy, and both can happily live next to each other if so wanted).
But don't use sudo and install it as a root: your system will get confused and may break.
Another alternative is Conda, in the form (my preference) of miniconda3. Conda can and will also install C library dependencies, and actively tries to resolve version conflicts between Python packages (in case you have to install a lot of packages). Again, browse the manual to get going, but it's relatively straightforward to use, and doesn't get in the way of your OS.

Installing jupyter with Python 3.8 fails with "SyntaxError: invalid syntax" because it's using Python 2.7

On a mac, I've installed Python 3.8 and I am trying to install jupyter using:
pip install jupyter
It runs, but I get the following output in the terminal:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 11, in <module>
load_entry_point('pip==21.0.1', 'console_scripts', 'pip')()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2843, in load_entry_point
return ep.load()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2434, in load
return self.resolve()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Library/Python/2.7/site-packages/pip-21.0.1-py2.7.egg/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
Seems odd as I am on Python 3.8 and the output seems to refer to Python 2.7.
Anyway, when I then try
jupyter notebook
Still, the command jupyter is not found.
From the error
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Library/Python/2.7/site-packages/pip-21.0.1-py2.7.egg/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
you can see that the pip you used is from the Python 2.7 installation. The latest pip 21.0 will not work on that version, and f-strings are only available starting Python 3.6.
The installation failed by the way, so trying to use jupyter command after that would, of course, still not work.
Seems odd as I am on Python 3.8 and output seems to refer to Python 2.7
You will always have to explicitly indicate which Python version you want to use. Since macOS comes with a built-in Python 2.7 installation, you'll have to explicitly indicate the Python 3 specific one, usually python3 and pip3.
Depending on how you installed your Python 3.8, best practices are:
Using the <python> -m pip command, which means "use the pip that comes from this <python> installation". (You can check the version with python -V)
~$ python3 -V
Python 3.9.1
~$ python3 -m pip install jupyter
Creating a virtual environment and installing your packages inside the virtual environment
~$ python3 -V
Python 3.9.1
~$ python3 -m venv myenv
~$ source ./myenv/bin/activate
(myenv) ~$ python -V
Python 3.9.1
(myenv) ~$ python3 -V
Python 3.9.1
(myenv) ~$ pip install jupyter
The virtual environment takes care of using the correct Python versions and pip install-ing things also uses the correct versions. Compared to option 1 which installs things system-wide, the packages you installed in a virtual environment will only be available when you activate the virtual environment.

netplan not working after change default python

in ubuntu 18.04, when i change default python from python 3.6 to other version by this command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
or when i remove python 3.6 and install other version netplan apply not working and result this error:
File "/usr/sbin/netplan", line 20, in <module>
from netplan import Netplan
File "/usr/share/netplan/netplan/__init__.py", line 18, in <module>
from netplan.cli.core import Netplan
File "/usr/share/netplan/netplan/cli/core.py", line 24, in <module>
import netplan.cli.utils as utils
File "/usr/share/netplan/netplan/cli/utils.py", line 25, in <module>
import netifaces
ModuleNotFoundError: No module named 'netifaces'
and command pip install netifaces has some errors.
I faced the same issue before with vagrant. If you use update-alternatives to make python3 alias points to another version of Python, vagrant will not work. You cannot use update-alternatives to change the alias of Python3.
For some reason I lost my python configuration after an update made by my ubuntu server,solved by:
Checking if I could import the module so on CLI:
python
import python
After getting the same message I realize my python environment was falling to use the modules even when it all showed up as installed I went ahead and "upgrade" the python modules:
python pip install --upgrade pip
python pip install --upgrade netifaces
python pip install --upgrade "any module you need to use for your script"
And just like that modules were recognized updated and properly install.
tl;dr: Create a link netifaces.so to netifaces.cpython-36m-x86_64-linux-gnu.so in /usr/lib/python3/
In /usr/lib/python3/dist-packages/ is only a module for python3.6:
vagrant#ubuntu18.04:~$ find /usr/lib/python3/dist-packages/ -type f -name netifaces\*
/usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gnu.so
That's why importing 'netifaces' failes for python3.8 while it works for python3.6:
vagrant#ubuntu18.04:~$ python3.6 -c 'import netifaces; print("works!")'
works!
vagrant#ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'netifaces'
One can link to it with a more unspecific name so python3.8 can find and use it:
vagrant#ubuntu18.04:~$ sudo ln -s /usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gn
u.so /usr/lib/python3/dist-packages/netifaces.so
vagrant#ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
works!
Hint: I had to do the same for apt_pkg.so → apt_pkg.cpython-36m-x86_64-linux-gnu.so

ImportError: The 'packaging.requirements' package is required

I'm running VPS with ubuntu:
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
I tried to upgrade Python to 2.7.16 from 2.7.6.
Just after upgrade Gunicorn library failed to start with
Traceback (most recent call last):
File "/home/user/bin/python-2.7/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 9, in <module>
from gunicorn.app.base import Application
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 12, in <module>
from gunicorn import util
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/util.py", line 12, in <module>
import pkg_resources
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/pkg_resources/__init__.py", line 82, in <module>
__import__('pkg_resources.extern.packaging.requirements')
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/pkg_resources/extern/__init__.py", line 61, in load_module
"distribution.".format(**locals())
ImportError: The 'packaging.requirements' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
I was told that it could be some setuptools issue, so I renewed them with
python -m ensurepip
Looking in links: /tmp/tmp98U8zf
Requirement already satisfied: setuptools in ./bin/python-2.7/lib/python2.7/site-packages (41.1.0)
Requirement already satisfied: pip in ./bin/python-2.7/lib/python2.7/site-packages (19.2.2)
(no changes)
I tried to do
pip install packaging
(no changes)
or downgrade setuptools to 19.2 (as suggested in Python 2.7 The 'packaging' package is required; normally this is bundled with this package)
and got
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/config.py", line 8, in <module>
import copy
File "/home/user/bin/python-2.7/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/home/user/bin/python-2.7/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Is there any working way to upgrade to 2.7.16 python in my case?
UPD:
I upgraded python with it with
wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
make clean && ./configure --enable-unicode=ucs4 --enable-shared --prefix=/home/user/bin/python-2.7/ --with-ensurepip=install && make && make install
pip install -r ~/django/django_projects/requirements.txt
pip freeze: https://www.pastiebin.com/5d592ea701503
Thanks.
Scenario
First: When trying to execute a Python script, the following error message appears:
ImportError: The 'packaging.requirements' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
Second: When trying to check the version of setuptools with pip show setuptools (or when using any other pip command), the next error message appears:
ImportError: cannot import name _remove_dead_weakref
Reason
This is because the current Python installation is somehow screwed up. In my case the mess was caused by an upgrade from Ubuntu 16.04 to Ubuntu 18.04.
Solution
With Virtual Environment
In the best case you were already using a virtual environment (this was my case). The solution here would be to recreate/setup your venv again (step-by-step):
$ cd /path/to/your/venv
# remove your old venv
$ rm -rf ./*
# create a new one
$ /usr/bin/virtualenv . --python=YOUR-PYTHON-VERSION
# activate venv
$ source bin/activate
# verify the correct python version is installed
$ python --version
For example replace YOUR-PYTHON-VERSION with python2.7 or python3.7.
After recreating your venv the problem should be fixed and you should be able to use pip again.
Without Virtual Environment
I think the best way to fix the problem would be to completely uninstall all non-default Python versions (for example Ubuntu 18.04 comes with Python 3.6+) and then make a clean reinstall of all other Python versions needed.
Try upgrading pip by typing:
pip install --upgrade pip

pip3 ImportError: cannot import name 'IncompleteRead'

Having trouble installing modules via pip3, tried several suggestions from top voted thread from Dec' 2014, but still getting the following:
sudo pip3 install send2trash
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
load_entry_point('pip==1.5.6', 'console_scripts', 'pip3')()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 558, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2682, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2355, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2361, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 74, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/lib/python3/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
from pip.download import path_to_url
File "/usr/lib/python3/dist-packages/pip/download.py", line 25, in <module>
from requests.compat import IncompleteRead
ImportError: cannot import name 'IncompleteRead'
On Ubuntu 15.10; tried removing python 3 completely and reinstalling, still having issues. Help?
Edit: UPDATE: Tried using my bootable USB to repair Ubuntu and get back to original config and reinstalled via sudo apt-get install python3-pip, but now it generates a crash report saying:
Sorry, the application pip3 has unexpectedly crashed.
This seems to be an issue with pip's interaction with the requests package. requests dropped the IncompleteRead module but older versions of pip, which you have, still rely on it, newer versions don't.
You should try updating pip via easy_install-3.5 (if you have Python 3.5) or easy_install-3 for other versions:
sudo easy_install-3.5 -U pip
or:
sudo easy_install-3 -U pip
and you should be good to go.
Had similar error on Raspberry Pi. The solution was a combination of all proposals above:
sudo pip install --upgrade pip
sudo pip install --upgrade requests
sudo easy_install3 -U pip
It happened me when I tried to install a pip module to my Python3 installation. I mistakenly run pip instead of pip3 and it installed Python2 modules into Python3 installation. After that I realized command was wrong, I run pip3 this time. But, it was no good, installation was blown up, there were errors in Python2 like parenthesis missing in print statement(obviously this is an error for Python3). Eventually, it blown up my terminal too, I needed to install another terminal.
Like you did, I tried to install and reinstall everything, but it didn't work. Fix needed to be done manually. After trying several days and failing, I backed up my files and reinstalled Ubuntu.
I know this is not what you want, but maybe you can save your time by accepting that reinstalling the OS is the easiest solution.
Make sure you install newest versions of pip and requests:
pip install --upgrade pip
pip install --upgrade requests

Categories