Passing options to Python executable in non-interactive mode

后端 未结 4 882
故里飘歌
故里飘歌 2021-01-21 04:09

I would like to pass some options to Python (version 2.6) every time, not just in interactive mode. Is there a file I can put such commands in?

EDIT: Specifically, I\'m

4条回答
  •  粉色の甜心
    2021-01-21 04:48

    The #!/usr/bin/python line at the beginning of a Python script under Linux can be used to also pass options to the interpreter.

    There are also a number of modules imported whenever Python starts up. On my system, a likely candidate for modification to set options in the manner suggested by other posters are here:

    /usr/lib/python2.6/site-packages/sitecustomize.py
    

    If you simply put this code in that file:

    import warnings
    warnings.simplefilter("ignore", DeprecationWarning)
    

    it will turn off deprecation warnings for everything always, which may not be what you want. You could instead put in code that would check your own PYTHONNODEPRECATIONWARNING environment variable so you had more control.

    After finding a reference to sitecustomize.py in Dive Into Python and this reference to the sitecustomize module in the Python 2.6 documentation, I think that file is your best bet for what you want. In Python 2.6, with its user specific site-packages directory it's possible to set this up on a per-user basis, though you may want to find any system-wide sitecustomize.py file and either copy it into yours or find a way to explicitly import it in yours.

提交回复
热议问题