Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 695
djangodude Avatar asked Sep 10 '13 17:09

djangodude


People also ask

How install pip using setup py?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

Does pip install call setup py?

pip is a package manager, which can install, upgrade, list and uninstall packages, like familiar package managers including: dpkg, apt, yum, urpmi, ports etc. Under the hood, it will run python setup.py install , but with specific options to control how and where things end up installed. In summary: use pip .

What is pip install option?

pip is a standard package manager used to install and maintain packages for Python. The Python standard library comes with a collection of built-in functions and built-in packages.


2 Answers

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"

like image 61
Ronen Botzer Avatar answered Sep 28 '22 00:09

Ronen Botzer


For consistency, you can add an option to both setup.py install and setup.py develop (aka pip install -e): (building off Ronen Botzer's answer)

from setuptools import setup from setuptools.command.install import install from setuptools.command.develop import develop   class CommandMixin(object):     user_options = [         ('someopt', None, 'a flag option'),         ('someval=', None, 'an option that takes a value')     ]      def initialize_options(self):         super().initialize_options()         # Initialize options         self.someopt = None         self.someval = 0      def finalize_options(self):         # Validate options         if self.someval < 0:             raise ValueError("Illegal someval!")         super().finalize_options()      def run(self):         # Use options         global someopt         someopt = self.someopt # will be 1 or None          super().run()  class InstallCommand(CommandMixin, install):     user_options = getattr(install, 'user_options', []) + CommandMixin.user_options  class DevelopCommand(CommandMixin, develop):     user_options = getattr(develop, 'user_options', []) + CommandMixin.user_options  setup(     ...,     cmdclass={         'install': InstallCommand,         'develop': DevelopCommand,     } 

Then you can pass options to pip like:

pip install --install-option="--someval=1" --install-option="--someopt" . 

Or in develop mode:

pip install -e --install-option="--someval=1" . 
like image 32
Quantum7 Avatar answered Sep 28 '22 01:09

Quantum7