1

I'm trying to use Python protobuf C++ implementation, and was able to use it from the command line.

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp 
export LD_LIBRARY_PATH=/usr/local/lib/
/myapp/bin/python myscript.py

However when I tried to use Django manage.py

/myapp/bin/python /myapp/app/manage.py myscript

I got

    Traceback (most recent call last):
  File "/myapp/app/manage.py", line 25, in <module>
    __import__('mymodule.scripts.' + script, fromlist=['mymodule.scripts']).main()
  File "/myapp/app/mymodule/scripts/myscript.py", line 4, in <module>
    import mycompany.protobuf.data_pb2 as pb
  File "/myapp/src/mycompany-protobuf/mycompany/protobuf/data_pb2.py", line 3, in <module>
    from google.protobuf import descriptor
  File "/myapp/lib/python2.7/site-packages/google/protobuf/descriptor.py", line 45, in <module>
    from google.protobuf.internal import cpp_message
  File "/myapp/lib/python2.7/site-packages/google/protobuf/internal/cpp_message.py", line 39, in <module>
    from google.protobuf.internal import _net_proto2___python
ImportError: libprotobuf.so.8: cannot open shared object file: No such file or directory

I already have libprotobuf.so.8 in /usr/local/lib, so it seems like manage.py is not able to access the file there?

This is what my manage.py looks like:

#!/usr/bin/env python
import os
import sys

SCRIPTS = [
    'myscript'
]

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mymodule.settings")
    os.environ.setdefault("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "cpp")
    os.environ.setdefault("LD_LIBRARY_PATH", "/usr/local/lib/")

    if len(sys.argv) >= 2 and sys.argv[1] in SCRIPTS:
        import mymodule.settings as settings

        sys.argv.pop(0)
        script = sys.argv[0]
        __import__('mymodule.scripts.' + script, fromlist=['mymodule.scripts']).main()
    else:
        from django.core.management import execute_from_command_line
        execute_from_command_line(sys.argv)

Notice that I have to set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION and LD_LIBRARY_PATH inside manage.py, because when I print out os.environ inside manage.py, those environment variables are not there.

I also tried copying myscript.py into /myapp/app, and then ran

/myapp/bin/python /myapp/app/myscript.py

and surprisingly myscript.py doesn't use the C++ protobuf implementation. If I tried setting the environment variables in the script, it resulted in the same error above.

Is there anything else that I'm missing? I think I need to put the environment variables somewhere in /myapp/app, but could not figure out the correct way to do it. Thank you very much.

wayne
  • 191
  • 1
  • 1
  • 9

1 Answers1

0

I worked around the issue by adding this into my manage.py

from ctypes import cdll
cdll.LoadLibrary('/usr/local/lib/libprotobuf.so')
os.environ.setdefault("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "cpp")
wayne
  • 191
  • 1
  • 1
  • 9