Python – PyDateTime_IMPORT macro not initializing PyDateTimeAPI variable

cdatetimepython-2.6python-c-api

I'm using the Python C API on Windows using Visual Studio 2008. When I attempt to use the PyDate_Check macro, and other related macros, they cause an access violation because the static variable PyDateTimeAPI is null. This variable is initialized using the PyDateTime_IMPORT macro which needs calling before using any date time macros. I do this when creating a new Python sub-interpreter on a separate thread.

Couple of questions:
– Why is the the PyCObject_Import function in the PyDateTime_IMPORT macro returning null. I understand a null return value is because the module cannot be found. But how can the datetime module not being found? Could it be because of an incorrect sys.path in the sub-interpreter?
– Also, am I calling PyDateTime_IMPORT macro in the correct place, should it be just after the sub-interpreter is initialized, or when the Python interpreter is initialized?

PyDateTime_IMPORT definition:

#define PyDateTime_IMPORT \
    PyDateTimeAPI = (PyDateTime_CAPI*) PyCObject_Import("datetime", \
                                                        "datetime_CAPI")`

Best Answer

I ran into this same problem using G++ and Python 3.2. It has something to do that since PyDateTimeAPI is declared in the header, each file that includes that header gets its own version of the variable.

Related Topic