Python DistutilsOptionError: 必须提供 home 或 prefix/exec-prefix —— 不能同时提供

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24257803/
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-19 04:20:24  来源:igfitidea点击:

DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both

pythongoogle-app-engineinstallpipdistutils

提问by Arkind

I've been usually installed python packages through pip.

我通常通过 pip 安装 python 包。

For Google App Engine, I need to install packages to another target directory.

对于 Google App Engine,我需要将包安装到另一个目标目录。

I've tried:

我试过了:

pip install -I flask-restful --target ./lib

pip install -I flask-restful --target ./lib

but it fails with:

但它失败了:

must supply either home or prefix/exec-prefix -- not both

必须提供 home 或 prefix/exec-prefix —— 不能同时提供

How can I get this to work?

我怎样才能让它发挥作用?

采纳答案by ayvazj

Are you using OS X and Homebrew? The Homebrew python page https://github.com/Homebrew/brew/blob/master/docs/Homebrew-and-Python.mdcalls out a known issue with pip and a work around.

您使用的是 OS X 和 Homebrew 吗?Homebrew python 页面https://github.com/Homebrew/brew/blob/master/docs/Homebrew-and-Python.md指出了 pip 的已知问题和解决方法。

Worked for me.

为我工作。

You can make this "empty prefix" the default by adding a ~/.pydistutils.cfg file with the following contents:

[install]
prefix=

您可以通过添加具有以下内容的 ~/.pydistutils.cfg 文件来将此“空前缀”设为默认值:

[install]
prefix=

Edit:Do not use this Homebrew recommended option, it will break normal pip operations.

编辑:不要使用这个 Homebrew 推荐的选项,它会破坏正常的 pip 操作

回答by OJFord

Another solution* for Homebrew users is simply to use a virtualenv.

Homebrew 用户的另一个解决方案* 就是简单地使用virtualenv.

Of course, that mayremove the need for the target directory anyway - but even if it doesn't, I've found --targetworks by default (as in, without creating/modifying a config file) when in a virtual environment.

当然,无论如何,这可能会消除对目标目录的需要 - 但即使不需要,我也发现--target在虚拟环境中默认有效(例如,无需创建/修改配置文件)。



*I say solution; perhaps it's just another motivation to meticulously use venvs...

*我说解决方案;也许这只是精心使用venvs的另一个动机......

回答by Imran Rashid

I hit errors with the other recommendations around --install-option="--prefix=lib". The only thing I found that worked is using PYTHONUSERBASEas described here.

我遇到了其他建议的错误--install-option="--prefix=lib"。我发现唯一有效的方法是PYTHONUSERBASE按照此处所述使用。

export PYTHONUSERBASE=lib
pip install -I flask-restful --user

this is not exactly the same as --target, but it does the trick for me in any case.

这与 不完全相同--target,但无论如何它对我有用。

回答by vs.

As other mentioned, this is known bug with pip & python installed with homebrew.

正如其他人提到的,这是与自制软件一起安装的 pip & python 的已知错误。

If you create ~/.pydistutils.cfgfile with "empty prefix" instruction it will fix this problem but it will break normal pip operations.

如果您~/.pydistutils.cfg使用“空前缀”指令创建文件,它将解决此问题,但会破坏正常的 pip 操作。

Until this bug is officially addressed, one of the options would be to create your own bash script that would handle this case:

在正式解决此错误之前,其中一种选择是创建您自己的 bash 脚本来处理这种情况:

 #!/bin/bash

 name=''
 target=''

 while getopts 'n:t:' flag; do
     case "${flag}" in
         n) name="${OPTARG}" ;;
         t) target="${OPTARG}" ;;
     esac
 done

 if [ -z "$target" ];
 then
     echo "Target parameter must be provided"
     exit 1
 fi

 if [ -z "$name" ];
 then
     echo "Name parameter must be provided"
     exit 1
 fi

 # current workaround for homebrew bug
 file=$HOME'/.pydistutils.cfg'
 touch $file

 /bin/cat <<EOM >$file
 [install]
 prefix=
 EOM
 # end of current workaround for homebrew bug

 pip install -I $name --target $target

 # current workaround for homebrew bug
 rm -rf $file
 # end of current workaround for homebrew bug

This script wraps your command and:

此脚本包装您的命令和:

  1. accepts name and target parameters
  2. checks if those parameters are empty
  3. creates ~/.pydistutils.cfgfile with "empty prefix" instruction in it
  4. executes your pip command with provided parameters
  5. removes ~/.pydistutils.cfgfile
  1. 接受名称和目标参数
  2. 检查这些参数是否为空
  3. 创建~/.pydistutils.cfg带有“空前缀”指令的文件
  4. 使用提供的参数执行您的 pip 命令
  5. 删除~/.pydistutils.cfg文件

This script can be changed and adapted to address your needs but you get idea. And it allows you to run your command without braking pip. Hope it helps :)

可以更改和调整此脚本以满足您的需求,但您会有所了解。它允许您在不制动 pip 的情况下运行您的命令。希望能帮助到你 :)

回答by AndreG

I believe there is a simpler solution to this problem (Homebrew's Python on macOS) that won't break your normal pip operations.

我相信这个问题有一个更简单的解决方案(macOS 上的 Homebrew 的 Python),它不会破坏您的正常 pip 操作。

All you have to do is to create a setup.cfgfile at the root directory of your project, usually where your main __init__.pyor executable py file is. So if the root folder of your project is: /path/to/my/project/, create a setup.cfgfile in there and put the magic words inside:

您所要做的就是setup.cfg在项目的根目录下创建一个文件,通常是您的主__init__.py文件或可执行 py 文件所在的位置。因此,如果您的项目的根文件夹是:/path/to/my/project/,请setup.cfg在其中创建一个文件并将魔术字放入其中:

[install]
prefix=  

OK, now you sould be able to run pip's commands for that folder:

好的,现在您应该能够为该文件夹运行 pip 命令:

pip install package -t /path/to/my/project/  

This command will run gracefully for that folder only. Just copy setup.cfgto whatever other projects you might have. No need to write a .pydistutils.cfgon your home directory.

此命令将仅针对该文件夹正常运行。只需复制setup.cfg到您可能拥有的任何其他项目。无需.pydistutils.cfg在您的主目录上写一个。

After you are done installing the modules, you may removesetup.cfg.

完成安装模块后,您可以删除setup.cfg.

回答by pipelog

I have a similar issue. I use the --systemflag to avoid the error as I decribe hereon other thread where I explain the specific case of my situation. I post this here expecting that can help anyone facing the same problem.

我有一个类似的问题。我用--system标志,以避免错误,因为我粗糙地在这里对其他线程在这里我解释一下我的情况的具体情况。我在这里张贴这个希望可以帮助任何面临同样问题的人。

回答by Jerome Anthony

On OSX(mac), assuming a project folder called /var/myproject

在 OSX(mac) 上,假设有一个名为 /var/myproject 的项目文件夹

  1. cd /var/myproject
  2. Create a file called setup.cfgand add [install] prefix=
  3. Run pip install <packagename> -t .
  1. cd /var/myproject
  2. 创建一个名为的文件setup.cfg并添加 [install] prefix=
  3. pip install <packagename> -t .

回答by Graham P Heath

If you're using virtualenv*, it might be a good idea to double check which pipyou're using.

如果您使用的是 virtualenv*,最好仔细检查一下which pip您正在使用的情况。

If you see something like /usr/local/bin/pipyou've broken out of your environment. Reactivating your virtualenv will fix this:

如果你看到类似/usr/local/bin/pip你已经脱离环境的东西。重新激活您的 virtualenv 将解决此问题:

VirtualEnv: $ source bin/activate

虚拟环境: $ source bin/activate

VirtualFish: $ vf activate [environ]

虚拟鱼: $ vf activate [environ]

*: I use virtualfish, but I assume this tip is relevant to both.

*:我使用 virtualfish,但我认为这个技巧与两者都相关。