【问题标题】:easy_install and file permissionseasy_install 和文件权限
【发布时间】:2023-04-01 12:51:01
【问题描述】:

首先,我目前正在运行 OSX 10.9.1 和 Python 2.7。

我想使用easy_install(在本例中为pip)安装Python 包。首先我尝试:

easy_install pip

然后我收到一条错误消息,指出我没有对 site-packages 目录的写入权限。没问题:

sudo easy_install pip

一切正常下载。下面是我安装pip后的Bash会话:

$ pip
-bash: /usr/local/bin/pip: Permission denied
$ cd /usr/local/bin/pip
$ ls -l

...
-rwx------  1 root   admin       275 Jan 10 11:05 pip
...

$ chmod 754 pip
chmod: Unable to change file mode on pip: Operation not permitted
$ sudo chmod 754 pip
$ pip
Traceback (most recent call last):
  File "./pip", line 5, in <module>
    from pkg_resources import load_entry_point
  File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 3007, in <module>

  File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 728, in require
    requirements specified when this environment was created, or False
  File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 626, in resolve

pkg_resources.DistributionNotFound: pip==1.5

好的,所以也许有些权限仍然搞砸了。这是 Finder 在我查看 /Library/Python/2.7/site-packages/ 时向我显示的内容:

这是ls -l 在查看同一目录时返回的内容:

...
drwx------   4 root  wheel     136 Jan 10 11:24 pip-1.5-py2.7.egg
...

好的,所以我显然没有使用pip 的权限,因为我没有存档文件的执行权限。怎么样:

$ chmod 754 pip-1.5-py2.7.egg
chmod: Unable to change file mode on pip-1.5-py2.7.egg/: Operation not permitted
$ sudo chmod 754 pip-1.5-py2.7.egg
$ pip
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5', 'console_scripts', 'pip')()
  File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 378, in load_entry_point
    def get_resource_stream(manager, resource_name):
  File "build/bdist.macosx-10.8-intel/egg/pkg_resources.py", line 2565, in    load_entry_point
    section = line[1:-1].strip()
ImportError: Entry point ('console_scripts', 'pip') not found

我的假设是所有权限都搞砸了,因为我使用了sudo,现在我只是在追逐权限。不过,这可能是错误的。我从来不记得在升级到 OSX 10.9 之前遇到过这个问题。

我的问题是:Unix 系统是否总是使用sudo 执行此操作,即使我是计算机的管理员?有没有办法让我永久纠正这个问题,以便我实际上有权运行已安装的软件包?您认为我的系统中某处的设置不正确吗?

【问题讨论】:

  • 理由273不升级到小牛...
  • @Thanizer,你运行disk utility 并修复权限了吗?这可能是一个很好的起点。 chmod 754 也不会削减它,您需要使用 755 并为此拥有 owner rootgroup wheel
  • @I'L'I,我只是修复了权限无济于事。现在我只是在 Homebrew 中使用 Python 公式,一切似乎都运行良好。不过,我仍然想知道为什么我不能使用 OSX 的原生 easy_install 脚本,但没有权限问题。

标签: python macos bash permissions


【解决方案1】:

您的假设是正确的,因为您使用了sudo,所有权限搞砸了。首先要改掉使用sudo 来“修复”问题的习惯。对于使用sudo 进行操作,您应该像以root 身份登录一样长时间而努力地思考。如果您的态度是“没问题:sudo ...”,那么您(最终)将陷入痛苦的世界。每次您使用 sudo 时,您都会对 OSX 开箱即用的方式进行一次更改。

接下来,改掉尝试将所有内容都放入操作系统版本的 python 的习惯。我建议先清除您的 /usr/local,尽管这可能要求很多。

然后brew install python - 给自己一个pristine python。很好 - 你已经做到了。

which pip - 确保您使用的是刚刚安装在 /usr/local 中的 pip brew;如果不根据需要调整您的~/.bashrc 启动文件。

接下来brew install virtualenvbrew install virtualenvwrapper - 当你pip install 支持一个项目时,这样做只会影响那个项目。当您使用它时,将export PIP_REQUIRE_VIRTUALENV=true 添加到您的~/.bashrc,这样您就不会忘记并在将来搞砸了。

现在了解virtualenv 并开始了解您的项目的真正依赖关系,并停止因不同项目的需求相互干扰而引起的冲突以及不分青红皂白地使用sudo 会导致您的普遍比特腐烂。

【讨论】:

  • 很棒的答案。虽然,你确定 brew install virtualenv 因为这样做会产生错误 - No available formula with the name "virtualenv" . These similarly named formulae were found:pyenv-virtualenv
  • 以上答案基于 2014 年的现实。从那时起,Python 的世界发生了很大的变化。这个What is the relationship between virtualenv and pyenv 可能会有所帮助。
猜你喜欢
  • 2011-01-16
  • 2012-04-12
  • 2012-03-02
  • 2011-11-09
  • 2011-08-19
  • 2011-06-13
  • 2012-01-06
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多