错误:无法创建'/Library/Python/2.7/site-packages/xlrd':权限被拒绝。

41

我正在尝试在Mac 10.8.4上安装xlrd,以便通过Python读取Excel文件。

我已经按照http://www.simplistix.co.uk/presentations/python-excel.pdf上的说明进行了操作。

我做了以下步骤:

  1. 将文件夹解压到桌面上

  2. 在终端中,cd到解压缩的文件夹

  3. $ python setup.py install

这就是我得到的结果:

running install
running build
running build_py
creating build
creating build/lib
creating build/lib/xlrd
copying xlrd/__init__.py -> build/lib/xlrd
copying xlrd/biffh.py -> build/lib/xlrd
copying xlrd/book.py -> build/lib/xlrd
copying xlrd/compdoc.py -> build/lib/xlrd
copying xlrd/formatting.py -> build/lib/xlrd
copying xlrd/formula.py -> build/lib/xlrd
copying xlrd/info.py -> build/lib/xlrd
copying xlrd/licences.py -> build/lib/xlrd
copying xlrd/sheet.py -> build/lib/xlrd
copying xlrd/timemachine.py -> build/lib/xlrd
copying xlrd/xldate.py -> build/lib/xlrd
copying xlrd/xlsx.py -> build/lib/xlrd
creating build/lib/xlrd/doc
copying xlrd/doc/compdoc.html -> build/lib/xlrd/doc
copying xlrd/doc/xlrd.html -> build/lib/xlrd/doc
creating build/lib/xlrd/examples
copying xlrd/examples/namesdemo.xls -> build/lib/xlrd/examples
copying xlrd/examples/xlrdnameAPIdemo.py -> build/lib/xlrd/examples
running build_scripts
creating build/scripts-2.7
copying and adjusting scripts/runxlrd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/runxlrd.py from 644 to 755
running install_lib
creating /Library/Python/2.7/site-packages/xlrd
error: could not create '/Library/Python/2.7/site-packages/xlrd': Permission denied

为什么会出现权限被拒绝的情况?谢谢。

3个回答

155

尝试使用命令 python setup.py install --user 进行安装。

不建议按上面所述使用 sudo,原因如下:

  1. 您会允许从互联网下载的任意不受信任的代码以root身份运行。
  2. 在执行 sudo pipsudo python setup.py 安装时,通过添加 --user 标志可以将软件包安装到属于用户的目录中。普通非root用户将无法访问这些文件。

如果我已经使用root安装了包,那么我只需要使用chown -R username /Library/Python/2.7/site-packages更改权限吗?还是您有其他建议? - Chris
1
这不是我需要处理的问题,而且我也不想让你完全相信我的话 - 最好提出一个新问题,看看其他人有什么说法 :-) 当你这样做时,或许可以在这里留下你的问题链接。 - Louis Maddox

6

尝试运行sudo python setup.py install

/Library文件夹需要管理员权限才能访问。


74
不要使用sudo!请使用--user标志。 - Louis Maddox
2
在我的情况下(OpenELEC),没有sudo,所以--user标志是做到这一点的关键! - s1m3n

4

virtualenv 中尝试:

  • sudo pip install virtualenvwrapper
  • mkvirtualenv
  • workon
  • python setup.py install

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接