Pyrex / py2exe import interaction

Clark C . Evans cce at clarkevans.com
Tue Jul 30 16:54:58 EDT 2002


Hello.  I'm having difficulty with using a combination
of pyrex and py2exe; the problem occurs when the module
compiled with pyrex has an "import".  The program then
works just fine until the calling script is packaged
up with py2exe, then you get the following:

    Traceback (most recent call last):
      File "<string>", line 1, in ?
      File "imputil.pyc", line 103, in _import_hook
      File "<string>", line 52, in _import_top_module
      File "imputil.pyc", line 216, in import_top
      File "imputil.pyc", line 267, in _import_one
      File "<string>", line 163, in get_code
      File "imputil.pyc", line 89, in _import_hook
      File "imputil.pyc", line 152, in _determine_import_context
    AttributeError: 'module' object has no attribute 'get'

Anyway, both of these programs play all sorts of tricks to
get importing to work, so I figure that interaction of the
two importing strategies is causing difficulty.

Following is the source code for the various files that 
I'm using.  Further, these files are packged up at
http://clarkevans.com/tmp/prob.tgz ; the build was performed
with ActiveState 2.2.1 on Win2K using py2exe 0.3.3 and 
pyrex 0.4 compiled using mingw32, gcc 2.95.3-6

Any help would be very cool.  My actual setup is much more
complicated than this, but I tried to make the smallest 
example which exhibits the behavior.   Also, I've had to 
split "setup.py" into two distinct files since I didn't 
know how to make them both work together.

Oh yes, both of these are *fantastic* tools.  Thank you
for them and your help!

Clark

P.S.  Great tools!

--------------- Makefile -------------------
test: clean dist
	cd dist/test; ./test.exe

testmod.pyd: testmod.pyx
	python build.py build_ext --inplace --compiler=mingw32
        
dist: test.py testmod.pyd
	python dist.py py2exe 

clean:
	rm -f *.c *.o *.so *.cpp *.pyd *.pyc *~ *.bak core
	rm -rf build dist


--------------- build.py -------------------
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext
setup(
  name = 'testmod',
  ext_modules=[ 
    Extension("testmod",["testmod.pyx"]),
    ],
  cmdclass = {'build_ext': build_ext}
)


--------------- dist.py --------------------
from distutils.core import setup
import py2exe
setup(
  name = 'Demos',
  scripts = ["test.py"],
)

--------------- testmod.pyx ----------------
import string
def go(): 
    for x in string.split("one,two",","):
        print x

--------------- test.py --------------------
from testmod import go
go()

--------------- OUTPUT ---------------------
$ make test
python build.py build_ext --inplace --compiler=mingw32
running build_ext
building 'testmod' extension
testmod.cpp: In function `struct PyObject * __pyx_f_go(PyObject *, PyObject *, PyObject *)':
testmod.cpp:73: warning: label `__pyx_L3' defined but not used
testmod.cpp:59: warning: label `__pyx_L2' defined but not used
creating build
creating build\temp.win32-2.2
creating build\temp.win32-2.2\Release
C:\msys\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -Ic:\Python22\include -c testmod.cpp -o build\temp.win32-2.2\Release\testmod.o
writing build\temp.win32-2.2\Release\testmod.def
C:\msys\mingw\bin\gcc.exe -mno-cygwin -mdll -static -s build\temp.win32-2.2\Release\testmod.o build\temp.win32-2.2\Release\testmod.def -Lc:\Python22\libs -lpython22 -o testmod.pyd
python dist.py py2exe 
running py2exe
running build
running build_scripts
creating build\scripts-2.2
copying test.py -> build\scripts-2.2
running install_scripts
creating build\bdist.win32
creating build\bdist.win32\winexe
creating build\bdist.win32\winexe\lib
creating build\bdist.win32\winexe\lib\Python22
creating build\bdist.win32\winexe\lib\Python22\Scripts
copying build\scripts-2.2\test.py -> build\bdist.win32\winexe\lib\Python22\Scripts
creating dist
+----------------------------------------------------
| Processing script test.py with py2exe-0.3.3
+----------------------------------------------------
creating dist\test
creating build\bdist.win32\winexe\collect
creating build\bdist.win32\winexe\collect\test
creating build\bdist.win32\winexe\collect\test\Scripts.py2exe
Searching modules needed to run 'test.py' on path:
['c:\\cygwin\\home\\cce\\prob\\build\\bdist.win32\\winexe\\lib\\Python22\\Lib\\site-packages', '', 'C:\\Python22\\Lib\\site-packages\\Pythonwin', 'C:\\Python22\\Lib\\site-packages\\win32', 'C:\\Python22\\Lib\\site-packages\\win32\\lib', 'C:\\Python22\\Lib\\site-packages', 'c:\\Python22\\DLLs', 'c:\\Python22\\lib', 'c:\\Python22\\lib\\lib-tk', 'c:\\Python22']
force_imports = 
Resolving binary dependencies:
  C:\WINNT\System32\python22.dll
  testmod.pyd
  C:\Python22\Lib\site-packages\py2exe\run.exe
ext_mapping = {
 'testmod': ('testmod.pyd', ('.pyd', 'rb', 3))
}
copying C:\Python22\Lib\site-packages\py2exe\support.py -> build\bdist.win32\winexe\collect\test\Scripts.py2exe
byte-compiling c:\Python22\lib\imputil.py to imputil.pyc
copying test.py -> build\bdist.win32\winexe\collect\test\Scripts.py2exe\__main__.py
changing into 'build\bdist.win32\winexe\collect\test'
c:\cygwin\bin\zip.exe -rq c:\cygwin\home\cce\prob\build\bdist.win32\winexe\test.zip .
changing back to 'c:\cygwin\home\cce\prob'
creating dist\test\test.exe
copying C:\WINNT\System32\python22.dll -> dist\test
copying testmod.pyd -> dist\test
removing 'build\bdist.win32\winexe\collect\test' (and everything under it)
Built File dist\test\test.exe
removing 'build\bdist.win32\winexe' (and everything under it)
warning: py2exe: could not parse version number ''
  No VersionInfo will be created
cd dist/test; ./test.exe
Traceback (most recent call last):
  File "<string>", line 1, in ?
  File "imputil.pyc", line 103, in _import_hook
  File "<string>", line 52, in _import_top_module
  File "imputil.pyc", line 216, in import_top
  File "imputil.pyc", line 267, in _import_one
  File "<string>", line 163, in get_code
  File "imputil.pyc", line 89, in _import_hook
  File "imputil.pyc", line 152, in _determine_import_context
AttributeError: 'module' object has no attribute 'get'
-- 
Clark C. Evans                   Axista, Inc.
http://www.axista.com            800.926.5525
XCOLLA Collaborative Project Management Software




More information about the Python-list mailing list