Pip permission denied on Ubuntu 20.04 - python

For some reasons, I was installing python packages using sudo pip for python3.6 back in my Ubuntu 18.04. I recently upgraded ubuntu version to 20.04 which comes with python3.8 which I want to continue with. Therefore, now, my system has three python versions - 2.7, 3.6, 3.8.
$ pip -V
pip 20.2.3 from /usr/local/lib/python2.7/dist-packages/pip-21.0.1-py2.7.egg/pip (python 2.7)
$ pip3 -V
pip 20.3.4 from /home/dell/.local/lib/python3.8/site-packages/pip (python 3.8)
$ python -V
2.7.18
$ python3 -V
Python 3.8.10
But whenever I install python packages using pip3, it shows the following error -
$ pip3 install numpy
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.4.1 requires numpy~=1.19.2, but you have numpy 1.22.0 which is incompatible.
Successfully installed numpy-1.22.0
ERROR: Exception:
Traceback (most recent call last):
File "/usr/lib/python3.8/shutil.py", line 791, in move
os.rename(src, real_dst)
PermissionError: [Errno 13] Permission denied: '/tmp/pip-target-x7bedgtq/lib/python/numpy-1.22.0.dist-info' -> '/usr/local/lib/python3.6/dist-packages/numpy-1.22.0.dist-info'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 223, in _main
status = self.run(options, args)
File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 180, in wrapper
return func(self, options, args)
File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 453, in run
self._handle_target_dir(
File "/home/dell/.local/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 510, in _handle_target_dir
shutil.move(
File "/usr/lib/python3.8/shutil.py", line 807, in move
copytree(src, real_dst, copy_function=copy_function,
File "/usr/lib/python3.8/shutil.py", line 557, in copytree
return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
File "/usr/lib/python3.8/shutil.py", line 458, in _copytree
os.makedirs(dst, exist_ok=dirs_exist_ok)
File "/usr/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/numpy-1.22.0.dist-info'
It is somehow pointing to python3.6 numpy package which I installed using sudo pip. How can I solve this? Is there anyway that python3.6 packages can be unblocked from sudo access?
Or will it be fine to uninstall all the packages in '/usr/local/lib/python3.6/dist-packages/'?
EDIT for #psarka comment,
$ sudo python3.6 -m pip uninstall numpy
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3/dist-packages/pip/__main__.py", line 16, in <module>
from pip._internal.cli.main import main as _main # isort:skip # noqa
File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/usr/lib/python3/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/usr/lib/python3/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
from pip._internal.cli import cmdoptions
File "/usr/lib/python3/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'

This is not a direct answer to your question, as you are doing things the wrong way, and facing the direct consequences of doing things the wrong way. So stop, and use venv! :)
cd my_project
python3.8 -m venv venv
source venv/bin/activate
pip install whatever
If you don't have python3.8-venv, install it:
sudo apt install python3.8-venv

Related

Accidentally deleted pip on my system when downgrading Python, now pip won't install

I downgraded my Python from 3.9 to 3.8, but Pip was still installing to 3.9 after the downgrade so I uninstalled Pip. I tried to reinstalling pip using: python get-pip.py from pip's documentation and pip install --upgrade --force pip, and both gives the same error:
ERROR: Exception:
Traceback (most recent call last):
File "/Users/Dan/Library/Python/3.8/lib/python/site-packages/pip/_internal/cli/base_command.py", line 180, in _main
status = self.run(options, args)
File "/Users/Dan/Library/Python/3.8/lib/python/site-packages/pip/_internal/cli/req_command.py", line 204, in wrapper
return func(self, options, args)
File "/Users/Dan/Library/Python/3.8/lib/python/site-packages/pip/_internal/commands/install.py", line 449, in run
self._handle_target_dir(
File "/Users/Dan/Library/Python/3.8/lib/python/site-packages/pip/_internal/commands/install.py", line 503, in _handle_target_dir
shutil.rmtree(target_item_dir)
File "/usr/local/Cellar/python#3.8/3.8.10/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 718, in rmtree
_rmtree_safe_fd(fd, path, onerror)
File "/usr/local/Cellar/python#3.8/3.8.10/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 675, in _rmtree_safe_fd
onerror(os.unlink, fullname, sys.exc_info())
File "/usr/local/Cellar/python#3.8/3.8.10/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 673, in _rmtree_safe_fd
os.unlink(entry.name, dir_fd=topfd)
PermissionError: [Errno 13] Permission denied: 'RECORD'
When I run pip in terminal, I get:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'
When I run which pip, I get:
/usr/local/bin/pip
The PermissionError you get makes me think you're not running it as sudo, especially since it's trying to use os.unlink. Try as sudo and see if it works then!

Unable to Install Python Packages on Linux

I am a new to Linux and have dual booted my system. I specifically want to install numpy, darknet or lightnet python packages. I tried following the instructions given here
When I run the below command:
pip install darknetpy
I am seeing the following error:
Collecting darknetpy
Using cached darknetpy-2.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-5J0HcA/darknetpy/setup.py", line 6, in <module>
import urllib.request
ImportError: No module named request
I am unable to understand what this error is about. When I tried installing numpy package, I get the below error:
Exception:
Traceback (most recent call last):
File "/home/test/.local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/home/test/.local/lib/python2.7/site-packages/pip/commands/install.py", line 342, in run
prefix=options.prefix_path,
File "/home/test/.local/lib/python2.7/site-packages/pip/req/req_set.py", line 784, in install
**kwargs
File "/home/test/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 851, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/home/test/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
isolated=self.isolated,
File "/home/test/.local/lib/python2.7/site-packages/pip/wheel.py", line 345, in move_wheel_files
clobber(source, lib_dir, True)
File "/home/test/.local/lib/python2.7/site-packages/pip/wheel.py", line 316, in clobber
ensure_dir(destdir)
File "/home/test/.local/lib/python2.7/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
os.makedirs(path)
File "/usr/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/numpy-1.13.3.dist-info'
Kindly help in solving this error.
OSError: [Errno 13] Permission denied Typically means you the invoker have insufficient permissions.
It looks like you are trying to pip install it using python2, as eyllanesc pointed out the package you're installing is only supported by python3, so you will have to install it first.
Here are some links to help with that:
http://ubuntuhandbook.org/index.php/2017/07/install-python-3-6-1-in-ubuntu-16-04-lts/
http://docs.python-guide.org/en/latest/starting/install3/linux/
https://tecadmin.net/install-python-3-6-on-centos/
https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f (Raspbian)
https://unix.stackexchange.com/questions/332641/how-to-install-python-3-6 (debian)
Try running the command again with sudo as in sudo pip install darknetpy, or as a super user (typically gained with sudo su) then run the command normaly.
If you are still getting ImportErrors when you are trying to pip install something, try pip installing the module that is missing.

AttributeError when running pip in virtualenv

I recently upgraded python from 2.7.6 to 2.7.9 using homebrew. I then tried to 'revirtualise' an existing virtual environment:
sudo virtualenv -p python2.7 existingvenv
But am getting this error:
Traceback (most recent call last):
File "venv.../bin/pip", line 7, in <module>
from pip import main
File "venv.../lib/python2.7/site-packages/pip/__init__.py", line 9, in <module>
from pip.log import logger
File "venv.../lib/python2.7/site-packages/pip/log.py", line 8, in <module>
from pip import backwardcompat
File "venv.../lib/python2.7/site-packages/pip/backwardcompat/__init__.py", line 66, in <module>
from urllib2 import URLError, HTTPError
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 93, in <module>
import hashlib
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 138, in <module>
_hashlib.openssl_md_meth_names)
AttributeError: 'module' object has no attribute 'openssl_md_meth_names'
----------------------------------------
...Installing setuptools, pip...done.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 2352, in <module>
main()
File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 825, in main
symlink=options.symlink)
File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 993, in create_environment
install_wheel(to_install, py_executable, search_dirs)
File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 961, in install_wheel
'PIP_NO_INDEX': '1'
File "/usr/local/lib/python2.7/site-packages/virtualenv.py", line 903, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /Users/robertelliott...hflow-app/bin/python -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip failed with error code 1
I also get the above error when running any pip install x inside the above active virtual environment.
I don't get the error when creating a new virtualenv:
$ virtualenv -p python2.7 newvenv
Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
New python executable in newenv/bin/python
Installing setuptools, pip...done.
Is there an old symlink or something inside the existing virtualenv causing the issue and how can I resolve this?
This is because of a new feature in Python, which wheel 0.24 is not compatible with. You'll find a folder called virtualenv_support containing base install files for setuptools, pip, and wheel under your Python 2.7 system install. There are a few solutions. The easiest is to just remove wheel-0.24* from the virtualenv_support folder. If you want to go a step farther, you could download version 0.26 of the wheel .whl file and replace it.
did you try:
sudo virtualenv --clear -p python2.7 existingvenv
?

ImportError while installing pip in virtualenv

I have a rhel machine with python2.6 installed on it. I've been trying to have an alternate install of python2.7 and set up a virtualenv for using 2.7. I installed python2.7 by building from source as follows:
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
I already had virtualenv installed on the system so I used it to create a venv for 2.7 as follows:
virtualenv -p python2.7 --no-setuptools py27
. py27/bin/activate
Now when I try to install pip inside the venv, it fails like so:
python get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 19857, in <module>
main()
File "get-pip.py", line 151, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 81, in bootstrap
import pip
File "/tmp/tmpArPs31/pip.zip/pip/__init__.py", line 15, in <module>
File "/tmp/tmpArPs31/pip.zip/pip/vcs/mercurial.py", line 11, in <module>
File "/tmp/tmpArPs31/pip.zip/pip/download.py", line 29, in <module>
File "/tmp/tmpArPs31/pip.zip/pip/_vendor/__init__.py", line 81, in load_module
ImportError: No module named 'pip._vendor.requests'
Can't figure out what's going wrong here. Please help.
I've installed python 2.7.8.
EDIT: I initially tried creating virtualenv without --no-setuptools option but that gave me the same error:
virtualenv -p python2.7 py27_with_pip
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in py27_with_pip/bin/python2.7
Also creating executable in py27_with_pip/bin/python
Installing setuptools, pip...
Complete output from command /data1/home/sagraw1/...th_pip/bin/python2.7 -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0- py2.py3-none-any.whl/pip/__init__.py", line 15, in <module>
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-py2.py3-none-any.whl/pip/vcs/mercurial.py", line 11, in <module>
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-py2.py3-none-any.whl/pip/download.py", line 29, in <module>
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv_support/pip-6.0-py2.py3-none-any.whl/pip/_vendor/__init__.py", line 81, in load_module
ImportError: No module named 'pip._vendor.requests'
----------------------------------------
...Installing setuptools, pip...done.
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 2363, in <module>
main()
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 848, in main
symlink=options.symlink)
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 1016, in create_environment
install_wheel(to_install, py_executable, search_dirs)
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 984, in install_wheel
'PIP_NO_INDEX': '1'
File "/usr/lib/python2.6/site-packages/virtualenv-12.0-py2.6.egg/virtualenv.py", line 926, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /data1/home/sagraw1/...th_pip/bin/python2.7 -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip failed with error code 1
I tried installing pip with python2.7 directly but even that doesn't work:
python2.7 get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 19857, in <module>
main()
File "get-pip.py", line 151, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 81, in bootstrap
import pip
File "/tmp/tmpPSVEkk/pip.zip/pip/__init__.py", line 15, in <module>
File "/tmp/tmpPSVEkk/pip.zip/pip/vcs/mercurial.py", line 11, in <module>
File "/tmp/tmpPSVEkk/pip.zip/pip/download.py", line 29, in <module>
File "/tmp/tmpPSVEkk/pip.zip/pip/_vendor/__init__.py", line 81, in load_module
ImportError: No module named 'pip._vendor.requests'
I was in a situation similar to yours, and I eventually found the fix. At least in my case the root problem was that when I compiled python 2.7.8 the build process didn't find the proper OpenSSL libraries (because they were not installed on my system). After make finished running it showed a summary similar to this:
Failed to find the necessary bits to build these modules:
_bsddb _curses _curses_panel
_hashlib _sqlite3 _ssl
This doesn't prevent make install from working, and after that Python works just fine, except that it can't access https URLs and other related functionality. You can tell if this is your case by starting python and then typing
import ssl
If you see an ImportError, then the ssl module is missing. Otherwise, you see no message at all.
I fixed this by installing the OpenSSL libraries by running (as root):
yum install openssl-devel
After this was in place, I rebuilt Python with make (now _ssl was not listed as missing), followed by make install. After this, running get-pip.py worked without a hitch.
Hope this helps!

unable to use pip with Python 3.2 using distribute

After installing python3.2 from src, I tried running:
curl -O http://python-distribute.org/distribute_setup.py
sudo python3.2 distribute_setup.py
sudo python3.2 -m easy_install pip
sudo python3.2 -m pip lxml
This gives:
Traceback (most recent call last):
File "/usr/local/lib/python3.2/runpy.py", line 140, in _run_module_as_main
mod_name, loader, code, fname = _get_module_details(mod_name)
File "/usr/local/lib/python3.2/runpy.py", line 110, in _get_module_details
return _get_module_details(pkg_main_name)
File "/usr/local/lib/python3.2/runpy.py", line 102, in _get_module_details
loader = get_loader(mod_name)
File "/usr/local/lib/python3.2/pkgutil.py", line 453, in get_loader
return find_loader(fullname)
File "/usr/local/lib/python3.2/pkgutil.py", line 463, in find_loader
for importer in iter_importers(fullname):
File "/usr/local/lib/python3.2/pkgutil.py", line 419, in iter_importers
__import__(pkg)
File "/usr/local/lib/python3.2/site-packages/pip-0.8.2-py3.2.egg/pip/__init__.py", line 56
print dist
^
SyntaxError: invalid syntax
Anyone able to get this working?
Thanks,
Matt
There is version of pip working with Python 3.
See my answer to Python 3, easy_install, pip and pypi question.
print dist is invalid in Python3. It should be print(dist).
However, with this error it is very likely that pip is not compatible with Python3. According to http://pypi.python.org/pypi/pip it also does not support Python3.

Categories