By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

SystemError: _PyImport_FixupExtension: module _types not loaded

P: n/a
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

Feb 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
En Mon, 12 Feb 2007 15:44:36 -0300, <us*****@ingfamily.netescribió:
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.

--
Gabriel Genellina

Feb 12 '07 #2

P: n/a
On Feb 12, 2:25 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
En Mon, 12 Feb 2007 15:44:36 -0300, <usen...@ingfamily.netescribió:
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.

--
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.

Thanks,
Steve

Feb 15 '07 #3

P: n/a
En Thu, 15 Feb 2007 15:42:51 -0300, <us*****@ingfamily.netescribió:
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.

--
Gabriel Genellina

Feb 15 '07 #4

P: n/a
"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.

--
Gabriel Genellina
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

Feb 15 '07 #5

P: n/a
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

Feb 16 '07 #6

P: n/a
En Thu, 15 Feb 2007 21:49:56 -0300, <us*****@ingfamily.netescribió:
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

--
Gabriel Genellina

Feb 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.