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

我通常是通过pip安装python软件包的。

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

我试过了:

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

但是它失败了:

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

我怎样才能使它工作?

I’ve been usually installed python packages through pip.

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

I’ve tried:

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

but it fails with:

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

How can I get this to work?


回答 0

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

为我工作。

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

[install]
prefix=

编辑:不要使用此Homebrew建议的选项,它将破坏正常的点操作

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

Worked for me.

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

[install]
prefix=

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


回答 1

我相信有一个更简单的解决方案(在macOS上使用Homebrew的Python),不会破坏您的常规pip操作。

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

[install]
prefix=  

好的,现在您可以为该文件夹运行pip的命令:

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

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

安装完模块后,可以将其卸下 setup.cfg

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

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

[install]
prefix=  

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

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

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

After you are done installing the modules, you may remove setup.cfg.


回答 2

在OSX(mac)上,假定项目文件夹为/ var / myproject

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

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

  1. cd /var/myproject
  2. Create a file called setup.cfg and add [install] prefix=
  3. Run pip install <packagename> -t .

回答 3

针对Homebrew用户的另一种解决方案*仅是使用virtualenv

当然,无论如何,这可能会消除对目标目录的需求-但是即使没有,我发现--target在虚拟环境中时默认情况下也可以工作(例如,无需创建/修改配置文件)。


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

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

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


*I say solution; perhaps it’s just another motivation to meticulously use venvs…


回答 4

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

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

这与并不完全相同--target,但是无论如何我都会成功。

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

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.


回答 5

如其他提到的那样,这是与homebrew一起安装的pip和python的已知错误。

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

在正式解决此错误之前,一种选择是创建自己的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

该脚本包装了您的命令,并:

  1. 接受名称和目标参数
  2. 检查这些参数是否为空
  3. 创建~/.pydistutils.cfg其中带有“空前缀”指令的文件
  4. 使用提供的参数执行您的pip命令
  5. 删除~/.pydistutils.cfg文件

可以更改此脚本并将其改编为满足您的需求,但您有所想法。而且它使您可以在不制动点的情况下运行命令。希望能帮助到你 :)

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

If you create ~/.pydistutils.cfg file with “empty prefix” instruction it will fix this problem but it will break normal pip operations.

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

 #!/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.cfg file with “empty prefix” instruction in it
  4. executes your pip command with provided parameters
  5. removes ~/.pydistutils.cfg file

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


回答 6

如果您使用的是virtualenv *,最好再次检查一下which pip您使用的是哪个。

如果看到类似情况,则说明/usr/local/bin/pip您已脱离环境。重新激活您的virtualenv将解决此问题:

VirtualEnv: $ source bin/activate

虚拟鱼: $ vf activate [environ]

*:我使用virtualfish,但我认为此技巧与两者都有关。

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

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

VirtualEnv: $ source bin/activate

VirtualFish: $ vf activate [environ]

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


回答 7

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

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。