distutils:如何将用户定义的参数传递给 setup.py?
如何将用户定义的参数从命令行和 setup.cfg 配置文件传递到 distutils 的 setup.py 脚本?
我想编写一个 setup.py 脚本,它接受我的包特定参数。 例如:
python setup.py install -foo myfoo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
由于 Setuptools/Distuils 的文档非常多,我自己找不到答案。 但最终我偶然发现了这个示例。 另外,这个类似的问题很有帮助。 基本上,带有选项的自定义命令如下所示:
As Setuptools/Distuils are horribly documented, I had problems finding the answer to this myself. But eventually I stumbled across this example. Also, this similar question was helpful. Basically, a custom command with an option would look like:
这是一个非常简单的解决方案,您所要做的就是在调用 distutils
setup(..)
之前过滤掉 sys.argv 并自行处理它。像这样的事情:
关于如何使用 distutils 执行此操作的文档非常糟糕,最终我遇到了这个:搭便车指南打包,它使用
sdist
及其user_options
。我发现 扩展 distutils 参考并不是特别有用。
尽管这看起来像是使用 distutils 执行此操作的“正确”方法(至少是我能找到的唯一一种模糊记录的方法)。 我在其他答案中提到的
--with
和--without
开关上找不到任何内容。这个 distutils 解决方案的问题是它对于我正在寻找的东西来说太复杂了(对于你来说也可能是这种情况)。
添加几十行并子类化
sdist
对我来说是错误的。Here is a very simple solution, all you have to do is filter out
sys.argv
and handle it yourself before you call to distutilssetup(..)
.Something like this:
The documentation on how to do this with distutils is terrible, eventually I came across this one: the hitchhikers guide to packaging, which uses
sdist
and itsuser_options
.I find the extending distutils reference not particularly helpful.
Although this looks like the "proper" way of doing it with distutils (at least the only one that I could find that is vaguely documented). I could not find anything on
--with
and--without
switches mentioned in the other answer.The problem with this distutils solution is that it is just way too involved for what I am looking for (which may also be the case for you).
Adding dozens of lines and subclassing
sdist
is just wrong for me.是的,现在已经是 2015 年了,在
setuptools
和distutils
中添加命令和选项的文档仍然很大程度上缺失。经过几个令人沮丧的小时后,我找到了以下代码,用于向
setup.py
的install
命令添加自定义选项:值得一提的是 install.run() 检查如果它被称为“本机”或已修补:
此时,您可以使用
setup
注册您的命令:Yes, it's 2015 and the documentation for adding commands and options in both
setuptools
anddistutils
is still largely missing.After a few frustrating hours I figured out the following code for adding a custom option to the
install
command ofsetup.py
:It's worth to mention that install.run() checks if it's called "natively" or had been patched:
At this point you register your command with
setup
:您无法真正将自定义参数传递给脚本。 但是,以下情况是可能的,并且可以解决您的问题:
--with-featurename
启用可选功能,可以使用--without-featurename
禁用标准功能。 [AFAIR 这需要 setuptools]设置
,而前缀它们可以在 linux/ OS X 上使用(FOO=bar python setup.py
>)。python setup.py foo install
) 将在执行install
之前执行foo
命令。希望能有所帮助。 一般来说,我建议提供更多信息,您的额外参数到底应该做什么,也许有更好的解决方案可用。
You can't really pass custom parameters to the script. However the following things are possible and could solve your problem:
--with-featurename
, standard features can be disabled using--without-featurename
. [AFAIR this requires setuptools]set
on windows whereas prefixing them works on linux/ OS X (FOO=bar python setup.py
).cmd_class
es which can implement new features. They are also chainable, so you can use that to change variables in your script. (python setup.py foo install
) will execute thefoo
command before it executesinstall
.Hope that helps somehow. Generally speaking I would suggest providing a bit more information what exactly your extra parameter should do, maybe there is a better solution available.
我成功地使用了一种解决方法来使用类似于 totaam 的建议的解决方案。 我最终从 sys.argv 列表中弹出了额外的参数:
可以添加一些额外的验证以确保输入良好,但这就是我的做法
I successfully used a workaround to use a solution similar to totaam's suggestion. I ended up popping my extra arguments from the sys.argv list:
Some extra validation could be added to ensure the inputs are good, but this is how I did it
与 totaam 给出的类似的快速简单的方法是使用 argparse 来获取 -foo 参数,并将其余参数留给 distutils.setup() 的调用。 恕我直言,为此使用 argparse 比手动迭代 sys.argv 更好。 例如,在 setup.py 的开头添加以下内容:
add_help=False
参数意味着您仍然可以使用-h
获取常规 setup.py 帮助(前提是给出了--foo
)。A quick and easy way similar to that given by totaam would be to use argparse to grab the -foo argument and leave the remaining arguments for the call to distutils.setup(). Using argparse for this would be better than iterating through sys.argv manually imho. For instance, add this at the beginning of your setup.py:
The
add_help=False
argument means that you can still get the regular setup.py help using-h
(provided--foo
is given).也许你和我一样是一个没有经验的程序员,在阅读了上面的所有答案后仍然在挣扎。 因此,您可能会发现另一个可能有用的示例(并解决先前答案中有关输入命令行参数的注释):
上面的内容经过测试并来自我编写的一些代码。 我还包含了稍微更详细的文档字符串,以使事情更容易理解。
至于命令行:
python setup.py runClient --socket=127.0.0.1:7777
。 使用 print 语句进行快速双重检查表明 run 方法确实选择了正确的参数。我发现有用的其他资源(更多和更多示例):
自定义 distutils 命令
https://seasonofcode.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy.html
Perhaps you are an unseasoned programmer like me that still struggled after reading all the answers above. Thus, you might find another example potentially helpful (and to address the comments in previous answers about entering the command line arguments):
The above is tested and from some code I wrote. I have also included slightly more detailed docstrings to make things easier to understand.
As for the command line:
python setup.py runClient --socket=127.0.0.1:7777
. A quick double check using print statements shows that indeed the correct argument is picked up by the run method.Other resources I found useful (more and more examples):
Custom distutils commands
https://seasonofcode.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy.html
为了与
python setup.py install
和pip install 完全兼容。
,您需要使用环境变量,因为pip
选项-- install-option=
被 bug:--install-option
跨行泄漏这是一个未使用的完整示例
--install-option
:然后,您可以在 Linux 上像这样运行它:
但是,如果您在 Windows 上,则像这样运行它:
参考资料:
To be fully compatible with both
python setup.py install
andpip install .
you need to use environment variables becausepip
option--install-option=
is bugged:--install-option
leaks across linesThis is a full example not using the
--install-option
:Then, you can run it like this on Linux:
But, if you are on Windows, run it like this:
References: