错误:无法在安装目录中创建或删除文件

1 投票
2 回答
12053 浏览
提问于 2025-04-18 03:23

我正在按照这个页面中Unix(wget)部分的指示来安装Setuptools,链接是https://pypi.python.org/pypi/setuptools

但是每次我运行这个命令时,都会出现以下错误

wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python

错误信息:

--2014-04-19 17:29:52--  https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
Resolving bitbucket.org (bitbucket.org)... 131.103.20.167, 131.103.20.168
Connecting to bitbucket.org (bitbucket.org)|131.103.20.167|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10332 (10K) [text/plain]
Saving to: `STDOUT'

100%[======================================>] 10,332      --.-K/s   in 0s      

2014-04-19 17:29:53 (267 MB/s) - written to stdout [10332/10332]

Extracting in /tmp/tmpDXrlBn
Now working in /tmp/tmpDXrlBn/setuptools-3.4.4
Installing Setuptools
running install
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/test-easy-install-    19015.write-test'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

/usr/local/lib/python2.7/dist-packages/

Perhaps your account does not have write access to this directory?  If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account.  If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.

For information on other options, you may wish to consult the
documentation at:

   https://pythonhosted.org/setuptools/easy_install.html

Please make the appropriate changes for your system and try again.

在寻找解决方案时,我发现使用 sudo easy_install 或者更改PYTHONPATH可能会有效。但我需要一个相对详细的解决方案,因为这两种方法我都没能成功。谢谢!

2 个回答

1

这对任何人来说都应该是个警告。如果你在自己的目录下以自己的用户身份安装东西,而那个目录是可以读、写和执行的,那么这个工具就不应该需要超级用户权限来运行,除非它要在系统目录里安装东西,这样就不好了。

如果你下载了某个东西,结果遇到这种情况,我建议你在运行sudo安装之前,仔细检查一下它的代码。如果你不明白这些内容,那就不要用sudo强行安装。此外,在某些平台上,如果它要安装到系统目录,可能会覆盖或破坏现有的依赖关系,导致你的系统出现问题。MAC在这方面尤其糟糕。

我建议你考虑一些替代方案: 1. 每个用户的站点包 - http://docs.python.org/whatsnew/2.6.html#pep-370-per-user-site-packages-directory 2. 虚拟环境(VirtualENV) - http://pypi.python.org/pypi/virtualenv 允许你克隆系统的Python,这样就不会干扰到它。 3. 查看源代码。如果有一些依赖关系,也许你可以先单独安装它们,然后再重新运行脚本。

3

答案就在错误信息里

/usr/local/lib/python2.7/dist-packages/

Perhaps your account does not have write access to this directory?  If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account.  If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.

如果你在运行你的命令之前先输入 sudo su,那么它应该就能正常工作了。

撰写回答