Reputation: 2221
I have a problem when I try to use pip in any way. I'm using Ubuntu 16.04.4
I should say that I've used it already, and I never had any problem, but starting today when I use any command I always get the same error (as an example using pip --upgrade
).
Traceback (most recent call last):
File "/usr/local/bin/pip", line 7, in <module>
from pip._internal import main
ImportError: No module named _internal
I have tried doing sudo apt-get remove python-pip
followed by sudo apt-get install python-pip
but nothing changed.
Upvotes: 212
Views: 367505
Reputation: 69
I got this issue after trying to upgrade to python 3.12
The fix for me was curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
Test with pip3.12 -v
Then issue echo "alias pip=$(which pip3.12)" >> ~/.bashrc
and don't forget source ~/.bashrc
Upvotes: 0
Reputation: 496
On Mac and MiniConda, I used the following command and it worked:
conda activate /opt/miniconda3 /opt/miniconda3/bin/conda install -p /opt/miniconda3 pip -y
Upvotes: 0
Reputation: 6159
I have fixed this error by running the following commands:
sudo apt remove python-pip
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
It will remove the previously installed pip and reinstall it.
Upvotes: 3
Reputation: 187
Please verify the directory permission for /usr/local/lib/python3.9/ and modify the permission using chown command
sudo chown -R centos:centos /usr/local/lib/python3.9/
Upvotes: 0
Reputation: 148
I did
py -m install packagename
instead of
pip install packagename
Upvotes: 0
Reputation: 532
I also had this problem because some of my folders got corrupted due to a hard disk failure, due to this the pip_internal folder also got corrupted.
To fix this, I first ran a disk repair restart so that the corrupted folders get removed from the drive, after that I deleted the whole pip
folder from the site packages (Python>Lib>site-packages>pip).
Then I ran this command in terminal: python -m ensurepip
and it reinstalled the whole pip package. After this pip is working fine.
Upvotes: 0
Reputation: 131626
In some cases, this can occur because pip was upgraded by the root user, and the upgrade - for some reason - defaults to creating non-all-user-accessible subdirectories and files in the "site-packages" directory.
To check if this is the case, we need locate the site-packages directory, e.g. with python3, running this command:
python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
Now let's check whether there are pip-related subdirs there with problematic permissions:
site_pkg_dirs=$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
pushd ${site_pkg_dirs}
ls -ld pip*
popd
If the output is empty - then this answer won't solve your problem; sorry :-(
But if the output is not empty, e.g.:
drwxr-x--- 5 root root 111 Jan 01 00:00 pip
drwxr-x--- 2 root root 170 Jan 01 00:00 pip-21.3.1.dist-info
then carefully examine the permission listing on each: If the "all" permission triplet doesn't have r
and x
- you need to add those; and since this same problem might be manifested for other packages, let's be more thorough about it:
find ${site_pkg_dirs} -type d | chmod a+rx
find ${site_pkg_dirs} -type f | chmod a+r
this ensures all subdirectories in your site package directory are readable and recursable, and that all files in those subdirectories are readable. Specifically, this will apply to the pip-related subdirs, which should now look like this when you ls -l
them:
drwxr-xr-x 5 root root 111 Jan 01 00:00 pip
drwxr-xr-x 2 root root 170 Jan 01 00:00 pip-21.3.1.dist-info
This should solve your pip troubles! Let's check: If you now run
pip show pip
you should get something like :
$ pip3.6 show pip
Name: pip
Version: 21.3.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: https://pip.pypa.io/
Author: The pip developers
Author-email: [email protected]
License: MIT
Location: /usr/lib/python/site-packages
Requires:
Required-by:
possibly with a different version and your own site packages dir.
Upvotes: 1
Reputation: 311
pip is not being installed properly on your "user", so try the following :
sudo python3 -m pip install --user --upgrade pip
Upvotes: 2
Reputation: 881
Checking from pip documentation, this command worked to me:
python -m ensurepip --upgrade
Upvotes: 9
Reputation: 314
windows OS:
1、download this file:“https://bootstrap.pypa.io/get-pip.py”,Put it in this(E:\PythonProject\venv\Scripts’(Your virtual environment installation directory)) directory!
2、open ‘Windows PowerShell’
3、cd ‘E:\PythonProject\venv\Scripts’(Your virtual environment installation directory)
4、run cmd ‘py get-pip.py’
Upvotes: 0
Reputation: 2863
An answer from askUbuntu works.
For pip2.7, you can at first curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
, then python2.7 get-pip.py --force-reinstall
to reinstall pip.
Problem solved. Also works for python3.
Upvotes: 96
Reputation: 511
Are you using python 2 or python 3? The following commands could be different!
python3 -m pip --version
to see if you have pip installed.python3 -m pip install --upgrade pip
.sudo apt-get install python3-pip
, and do it again.Upvotes: 40
Reputation: 43
These often comes from using pip to "update" system installed pip, and/or having multiple pip installs under user. My solution was to clean out the multiple installed pips under user, reinstall pip repo, then "pip install --user pip" as above.
See: https://github.com/pypa/pip/issues/5599 for an official complete discussion and fixes for the problem.
Upvotes: 0
Reputation: 48406
Refer to this issue list
sudo easy_install pip
works for me under Mac OS
For python3
, may try sudo easy_install-3.x pip
depends on the python 3.x version. Or python3 -m pip install --user --upgrade pip
Upvotes: 22
Reputation: 2139
This solution works for me:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --force-reinstall
or use sudo for elevated permissions (sudo python3 get-pip.py --force-reinstall
).
Of course, you can also use python
instead of python3
;)
Upvotes: 88
Reputation: 154
My solution is adding import pip
to the script linked to the pip/pip3
commands.
Firstly, open the file (e.g. /usr/local/bin/pip
) with your favorite text editor and the sudo mode. For example, I use sudo vim /usr/local/bin/pip
to open the script file.
You will obtain some file as following:
import re
import sys
from pip._internal import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
Afterwards, insert the statement import pip
just before the from pip._internal import main
then the issue is resolved.
Upvotes: 0
Reputation: 28389
I met the same error on Windows when I tried to install a package via pip3:
Traceback (most recent call last):
File "d:\anaconda\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\anaconda\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Anaconda\Scripts\pip3.6.exe\__main__.py", line 5, in <module>
ModuleNotFoundError: No module named 'pip._internal'
My python is installed via Anaconda. I solved this issue by reinstalling pip via conda:
conda install pip
After that, pip returns to normal.
Upvotes: 2
Reputation: 504
For the current user only:
easy_install --user pip
or
python -m pip install --upgrade --user pip
The second may give /usr/bin/python: No module named pip
Even if which pip
finds the module named pip.
In this case try the easy_install
Upvotes: 1
Reputation: 1
you can remove it first, and install again ,it will be ok. for centos:
yum remove python-pip
yum install python-pip
Upvotes: 0
Reputation: 4234
This did it for me:
python -m pip install --upgrade pip
Environment: OSX && Python installed via brew
Upvotes: 231
Reputation: 141
my solution: first step like most other answer:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python2.7 get-pip.py --force-reinstall
second, add soft link
sudo ln -s /usr/local/bin/pip /usr/bin/pip
Upvotes: 0
Reputation: 718
I just encountered the same problem and in my case, it turns out this is a conflict between the python installation in my virtualenv and the site-wide python (Ubuntu). What solves it for me is to run pip in this way, to force usage of the correct python installation (in my vortualenv):
python3 -m pip install PACKAGE
instead of
pip3 install PACKAGE
I realised this when I tried to follow some of the answers here that suggest re-installing pip and the error output I got was pointing to an existing site-wide python library path although I had activated my virtualenv. Worth trying before deleting and re-installing stuff.
Upvotes: 6
Reputation: 1
I have the same problem on my virtual environment after upgrade python installation from 3.6 to 3.7 but only on vent globally pip work fine, to solve it I deactivate and delete my virtual environment after recreate again and now is fine, on venv:
deactivate
rm -rvf venv
and after recreate the virtual environment. I use mac OS 10.11, and python 3
Upvotes: 0
Reputation: 377
In file "/usr/local/bin/pip" change from pip._internal import main
to from pip import main
Upvotes: 17
Reputation: 49
Nothing worked for me, but only one thing: I used sudo in front of the command and it is working fine.
Upvotes: 2
Reputation: 359
This issue maybe due to common user do not have privilege to access packages py file.
1. root user can run 'pip list'
2. other common user cannot run 'pip list'
[~]$ pip list
Traceback (most recent call last):
File "/usr/bin/pip", line 7, in <module>
from pip._internal import main
ImportError: No module named pip._internal
Check pip py file privilege.
[root@]# ll /usr/lib/python2.7/site-packages/pip/
合計 24
-rw------- 1 root root 24 6月 7 16:57 __init__.py
-rw------- 1 root root 163 6月 7 16:57 __init__.pyc
-rw------- 1 root root 629 6月 7 16:57 __main__.py
-rw------- 1 root root 510 6月 7 16:57 __main__.pyc
drwx------ 8 root root 4096 6月 7 16:57 _internal
drwx------ 18 root root 4096 6月 7 16:57 _vendor
solution : root user login and run
chmod -R 755 /usr/lib/python2.7
fix this issue.
Upvotes: 23
Reputation: 161
Its probably due to a version conflict, try to run this, it will remove the older pip somehow.
sudo apt remove python pip
Upvotes: 3
Reputation: 19552
I've seen this issue when PYTHONPATH
was set to include the built-in site-packages
directory. Since Python looks there automatically it is unnecessary and can be removed.
Upvotes: 7