SystemError: _PyImport_FixupExtension: module _types not loaded

U

usenet1

I'm a newbie with hopefully an easy question.

I'm trying to write some "C" code that will run a python script that
can in turn call some "C" functions. However I'm having a problem
getting started because although I can run a script from the python
ide that imports ctypes, when I execute that 'import ctypes' code from
the "C" code I get the following error:

'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python25\lib\ctypes\__init__.py", line 6, in <module>
import os as _os, sys as _sys
File "C:\Python25\lib\os.py", line 690, in <module>
import copy_reg as _copy_reg
File "C:\Python25\lib\copy_reg.py", line 7, in <module>
from types import ClassType as _ClassType
File "C:\Python25\lib\types.py", line 93, in <module>
import _types
SystemError: _PyImport_FixupExtension: module _types not loaded
[6569 refs]

For reference here is my code:
// pytest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "python.h"

int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize();
PyRun_SimpleString("import ctypes");
Py_Finalize();
return 0;
}

Steve
 
G

Gabriel Genellina

I'm trying to write some "C" code that will run a python script that
can in turn call some "C" functions. However I'm having a problem
getting started because although I can run a script from the python
ide that imports ctypes, when I execute that 'import ctypes' code from
the "C" code I get the following error:

'import site' failed; use -v for traceback

You have to fix this first. Probably you can't import anything, not just
ctypes.
Quoting myself from a similar problem:

Try this:
PyRun_SimpleString("import sys; print sys.path");
to see where Python expects to find its library (or call the Py_GetPath
function).
You may need to call Py_SetProgramName (before Py_Initialize) so it can
find where the standard library resides.
At least for testing purposes, you can copy your executable into the same
directory where Python is installed.
 
U

usenet1

You have to fix this first. Probably you can't import anything, not just
ctypes.
Quoting myself from a similar problem:

Try this:
PyRun_SimpleString("import sys; print sys.path");
to see where Python expects to find its library (or call the Py_GetPath
function).
You may need to call Py_SetProgramName (before Py_Initialize) so it can
find where the standard library resides.
At least for testing purposes, you can copy your executable into the same
directory where Python is installed.

This is what I get:

'import site' failed; use -v for traceback
['c:\\temp\\pytest\\Debug\\python25_d.zip', 'C:\\Python25\\Lib', 'C:\
\Python25\\DLLs', 'C:\\Python25\\Lib\\lib-tk', '', 'c:\\temp\\pytest\
\Debug']

In fact, the 'import site' failed; use -v for traceback happens when I
call the Py_Initialize().

As can be seen from the output, the import sys; and print sys.path
works. I also added Py_SetProgramName(argv[0]) before calling
PyInitialize() but there was no change.

Thanks,
Steve
 
G

Gabriel Genellina

This is what I get:

'import site' failed; use -v for traceback
['c:\\temp\\pytest\\Debug\\python25_d.zip', 'C:\\Python25\\Lib', 'C:\
\Python25\\DLLs', 'C:\\Python25\\Lib\\lib-tk', '', 'c:\\temp\\pytest\
\Debug']

In fact, the 'import site' failed; use -v for traceback happens when I
call the Py_Initialize().

As can be seen from the output, the import sys; and print sys.path
works. I also added Py_SetProgramName(argv[0]) before calling
PyInitialize() but there was no change.

"works" in the sense that it prints something; but sys.path is incomplete,
it lacks site-packages and others (they are added by site.py).
It appears that you have installed Python on C:\Python25 and you build
your application executable into c:\temp\pytest\Debug - is that true?
Hmmm, you will need a debug build of Python too, python25_d.lib/.dll.
Perhaps at this stage it's easier to use the Release build, because you
already have python25.lib/dll.

You have to fix the "import site" error. Use the following command on a
console window before launching your executable:
set PYTHONVERBOSE=1
You'll see a lot of lines showing the initial imports; you should be able
to detect what's the problem at "import site"; usually it's trying to load
a missing DLL.
 
U

usenet1

"works" in the sense that it prints something; but sys.path is incomplete,
it lacks site-packages and others (they are added by site.py).
It appears that you have installed Python on C:\Python25 and you build
your application executable into c:\temp\pytest\Debug - is that true?
Hmmm, you will need a debug build of Python too, python25_d.lib/.dll.
Perhaps at this stage it's easier to use the Release build, because you
already have python25.lib/dll.

You have to fix the "import site" error. Use the following command on a
console window before launching your executable:
set PYTHONVERBOSE=1
You'll see a lot of lines showing the initial imports; you should be able
to detect what's the problem at "import site"; usually it's trying to load
a missing DLL.

Thank you for your quick reply and your interest. I had a debug
python25_d.lib/dll which i was even using to step into the code to see
why it was failing but it will take me more time to understand the
python code. The other interesting thing I discovered when trying to
build my debug python dll with vs2005 usig the build8 solution was
that I was even getting the exact same error:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python25\lib\ctypes\__init__.py", line 6, in <module>
import os as _os, sys as _sys
File "C:\Python25\lib\os.py", line 690, in <module>
import copy_reg as _copy_reg
File "C:\Python25\lib\copy_reg.py", line 7, in <module>
from types import ClassType as _ClassType
File "C:\Python25\lib\types.py", line 93, in <module>
import _types
SystemError: _PyImport_FixupExtension: module _types not loaded

that I was getting when when trying to run the script. I decided at
that point to go back to python2.4, actually activestate python2.4 and
then add the ctype module. I will update this thread on my success or
failure.
Thanks,
Steve
 
U

usenet1

After installing activestate python 2.4 and ctypes-1.0.1.win32-
py2.4.exe, everything just worked. So, I decided to stay with 2.4
since I don't have time to figure out the problem.
Steve
 
G

Gabriel Genellina

After installing activestate python 2.4 and ctypes-1.0.1.win32-
py2.4.exe, everything just worked. So, I decided to stay with 2.4
since I don't have time to figure out the problem.

Glad to see it worked, at least on 2.4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top