Python 将 --user 与 --prefix 错误与 setup.py install 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4495120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:00:41  来源:igfitidea点击:

Combine --user with --prefix error with setup.py install

pythoninstallationdistutils

提问by gotgenes

I was trying to install Python packages a system I recently gained access to. I was trying to take advantage of Python's relatively new per user site-packages directory, and the new option --user. (The option is currently undocumented, however it exists for Python 2.6+; you can see the help by running python setup.py install --help.)

我试图在我最近获得访问权限的系统上安装 Python 包。我试图利用 Python 相对较新的每个用户 site-packages directory和新选项--user. (该选项目前未记录,但它存在于 Python 2.6+ 中;您可以通过运行查看帮助python setup.py install --help。)

When I tried running

当我尝试跑步时

python setup.py install --user

on any package I downloaded, I always got the following error:

在我下载的任何包上,我总是收到以下错误:

error: can't combine user with with prefix/exec_prefix/home or install_(plat)base

The error was extremely perplexing because, as you can see, I wasn't providing the --prefix, --exec-prefix, --install-base, or --install-platbaseflags as command line options. I wasted a lot of time trying to figure out what the problem was. I document my answer below, in hopes to spare some other poor soul a few hours of yak shaving.

错误是非常令人费解,因为你可以看到,我不提供--prefix--exec-prefix--install-base,或--install-platbase标志作为命令行选项。我浪费了很多时间试图找出问题所在。我在下面记录了我的答案,希望能让其他可怜的人少花几个小时的牦牛剃毛

采纳答案by gotgenes

One time workaround:

一次性解决方法:

pip install --user --install-option="--prefix=" <package_name>

or

或者

python setup.py install --user --prefix=

Note that there is no text (not even whitespace) after the =.

请注意, . 之后没有文本(甚至没有空格)=

Do notforget the --userflag.

千万不能忘记的--user标志。

Installing multiple packages:

安装多个包:

Create ~/.pydistutils.cfg(or equivalent for your OS/platform) with the following contents:

~/.pydistutils.cfg使用以下内容创建(或等效于您的操作系统/平台):

[install]
prefix=

Note that there is no text (not even whitespace) after the =.

请注意, . 之后没有文本(甚至没有空格)=

Then run the necessary pip install --useror python setup.py install --usercommands. Do notforget the --userflag.

然后运行必要的pip install --userpython setup.py install --user命令。千万不能忘记的--user标志。

Finally, remove or rename this file. Leaving this file present will cause issues when installing Python packages system-wide (i.e., without --user) as this user with this ~/.pydistutils.cfg.

最后,删除或重命名此文件。保留此文件会导致在系统范围内(即,没有--user)以该用户身份安装 Python 包时出现问题~/.pydistutils.cfg

The cause of this issue

这个问题的原因

This appears to be an issue with both OpenSUSE and RedHat, which has lead to a bug in virtualenvon these platforms.

这似乎是 OpenSUSE 和 RedHat 的问题,导致这些平台上的virtualenv 出现错误

The error stems from a system-level distutils configuration file(in my case /usr/lib64/python2.6/distutils/distutils.cfg) where there was this

该错误源于系统级distutils 配置文件(在我的情况下/usr/lib64/python2.6/distutils/distutils.cfg),其中有这个

[install]
prefix=/usr/local

Basically, this is equivalent to always running the install command as install --prefix=/usr/local. You have to override this specification using one of the techniques above.

基本上,这相当于始终将安装命令作为install --prefix=/usr/local. 您必须使用上述技术之一覆盖此规范。

回答by dbliss

As has been noted in the comments, the accepted answer (by @gotgenes, who, presumably, has genes) can lead to unexpected consequences.

正如评论中所指出的,接受的答案(@gotgenes,大概有基因)可能会导致意想不到的后果。

@rogeleaderr says, "Note that keeping this file like this will make Python think that / is your root python library directory, leading to confusing issues if you try to install other new packages."

@rogeleaderr 说,“请注意,保持这样的文件会使 Python 认为 / 是您的根 Python 库目录,如果您尝试安装其他新软件包,则会导致混淆问题。”

Rather than write a new config file, as @gotgenes recommends, a better option is to add --prefix=(with no text to the right of the equals sign) as an option on the command line, as in

与其像@gotgenes 建议的那样编写一个新的配置文件,更好的选择是--prefix=命令行上添加(等号右侧没有文本)作为一个选项,如

$ python setup.py install --user --prefix=

回答by EE1213

Posting to save others time, as no available answers worked for me...

发布以节省其他人的时间,因为没有可用的答案对我有用...

In some environments, using the --target(-t) switch will still hit the same error. In my testing on two flavors of linux, I encountered the same issue when using the --prefix=parameter.

在某些环境中,使用--target( -t) 开关仍会遇到相同的错误。在我对两种 linux 版本的测试中,我在使用该--prefix=参数时遇到了同样的问题。

Code:

代码:

PYTHONUSERBASE=/tmp/ pip install --user --force-reinstall $PACKAGE

Explanation: My workaround, which seems to work across many environments (MacOS, Amazon Linux, Debian) is to set the PYTHONUSERBASEenvironment variable to a temp location. --force-reinstallis used to trigger the local installation even when the package is already installed.

说明:我的解决方法(似乎适用于许多环境(MacOS、Amazon Linux、Debian))是将PYTHONUSERBASE环境变量设置为临时位置。 --force-reinstall即使包已经安装,也用于触发本地安装。

This will result in the module being compiled/installed (depending on the OS and Python version) to: /tmp/lib/python2.7/site-packages/*

这将导致模块被编译/安装(取决于操作系统和 Python 版本): /tmp/lib/python2.7/site-packages/*

回答by wesinat0r

You can simply run pip install --user ., no prefix args required.

您可以简单地运行pip install --user .,不需要前缀参数。

This is better anyway because it will default to python3 if your pip is configured to use Python 3. (I forgot to enter python3 setup.pyand it installed a 3-only package under 2.7)

无论如何,这更好,因为如果您的 pip 配置为使用 Python 3,它将默认为 python3。(我忘了输入python3 setup.py,它安装了 2.7 下的 3-only 包)

(credit https://stackoverflow.com/a/1550235/4364036)

(信用https://stackoverflow.com/a/1550235/4364036

回答by sir__finley

I had have the same problem. It was hidden inside the ~/.config/pip/pip.confwith:

我有同样的问题。它隐藏在里面~/.config/pip/pip.conf

[global]
target=/foo/bar

Such a config was created by a third-party script without my knowledge.

这样的配置是在我不知情的情况下由第三方脚本创建的。

I suggestchecking the pip configuration files and removing the target=/foo/baroptions.

我建议检查 pip 配置文件并删除target=/foo/bar选项。