获得“权限被拒绝"在我的 Mac 上以 root 身份运行 pip 时 [英] Getting "Permission Denied" when running pip as root on my Mac

查看:23
本文介绍了获得“权限被拒绝"在我的 Mac 上以 root 身份运行 pip 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用我的 Mac 安装 Python 包,就像我在工作时使用 Windows PC 一样;但是在我的 Mac 上,我经常遇到权限被拒绝错误,同时写入日志文件或站点包.

I've started to use my Mac to install Python packages in the same way I do with my Windows PC at work; however on my Mac I've come across frequent permission denied errors while writing to log files or site-packages.

因此,我考虑在 sudo 下运行 pip install 但考虑到我只想安装它,这是安全/可接受的 sudo 使用在我当前的用户帐户下?

Therefore I thought about running pip install <package> under sudo but is that a safe/acceptable use of sudo considering I'm just wanting this to be installed under my current user account?

来自日志文件 I/O 错误的示例回溯:

Example traceback from a logfile I/O error:

Command /usr/bin/python -c "import setuptools;__file__='/Users/markwalker/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('
', '
'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/tq/hy1fz_4j27v6rstzzw4vymnr0000gp/T/pip-k6f2FU-record/install-record.txt failed with error code 1 in /Users/markwalker/build/pycrypto
Storing complete log in /Users/markwalker/Library/Logs/pip.log
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 8, in <module>
    load_entry_point('pip==1.1', 'console_scripts', 'pip')()
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main
    return command.main(args[1:], options)
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main
    log_fp = open_logfile(log_fn, 'w')
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile
    log_fp = open(filename, mode)
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

更新这可能取决于权限,但是最好的方法是为您的 Python 项目使用虚拟环境.除非绝对必要,否则应避免运行 sudo pip.

Update This was likely down to permissions, however the best approach is to use virtual environments for your python projects. Running sudo pip should be avoided unless absolutely necessary.

推荐答案

使用虚拟环境:

$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want

当您想要为全局、系统范围的 Python 安装安装内容时,您只能使用 sudo 或提升的权限.

You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation.

最好使用为您隔离包的虚拟环境.这样你就可以在不污染全局 python 安装的情况下玩耍.

It is best to use a virtual environment which isolates packages for you. That way you can play around without polluting the global python install.

作为奖励,virtualenv 不需要提升权限.

As a bonus, virtualenv does not need elevated permissions.

这篇关于获得“权限被拒绝"在我的 Mac 上以 root 身份运行 pip 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆