2

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!

Jorge E. Cardona
  • 92,161
  • 3
  • 37
  • 44

1 Answers1

2

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()
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • It seems to be a good workaround, I will check if effectively there is no such option to mark it as an answer. – Jorge E. Cardona Oct 19 '12 at 01:47
  • I just found that using "scripts" instead of entry_points setuptools will mantain the shebang, then and I can put a #!/usr/bin/python -S in the first line which will be on the final script too. – Jorge E. Cardona Oct 20 '12 at 14:44
  • @JorgeEduardoCardona: shebang is a good option but it doesn't work if you invoke the script with explicit interpreter `python path/to/script`, it doesn't allow `-S` with `/usr/bin/env python`, and it won't work on Windows unless pylauncher is installed (it might support options in shebang) – jfs Oct 20 '12 at 16:29
  • Not only that, I just realized that If I have the -S then the code generated by setuptools wont work since it use pkg_resources which is not in the standard library. I was following the code of setuptools in particular the easy_install.py code at line 1800 where the function get_script_args is defined but I think there is no way to do it nicely. – Jorge E. Cardona Oct 20 '12 at 18:14