0

I am trying to use glog in my python code and when I am trying to import, it throws the following error:

/usr/local/lib/python2.7/dist-packages/glog.py:171: RuntimeWarning: 
Trying to access flag verbosity before flags were parsed. This will raise 
an exception in the future. 
setLevel(FLAGS.verbosity)
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/glog.py", line 171, in 
<module>
setLevel(FLAGS.verbosity)
File "/usr/local/lib/python2.7/dist-packages/gflags/flagvalues.py", line 
390, in __getattr__
traceback.print_stack()
E0602 09:45:07.674463 4695 flagvalues.py:399] Trying to access flag 
verbosity before flags were parsed.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gflags/flagvalues.py", line 
391, in __getattr__
raise exceptions.UnparsedFlagAccessError(error_message)
UnparsedFlagAccessError: Trying to access flag verbosity before
flags were parsed.

My code is as follows.

import gflags 
import glog as log

I have searched online but haven't got any information about the python version of glog. I think this error has to do something with gflags and glog together. Can anyone please explain what is going wrong ?

jpsagarm95
  • 1
  • 1
  • 4
  • I have the same problem, I'm not overly familiar with Python yet, but am wondering whether this a version conflict issue in glog. The examples for glog are quite old, and some of the class methods are throwing not found errors. – Pete855217 Jun 20 '16 at 16:15

1 Answers1

2

I have the same issue. After checking the code, it seems some kind of bug of glog.

When import glog is intercepted, it assumes gflags.GFLAGS has been initiated, and tries to read gflags.GFLAGS.verbosity, which actually is not guaranteed to be initialized in most cases.

For example, the following code will throw exception:

import gflags
import glog
import sys

if __name__ == "__main__":
    gflags.FLAGS(sys.argv)
    # do something.

Since when import glog is intercepted, it reads FLAGS.vebosity, but since glfags.FLAGS(sys.argv) is not executed yet, reading from FLAGS will fail.

So we have to make sure import gflags intercepted after GFLAGS being initialized, the only solution I could think of is putting import glog in another file instead of the main py script.

For example, in main script:

import gflags
import sys
# DON'T IMPORT ANYTHING that evolves of 'glog' 

FLAGS = gflags.FLAGS

if __name__ == "__main__":
    argv = FLAGS(sys.argv)
    # import any modle that uses 'glog'
    import your_other_module
    your_other_module.do_something()    
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55