将–user和–prefix错误与setup.py install结合使用

问题:将–user和–prefix错误与setup.py install结合使用

我试图将Python软件包安装到最近可以访问的系统中。我试图利用Python相对较新的每用户site-packages目录以及new选项--user。(该选项当前未公开,但是适用于Python 2.6+;您可以通过运行来查看帮助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.


回答 0

一种解决方法:

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标志。

最后,删除或重命名此文件。如果--user以此用户身份在系统范围内安装Python软件包(即,不带),则将该文件保留为当前内容将导致问题~/.pydistutils.cfg

此问题的原因

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

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

[install]
prefix=/usr/local

基本上,这等效于始终以方式运行install命令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.


回答 1

正如评论中所指出的那样,公认的答案(@gotgenes,大概拥有基因)可能导致意外的后果。

@rogeleaderr说:“请注意,将文件保存为这样会使Python认为/是您的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=

回答 2

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

在某些环境中,使用--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/*


回答 3

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

这是更好的呢,因为如果你的点子被配置为使用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)


回答 4

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

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