如何使用'--install-option'从pip获取传递给setup.py的参数? [英] How to obtain arguments passed to setup.py from pip with '--install-option'?
问题描述
我正在使用pip 1.4.1,尝试从本地路径安装软件包,例如:
I am using pip 1.4.1, attempting to install a package from a local path, for example:
pip install /path/to/my/local/package
这可以满足我的要求,与运行python /path/to/my/local/package/setup.py install
差不多,但是我想将一些其他选项/参数传递给程序包的setup.py install.
This does what I want, which is more or less the equivalent of running python /path/to/my/local/package/setup.py install
, but I would like to pass some additional options/arguments to my package's setup.py install.
我从pip文档中了解 使用--install-option
选项可以做到这一点,例如:
I understand from the pip documentation that this is possible with the --install-option
option, for example:
pip install --install-option="--some-option" /path/to/my/local/package
python-virtualenv Google小组建议的
这篇文章这是可能的.
This post from the python-virtualenv Google Group suggests this is possible.
我不了解的是如何从setup.py中获取传入的"--some-option".我尝试查看sys.argv
,但是无论我为"--install-option =加上什么,sys.argv
总是这样:
What I do not understand is how to obtain the passed-in "--some-option" from within setup.py. I tried looking at sys.argv
, but no matter what I put for "--install-option=", sys.argv
is always this:
['-c', 'egg_info', '--egg-base', 'pip-egg-info']
如何从pip install中获取以"--install-option"形式传递的事物的值?
How can I get the values of things passed in as "--install-option" from pip install?
推荐答案
您需要使用自己的自定义命令来扩展install命令.在run
方法中,您可以将选项的值公开给setup.py
(在我的示例中,我使用全局变量).
You need to extend the install command with a custom command of your own. In the run
method you can expose the value of the option to setup.py
(in my example I use a global variable).
from setuptools.command.install import install
class InstallCommand(install):
user_options = install.user_options + [
('someopt', None, None), # a 'flag' option
#('someval=', None, None) # an option that takes a value
]
def initialize_options(self):
install.initialize_options(self)
self.someopt = None
#self.someval = None
def finalize_options(self):
#print("value of someopt is", self.someopt)
install.finalize_options(self)
def run(self):
global someopt
someopt = self.someopt # will be 1 or None
install.run(self)
使用setup
功能注册自定义安装命令.
Register the custom install command with the setup
function.
setup(
cmdclass={
'install': InstallCommand,
},
:
似乎您的论点顺序不对
pip install /path/to/my/local/package --install-option="--someopt"
这篇关于如何使用'--install-option'从pip获取传递给setup.py的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!