Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

python - How to obtain arguments passed to setup.py from pip with '--install-option'?

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

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.

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

This post from the python-virtualenv Google Group suggests this is possible.

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']

How can I get the values of things passed in as "--install-option" from pip install?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

Register the custom install command with the setup function.

setup(
    cmdclass={
        'install': InstallCommand,
    },
    :

It seems that the order of your arguments is off

pip install /path/to/my/local/package --install-option="--someopt"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...