pip Python 3的权限错误 [英] Permission error with pip Python 3
问题描述
我在Mac OS X上使用Python3.一切都很完美,我曾经安装过这样的软件包:
I am using Python 3 on Mac OS X. Everything was perfect, I used to install packages like this:
pip install somePackage
但是IDLE停止工作,所以我不得不重新安装它.之后,当我运行pip时,我得到了pip is not defined
,因此我必须安装pip.现在,每次我尝试使用pip安装软件包时,都会出现此错误:
But IDLE stopped working so I had to re-install it. After that, when I ran pip, I got pip is not defined
so I had to install pip. Now, every time I try to install a package using pip, I get this error:
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
prefix=options.prefix_path,
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
**kwargs
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
isolated=self.isolated,
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 377, in move_wheel_files
clobber(source, dest, False, fixer=fixer, filter=filter)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 287, in clobber
ensure_dir(dest) # common for the 'include' path
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 83, in ensure_dir
os.makedirs(path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 1] Operation not permitted: '/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pygame'
我认为它可能来自该版本,所以我对其进行了更新.现在,每当我运行pip install something
时,都会出现此错误:
I thought it may be from the version so I updated it. Now, whenever I run pip install something
I get this error:
命令"/usr/bin/python -u -c"导入设置工具,标记化; 文件 ='/private/var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-build-hSNyxK/termcolor/setup.py'; f = getattr(tokenize,'open',open)(文件); code = f.read().replace('\ r \ n','\ n '); f.close(); exec(compile(code, file ,'exec'))安装--record/var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-U3ZtNq-record /install-record.txt --single-version-externally-managed --compile在/private/var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-build-hSNyxK/termcolor/
Command "/usr/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-build-hSNyxK/termcolor/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-U3ZtNq-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-build-hSNyxK/termcolor/
我什至无法删除软件包-运行pip uninstall something
时出现此错误:
I can't even delete a package - when I run pip uninstall something
I get this error:
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/uninstall.py", line 76, in run
requirement_set.uninstall(auto_confirm=options.yes)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 346, in uninstall
req.uninstall(auto_confirm=auto_confirm)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 754, in uninstall
paths_to_remove.remove(auto_confirm)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove
renames(path, new_path)
File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 267, in renames
shutil.move(old, new)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
copystat(src, dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/var/folders/bc/hwpn9sqn5352xcbst0p89snm0000gn/T/pip-OQKY_a-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'
我的点子版本是:
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
我正在使用Mac 10.12.6
I am using Mac 10.12.6
我该如何解决?
推荐答案
您看到的权限错误是由于您的site-packages
目录(pip试图安装到的目录)受到保护并拥有的行政人员.
The permission error you're seeing is due to the fact that your site-packages
directory (where pip is trying to install to) is protected and owned by an administrator.
这取决于您的Python发行版.这就是为什么我要您包括$ which python
命令(从命令行)和>>> import site; site.getsitepackages()
命令(从Python)的输出.这些将帮助您弄清楚站点包目录的位置.另外,运行pip -v
将打印pip尝试安装的目录.
It depends on your Python distribution. That's why I asked for you to include the output of the $ which python
command (from the command line), and the >>> import site; site.getsitepackages()
command (from Python). These will help you figure out where your site packages directory is. Also, running pip -v
will print the directory to which pip is trying to install.
请务必注意,Mac随附Python的系统版本.您不应该安装此系统版本的Python可访问的任何软件包,因为它已被系统使用,并且有可能干扰其正常运行和/或安装恶意版本的软件包,这些版本的恶意软件会在系统使用Python(例如,如果有人制作了一个讨厌的鸡蛋并将其命名为"os",并将其放在系统Python的site-packages目录中,则下一次系统python版本运行"import os"时,它将导入讨厌的鸡蛋.)
It is important to note that Macs come with a system version of Python. You should not install any packages accessible to this system version of Python, because it is used by the system and you can potentially interfere with its normal workings and/or install malicious versions of packages that will do bad things when the system uses that Python (e.g., if someone made a nasty egg and called it "os" and put it in the site-packages directory for your system Python, then the next time the system python version ran "import os" it would import the nasty egg.)
以sudo方式运行pip install
命令可能会可能解决问题,但这是短期修复,非常糟糕,原因在我的评论中已经提到在@Nenand的答案中-运行pip install X
从Internet下载X的鸡蛋,并在setup.py
中发生的任何情况下运行python setup.py build && python setup.py install
.虽然很容易沾沾自喜,因为,嘿,它很奏效!,信任互联网上的陌生人也很幼稚(让我们面对现实,制作pem软件包的人都是 陌生人).
While running your pip install
command as sudo would probably solve the problem, this is a short-term fix that is very bad for the reasons mentioned in my comment in @Nenand's answer - running pip install X
downloads the egg for X from the internet, and runs python setup.py build && python setup.py install
on whatever happens to be in the setup.py
. While it's easy to be lulled into complacency because, hey, it works!, it's also naive to trust strangers on the internet (and let's face it, the people who make pip packages are strangers to you).
我什至没有想到这一点,但是也有可能您错误地键入了库,或者猜错了名称,然后结束和你真的不想要的人(窃案).请参见此BleepingComputer 有关pip域名抢注的文章.
I didn't even think of this, but there is also the potential that you mis-type a library, or guess the wrong name, and end up with one you REALLY didn't want (typosquatting). See this BleepingComputer article on typosquatting pip packages.
正确的解决方案是找出为什么您的site-packages
目录受保护,并修复其权限或更改pip使用的site-packages
目录.
The proper solution is to figure out why your site-packages
directory is protected, and either fix its permissions, or change the site-packages
directory pip is using.
我怀疑您看到此权限错误的原因是pip试图安装到系统Python的site-packages
目录,该目录归root所有,这是有充分理由的.系统Python位于/usr/bin/
,其site-packages
目录位于以下目录:
I suspect the reason you're seeing this permissions error is that pip is trying to install to the system Python's site-packages
directory, which is owned by root for a good reason. The system Python is located at /usr/bin/
and its site-packages
directory is located at the following directories:
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages'
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python'
'/Library/Python/2.7/site-packages'
在这种情况下,我的建议是使用Homebrew,Python.org或Canopy安装Python,这会将您的蛇保存在单独的笼子中,而不会把系统Python保留下来.在我的系统上,python
是系统python,python2
是Python版本2,而python3
是Python版本3.这也使我正在使用哪个版本更加清楚.我也没有pip
命令-不在我的路径上-所以我不能不小心将某些东西安装到我的系统Python中.我必须显式调用pip2
或pip3
.使用Homebrew Python将为您完成所有这些工作,因此,这是我在许多可用的Python发行版中最喜欢的建议.
My advice, in this case, is to install Python using Homebrew, Python.org, or Canopy, which will keep your snakes in separate cages and will leave the system Python alone. On my system, python
is the system python, python2
is Python version 2, and python3
is Python version 3. This also makes it more clear which version I'm using. I also do not have a pip
command - not on my path - so I can't accidentally install something to my system Python. I have to explicitly call pip2
or pip3
. Using Homebrew Python will do all of this for you, so that's my most-favored recommendation of the many Python distributions available.
这篇关于pip Python 3的权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!