I just tested this and it works fine on 10.8.2 with Apple Python 2.7 installed.
$ echo -e 'import bar\n' > foo.py $ echo -e 'pass\n' > bar.py $ export PYTHONDONTWRITEBYTECODE=1 $ python foo.py $ ls -l bar.py* -rw-r--r-- 1 abarnert staff 6 Dec 14 17:25 bar.py $ unset PYTHONDONTWRITEBYTECODE $ python foo.py $ ls -l bar.py* -rw-r--r-- 1 abarnert staff 6 Dec 14 17:25 bar.py -rw-r--r-- 1 abarnert staff 118 Dec 14 17:26 bar.pyc
You can replace python with python2.7 , python2.6 or python2.5 and you will get the same result.
And it also works with all installed Apple Pythons from 10.5-10.7 and with all python.org, Enthought, MacPorts and Homebrew Pythons, which I have on different machines that are available to me, and on standard python packages on two different Linux distributions.
So, if this is a mistake, it is very specific, that is, you will need to say exactly which Python (and how you installed it, if you did not stock it) and which OS X 10.8.
Most likely PYTHONDONTWRITEBYTECODE just not in python environment. As suggested by Mark Ransom, you can verify this by adding:
import os print 'PYTHONDONTWRITEBYTECODE is', os.environ.get('PYTHONDONTWRITEBYTECODE', None)
If he says None , this is a problem.
So PYTHONDONTWRITEBYTECODE not in your environment. But you say that it is in your calling environment. How is this possible? Well, if you use bash , then most likely you forgot to export . Of all the stupid ways to do this, that I did the most. But here are some stupid things I did:
- Forget
export . You can echo $PYTHONDONTWRITEBYTECODE you want, and it is clearly installed, and yet python does not see it ... - Remember
export , but forget to install it. bash allows you to export PYTHONDONTWRITEBYTECODE , even if it is undefined. export $PYTHONDONTWRITEBYTECODE . (You can avoid this by setting it to something that is not a valid identifier, for example 1 )- Remember to
export , but export wrong variable (usually by recklessly misusing the story in an attempt to save a few keystrokes). - typo. No matter how many times you look at
PYTHONDOTWRITEBYTECODE , it looks good ... - Make some settings in the terminal, switch to another window, return to the wrong terminal window and run
python . - Change
~/.bash_profile , and instead of reloading or starting a new shell, the number “I'll just do the same right” and make some kind of silly typo and then spend half an hour on my .bash_profile to see that I was mistaken before than thinking about how to look at my shell story. - Run one Python script from another using
subprocess / os.exec* / whatever, going out of my way to ask for a clean environment, and then wonder why my environment variable does not appear. - Run Python with C using
execl instead of execle , so your intended envp argument envp not affected. (On some platforms, this may be segfault, but not x86 / x64 OS X.)
Anyway, if you tried it manually in the terminal, try again.
If you do this in your .bash_profile , script startup shell, Python script launcher, or something else, show us the appropriate code and someone will immediately find your dumb error. If they laugh at you, tell them to read my answer so that they can laugh at me.
PS, Please note that I did export PYTHONDONTWRITEBYTECODE=1 instead of the individual lines PYTHONDONTWRITEBYTECODE=1 and export PYTHONDONTWRITEBYTECODE in my test. I always do this, with the exception of shell scripts, which should be portable, because it’s easier to debug the blatant error that occurs when trying to use it on an old school shell that does not have direct export syntax, than debug problems caused by all the stupid errors that I described above.