tags:

views:

115

answers:

4

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 wanting to silence the Deprecation warnings.

A: 

Have you tried ~/.pythonrc.py? It is listed in the Python man page with the following description:

User-specific initialization file loaded by the user module; not used by default or by most applications.

I'm not sure what it means by the 'user' module but I think it's worth a shot to try this.

EDIT: It looks like you have to import user in your python scripts and then Python will automatically execute this ~/.pythonrc.py file upon startup (and not just in interactive mode). This should work in Python version < 3.0. http://docs.python.org/library/user.html

Roman Stolper
+2  A: 

Most of the options can be passed in as environment variables -- do python -h to see the list:

$ py26 -h|grep PYTH
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
-v     : verbose (trace import statements); also PYTHONVERBOSE=x
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH   : ':'-separated list of directories prefixed to the
PYTHONHOME   : alternate <prefix> directory (or <prefix>:<exec_prefix>).
PYTHONCASEOK : ignore case in 'import' statements (Windows).
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.

Are you concerned with other flags that can't be set via environment variables?

PS the PYTHONINSPECT=x is the equivalent of -i (the grep cut that info because it comes on the immediately previous line;-).

Alex Martelli
I am, in fact concerned with other variables than those. I've edited my question.
chiggsy
+2  A: 

I'm sorry, I don't quite understand exactly what your question is, but, you might try:

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

So, this might not really answer the question.

benno
Thank you. I didn't know about this, and this helped me give a better answer to the OPs problem.
Omnifarious
+4  A: 

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.

Omnifarious
Brilliant, and thank you very much. I think this is how I'll roll , and I'll turn it on for a ( dare I say it) "use strict;" effect
chiggsy
Glad you liked the answer. :-) I'm sort of amused that people still want to mod Mr. 63.9k up even though his answer clearly doesn't answer your question. He must have quite the fan club. :-)
Omnifarious