Force options to python interpretion with commands

2019-07-21 15:23发布

问题:

I want to force a script to be run with python -S, I'm defining the script using entry_points in the setup.py. Is there an option for this?

Thanks!

回答1:

I don't think there is such option in setuptools. You could create a stub script and specify it in the scripts distutils option instead. Bases on Is it possible to set the python -O (optimize) flag within a script?:

#!/usr/bin/env python
from your_package.script import main

if __name__=="__main__":
   import os, sys
   sentinel_option = '--dont-add-no-site-option'
   if sentinel_option not in sys.argv:
      sys.argv.append(sentinel_option)
      os.execl(sys.executable, sys.executable, '-S', *sys.argv)
   else:
      sys.argv.remove(sentinel_option)
      main()