将 --user 与 --prefix 错误与 setup.py install 结合起来

发布于 2024-10-08 09:48:58 字数 836 浏览 5 评论 0原文

我试图在我最近访问的系统上安装 Python 软件包。我试图利用Python相对较新的 每用户站点包目录,以及新选项--user。 (该选项当前未记录,但它存在于 Python 2.6+ 中;您可以通过运行 < code>python setup.py install --help。)

当我尝试

python setup.py install --user

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

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

该错误非常令人困惑,因为如您所见,我没有提供--prefix--exec-prefix--install-base--install-platbase > 标志作为命令行选项。我浪费了很多时间试图找出问题所在。我将我的答案记录在下面,希望能让其他可怜的人花几个小时牦牛剃毛

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.)

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-platbase flags 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深空失忆 2024-10-15 09:48:58

一次性解决方法:

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

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

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

不要忘记--user 标志。

安装多个软件包:

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

[install]
prefix=

请注意,=< 后面没有文本(甚至没有空格) /代码>。

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

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

此问题的原因

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

该错误源于系统级 distutils 配置文件(在我的情况下 /usr/lib64/python2.6/distutils/distutils.cfg) 基本上

[install]
prefix=/usr/local

,这相当于始终运行安装命令 install --prefix=/usr/local。您必须使用上述技术之一来覆盖此规范。

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 not forget the --user flag.

Installing multiple packages:

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

[install]
prefix=

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

Then run the necessary pip install --user or python setup.py install --user commands. Do not forget the --user flag.

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.

The cause of this issue

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

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

[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.

柳若烟 2024-10-15 09:48:58

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

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

代码:

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

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

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

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.

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 PYTHONUSERBASE environment variable to a temp location. --force-reinstall is used to trigger the local installation even when the package is already installed.

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

淡莣 2024-10-15 09:48:58

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

@rogeleaderr 说:“请注意,像这样保留此文件将使 Python 认为 / 是您的 root python 库目录,如果您尝试安装其他新包,则会导致混乱的问题。”

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

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

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

@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."

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

$ python setup.py install --user --prefix=
沉溺在你眼里的海 2024-10-15 09:48:58

您只需运行 pip install --user . 即可,无需前缀参数。

无论如何,这更好,因为如果你的 pip 配置为使用 Python 3,它将默认为 python3。
(我忘记输入python3 setup.py,它在 2.7 下安装了一个仅包含 3 个版本的软件包)

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

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

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.py and it installed a 3-only package under 2.7)

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

无敌元气妹 2024-10-15 09:48:58

我也有同样的问题。它隐藏在 ~/.config/pip/pip.conf 中:

[global]
target=/foo/bar

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

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

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

[global]
target=/foo/bar

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

I suggest checking the pip configuration files and removing the target=/foo/bar options.

披肩女神 2024-10-15 09:48:58

以下内容对我不起作用:

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

但是,当与 sudo 一起使用时,它可以工作:

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

感谢 gotgenes 答案

The following didn't work for me:

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

However, when using it with sudo, it works:

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

Thanks to gotgenes for the answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文