import ctypes fails

Posted by annu7uch on 2010-01-13 06:26

Hi
I recently installed ActivePython 2.6.4.8 on my Win XP Pro (32 bit) system
PYTHONPATH variable is set as C:\Python26\Lib

when I try to import ctypes it fails with the following error message

>>> import ctypes
import ctypes # directory C:\Python26\Lib\ctypes
# C:\Python26\Lib\ctypes\__init__.pyc matches C:\Python26\Lib\ctypes\__init__.py
import ctypes # precompiled from C:\Python26\Lib\ctypes\__init__.pyc
Traceback (most recent call last):
File "", line 1, in
File "C:\Python26\Lib\ctypes\__init__.py", line 10, in
from _ctypes import Union, Structure, Array
ImportError: No module named _ctypes
>>>

Please help

thanks
Anurag

sridharr
ActiveState Staff
Thu, 2010-01-14 15:57

I cannot reproduce this at all. Are you sure you have the file DLLs\_ctypes.pyd? If not, try re-installing ActivePython.

Note that PYTHONPATH *need not* be setup. I also wonder if setting that variable is causing the issue.

-srid

annu7uch | Thu, 2010-01-14 21:33

Hi,
To double check the issue, I deleted the PYTHONPATH env variable and tested. But the issue still happened.
Then I uninstalled activePython. Downloaded "ActivePython-2.6.4.8-win32-x86.msi" again and installed it. But the issue still happened/

Following are the ctypes related files that are installed
C:\Python26\DLLs\_ctypes.pyd
C:\Python26\DLLs\_ctypes_test.pyd
C:\Python26\libs\_ctypes.lib
C:\Python26\libs\_ctypes_test.lib
C:\Python26\Lib\multiprocessing\sharedctypes.py
C:\Python26\Lib\test\test_ctypes.py
C:\Python26\Lib\ctypes\macholib\README.ctypes
C:\Python26\Lib\test\leakers\test_ctypes.py

Am I missing any file? Or is the location of these files correct?

Also, I tried installing "ActivePython-2.6.4.8-win32-x86.msi" on a different system. The issue happened on that system as well.

here is the output
C:\>python
'import site' failed; use -v for traceback
ActivePython 2.6.4.8 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Nov 3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named ctypes
>>>

Is ActivePython-2.6.4.8-win32-x86.msi a stable version? Is there any patch that needs to be installed??

sridharr
ActiveState Staff
Thu, 2010-01-14 21:46

You need to make sure that you have the following two files:

- C:\Python26\lib\ctypes\__init__.py[c]
- C:\Python26\DLLs\_ctypes.pyd

I cannot reproduce this with a fresh installation:

C:\>\Python26\python.exe
ActivePython 2.6.4.8 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Nov  3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> import _ctypes
>>> ctypes
<module 'ctypes' from 'C:\Python26\lib\ctypes\__init__.py'>
>>> _ctypes
<module '_ctypes' from 'C:\Python26\DLLs\_ctypes.pyd'>
>>>

ANURAG: Is ActivePython-2.6.4.8-win32-x86.msi a stable version?

Yes.

ANURAG: Is there any patch that needs to be installed??

No, because the ActivePython 2.6.4.8 installer installs both of these files.

From the list of files you posted, you do not seem to have the Lib/ctypes/__init__.py[c] file(s) at all. Also your Python session complains 'import site' failed; use -v for traceback which means Lib/site-packages/site.py is missing as well (unless you have modified PYTHONPATH or sys.path).

-srid

annu7uch | Thu, 2010-01-14 21:56

Hi Srid
Thanks for a quick response
I have both these files.

- C:\Python26\lib\ctypes\__init__.py
(I renamed it to "__init__.py[c]" as mentioned in your response. But the issue still remained. So I renamed it back to "__init__.py")

- C:\Python26\DLLs\_ctypes.pyd

I also have the directory named C:\Python26\Lib\ctypes which has 6 objects in it (2 folders, 4 files)

I also have site.py file
C:\Python26\Lib\site-packages\site.py

here is the output with -v

C:\>python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
'import site' failed; traceback:
ImportError: No module named site
ActivePython 2.6.4.8 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Nov 3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named ctypes
>>>

Thanks
Anurag

sridharr
ActiveState Staff
Thu, 2010-01-14 22:06

1. Open a new command prompt
2. What is the output of typing set PYTHON?
3. What is the output of typing python -c "import sys; print sys.path"?

annu7uch | Thu, 2010-01-14 22:09

hi Srid
Here is the output

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\anurags>set PYTHON
Environment variable PYTHON not defined

C:\Documents and Settings\anurags>python -c "import sys; print sys.path"
['', 'C:\\WINDOWS\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\pla
t-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\s
ite-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Python26\\lib\\site-packages\\Pytho
nwin']

C:\Documents and Settings\anurags>

sridharr
ActiveState Staff
Thu, 2010-01-14 22:14

In the same command prompt, what is the output of python -c "import ctypes; print ctypes"?

annu7uch | Thu, 2010-01-14 22:28

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\anurags>python -c "import ctypes; print ctypes"
<module 'ctypes' from 'C:\Python26\lib\ctypes\__init__.pyc'>

C:\Documents and Settings\anurags>

sridharr
ActiveState Staff
Thu, 2010-01-14 22:30

Eh? So importing ctypes work, isn't it?

What happens when you now run the Python shell and then type "import ctypes" in it?

If you still get run ImportError, try running the Python shell as "python -E -s -v" and then paste the output of running "import ctypes" in it.

annu7uch | Thu, 2010-01-14 22:33

Hey Srid
I observed that there was this Env variable called APPDATA set on my system. I deleted this env variable and the issue went away.

Thanks for you help all along.

Thanks
Anurag