Search code examples
pythonoptimizationpipcompiler-flagspython-packaging

How to set Interpreter flags when packaging


I want to set the -O flag, so that when the package will be build the code will be optimized.

I use the setuptools for building the package and it whould be good to set the optimization flag here!


Solution

  • setup.py build_py has an option optimize:

    $ python setup.py build_py --help | grep -A1 optimize
      --optimize (-O)   also compile with optimization: -O1 for "python -O", -O2
                        for "python -OO", and -O0 to disable [default: -O0]
    

    It also works for setup.py install. For pip pass --install-option=-O1

    But please bear in mind the optimization perhaps doesn't do what you expect it to do and also could make things worse: it removes assert statements and docstrings which is not always what you want. I recommend to avoid this "optimization".