Pip问题 - 由于环境错误无法安装软件包

12

我认为我的Mac上的Python和/或pip存在一些问题。我安装了全局的Python 2.7,然后通常设置虚拟环境并安装Python3.6.4,但是在过去的一天左右,我遇到了诸如Fabric和SSH2等包的问题,其中我要么无法安装它们,出现各种错误,要么在导入该包时,Fabric会抛出异常。

现在我正在尝试删除Fabric并安装Fabric3,但会出现以下错误:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Users/david/Documents/projects/uptimeapp/env/lib/python3.6/site-packages/Fabric3-1.14.post1.dist-info'
Consider using the `--user` option or check the permissions.

(env) Davids-MacBook-Air:uptimeapp david$ pip install fabric3 --user
Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

如果我执行sudo pip install fabric,那么它会安装,但会出现以下警告:

The directory '/Users/david/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/david/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

但是我认为不建议使用sudo进行pip安装?

当我尝试pip install ssh2-python时,出现了以下错误:

ssh2/agent.c:569:10: fatal error: 'libssh2.h' file not found
    #include "libssh2.h"
             ^~~~~~~~~~~
    1 error generated.
    error: command 'clang' failed with exit status 1

    ----------------------------------------
Command "/Users/david/Documents/projects/uptimeapp/env/bin/python3.6 -u  -c "import setuptools,   tokenize;__file__='/private/var/folders/bl/97vt48j97zd2sj05zmt4xst00000gn/T  /pip-install-mpyq41q4/ssh2-python/setup.py';f=getattr(tokenize, 'open',   open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record   /private/var/folders/bl/97vt48j97zd2sj05zmt4xst00000gn/T/pip-record-qul_k3kq/install-record.txt --single-version-externally-managed --compile -  -install-headers /Users/david/Documents/projects/uptimeapp/env/bin/../include/site/python3.6  /ssh2-python" failed with error code 1 in /private/var/folders/bl/97vt48j97zd2sj05zmt4xst00000gn/T/pip-install-mpyq41q4/ssh2-python/

我已经使用sudo命令成功删除了Fabric并安装了Fabric3,但我宁愿不这样做。

值得一提的是,我在Python2.7中全局安装其他软件包或envs中也没有遇到任何其他问题。

2个回答

10

如果您使用sudo安装,将导致虚拟环境被损坏,从而引发permission denied错误。请运行

$ sudo chown -R david:staff /Users/david/Documents/projects/uptimeapp/env

修复权限。如果您有其他权限问题,甚至可能明智地为整个主目录修复权限:

$ sudo chown -R david:staff /Users/david/

现在重新安装包应该可以正常工作:

$ source /Users/david/Documents/projects/uptimeapp/env/bin/activate
$ (env) pip uninstall -y fabric
$ (env) pip install fabric

'libssh2.h'文件未找到

意味着在安装ssh-python之前,您需要先安装相应的库:

$ brew install libssh2

谢谢,这就是环境权限错误的全部了,但在安装了libssh2之后仍然出现相同的错误,我可能会单独提出一个问题。 - goblin_rocket
嗯,看着 libssh2公式,我发现有一个测试明确检查所需的头文件是否已安装。因此该文件必须在 /usr/Cellar/ 中的某个位置:运行 brew list libssh2 | grep libssh2.h` 来检查。 - hoefling
如果文件存在,则意味着头文件的包含路径在编译阶段中没有传递给 clang,这表明 Homebrew 存在一些问题。也许 brew doctor 命令会提供一些有用的信息。 - hoefling
如果头文件存在,您可以尝试将头文件显式地传递作为解决方法:CPPFLAGS="-I/path/to/libssh2.h/parent/dir" CFLAGS="-I/path/to/libssh2.h/parent/dir" pip install ssh2-python,但是,一个正确的解决方案应该是在您的环境中修复默认的包含路径。 - hoefling

1
你可以让pip在虚拟环境库位置内安装软件包:
sudo -H venv/bin/pip install fabric

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