Can't run Cython's hello world example

1,059 views
Skip to first unread message

Jorge E. ´Sanchez Sanchez

unread,
Nov 19, 2009, 4:57:26 PM11/19/09
to sage-s...@googlegroups.com
Hi dear sage-support group:

   I was working a Differential equations problem with few elements in some regular 3D-mesh which needed not too much calculations but I am upgrading it to a bigger system. Then I shall need to do some calculations using cython, so my first task is to build the simplest Hello world example to understand how to proceed with my implementation.
   But I think something is missing in my Cython installation or perhaps I am doing something wrong, because I cannot accomplish to build even this simplest cython example. I am using sage 4.1 in a Karmic Ubuntu 9.10.

In my first try I just follow the directions in the Cython's manual; first save a file with just the line:

   print "hello world"

as hello.pyx and then from the command line:

   python setup.py build_ext --inplace

where the setup.py file is the same as in the manual.

But I get the message:

Traceback (most recent call last):
  File "setup.py", line 3, in <module>
    from Cython.Distutils import build_ext
ImportError: No module named Cython.Distutils

within sage:
   sage -sh

running build_ext
cythoning helloworld.pyx to helloworld.c
building 'helloworld' extension
gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/opt/sage/local/include/python2.6 -c helloworld.c -o build/temp.linux-x86_64-2.6/helloworld.o
gcc -pthread -shared build/temp.linux-x86_64-2.6/helloworld.o -o helloworld.so

it finally got built the file helloworld.c and compile and link them until reach helloworld.so

but when I try the next step:
   import helloworld

/usr/bin/python: /opt/sage/local/lib/libz.so.1: no version information available (required by /usr/bin/python)
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 8, in <module>
    import CommandNotFound
ImportError: No module named CommandNotFound

My next try was following the directions in:
http://behnel.de/cython200910/talk.html

with the William Stein's file hw.py:

   import sage.all
   def hello_world():
       import sys
       print "Welcome to Sage - ", sage.all.factor(89392)
  
   if __name__ == '__main__':
       hello_world()

within sage:

   cython --embed hw.py

Cython (http://cython.org) is a compiler for code written in the
Cython language.  Cython is based on Pyrex by Greg Ewing.

Usage: cython [options] sourcefile.pyx ...

Options:
  -V, --version                  Display version number of cython compiler
  -l, --create-listing           Write error messages to a listing file
  -I, --include-dir <directory>  Search for include files in named directory
                                 (multiply include directories are allowed).
  -o, --output-file <filename>   Specify name of generated C file
  -r, --recursive                Recursively find and compile dependencies
...
...

it seems that I don't have the option --embed, to create an embedded main() function.
If I ignore this and proceed:
   cython hw.py
I get the hw.c and from here:

   gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/opt/sage/local/include/python2.6 \
         /opt/sage/local/lib/libpython2.6.a -o hw hw.c -lpthread -lm -lutil -ldl

(may be I exagerate a little with all the compilation parameters, but I get a

/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
/tmp/ccLFKVfV.o: In function `__Pyx_AddTraceback':
/home/george/hw.c:995: undefined reference to `PyString_FromString'

...
...
a bunch of more similar complains and finally
/home/george/hw.c:612: undefined reference to `PyErr_SetObject'
collect2: ld returned 1 exit status

so I need the embedded main(), could you tell me how I can proceed? (I am sorry if I extended too much).

Greetings
Jorge




¿Cómo sobrevivir en la red? Otto de IE8 te dice cómo

Robert Bradshaw

unread,
Nov 19, 2009, 10:16:31 PM11/19/09
to sage-s...@googlegroups.com
On Nov 19, 2009, at 1:57 PM, Jorge E. ´Sanchez Sanchez wrote:

> Hi dear sage-support group:
>
> I was working a Differential equations problem with few elements
> in some regular 3D-mesh which needed not too much calculations but I
> am upgrading it to a bigger system. Then I shall need to do some
> calculations using cython, so my first task is to build the simplest
> Hello world example to understand how to proceed with my
> implementation.
> But I think something is missing in my Cython installation or
> perhaps I am doing something wrong, because I cannot accomplish to
> build even this simplest cython example. I am using sage 4.1 in a
> Karmic Ubuntu 9.10.
>
> In my first try I just follow the directions in the Cython's manual;
> first save a file with just the line:
>
> print "hello world"
>
> as hello.pyx and then from the command line:
>
> python setup.py build_ext --inplace
>
> where the setup.py file is the same as in the manual.
>
> But I get the message:
>
> Traceback (most recent call last):
> File "setup.py", line 3, in <module>
> from Cython.Distutils import build_ext
> ImportError: No module named Cython.Distutils

That is because Cython is not installed into your system Python, but
the Sage one. Try running

sage -python setup.py build_ext --inplace


> within sage:
> sage -sh

OK, this should be the same as running sage -python ...

>
> running build_ext
> cythoning helloworld.pyx to helloworld.c
> building 'helloworld' extension
> gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-
> prototypes -fPIC -I/opt/sage/local/include/python2.6 -c helloworld.c
> -o build/temp.linux-x86_64-2.6/helloworld.o
> gcc -pthread -shared build/temp.linux-x86_64-2.6/helloworld.o -o
> helloworld.so
>
> it finally got built the file helloworld.c and compile and link them
> until reach helloworld.so
>
> but when I try the next step:
> import helloworld
>
> /usr/bin/python: /opt/sage/local/lib/libz.so.1: no version
> information available (required by /usr/bin/python)
> Traceback (most recent call last):
> File "/usr/lib/command-not-found", line 8, in <module>
> import CommandNotFound
> ImportError: No module named CommandNotFound

I have never heard of this error. Sounds like libz was somehow
miscompiled. Did you get your sage as a binary, or build from source?
What happens if you do "import zlib" directly?


> My next try was following the directions in:
> http://behnel.de/cython200910/talk.html

[...]

> it seems that I don't have the option --embed, to create an embedded
> main() function.

Sounds like you're using an old version of Sage (and Cython), the --
embedd option was added this spring. In fact, if it looks like 4.1.1
was the first to include this new feature.

> If I ignore this and proceed:
> cython hw.py
> I get the hw.c and from here:
>
> gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-
> prototypes -fPIC -I/opt/sage/local/include/python2.6 \
> /opt/sage/local/lib/libpython2.6.a -o hw hw.c -lpthread -lm
> -lutil -ldl
>
> (may be I exagerate a little with all the compilation parameters,
> but I get a
>
> /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In
> function `_start':
> /build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109:
> undefined reference to `main'
> /tmp/ccLFKVfV.o: In function `__Pyx_AddTraceback':
> /home/george/hw.c:995: undefined reference to `PyString_FromString'
>
> ...
> ...
> a bunch of more similar complains and finally
> /home/george/hw.c:612: undefined reference to `PyErr_SetObject'
> collect2: ld returned 1 exit status
>
> so I need the embedded main(), could you tell me how I can proceed?
> (I am sorry if I extended too much).

To use the --embed option, you'll need to upgrade to a newer version
of Sage. Then try the above and it may just work (if not, let us know).

- Robert

Jorge E. ´Sanchez Sanchez

unread,
Nov 20, 2009, 11:59:15 AM11/20/09
to sage-s...@googlegroups.com
Thank you very much Robert,

   I recently upgrade my Ubuntu 9.04 to 9.10 and because I have no problems with my binary Sage Version 4.1, Release Date: 2009-07-09 installation I thought I could stay with it, (last time I upgrade sage I had too many problems with other spkg's I had working, which I need to rebuilt -process which took me more time I am able to spend now- so I am a little affraid to make this new upgrade, although I know it deserves the pain).

   I have just tried the import zlib, within sage shell and got:


    /usr/bin/python: /opt/sage/local/lib/libz.so.1: no version information available (required by /usr/bin/python)
   Traceback (most recent call last):
     File "/usr/lib/command-not-found", line 8, in <module>
       import CommandNotFound
   ImportError: No module named CommandNotFound
 
so there must be a compatibility problem with my 4.1sage binary installation and the upgrade to Ubuntu 9.10,
what would you recommend me in my circumstances.

Jorge
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org


¿Cómo evolucionar tu navegación en la red? Otto te dice cómo

Robert Bradshaw

unread,
Nov 20, 2009, 1:02:28 PM11/20/09
to sage-s...@googlegroups.com
On Nov 20, 2009, at 8:59 AM, Jorge E. ´Sanchez Sanchez wrote:

> Thank you very much Robert,
>
> I recently upgrade my Ubuntu 9.04 to 9.10 and because I have no
> problems with my binary Sage Version 4.1, Release Date: 2009-07-09
> installation I thought I could stay with it, (last time I upgrade
> sage I had too many problems with other spkg's I had working, which
> I need to rebuilt -process which took me more time I am able to
> spend now- so I am a little affraid to make this new upgrade,
> although I know it deserves the pain).
>
> I have just tried the import zlib, within sage shell and got:
>
> /usr/bin/python: /opt/sage/local/lib/libz.so.1: no version
> information available (required by /usr/bin/python)
> Traceback (most recent call last):
> File "/usr/lib/command-not-found", line 8, in <module>
> import CommandNotFound
> ImportError: No module named CommandNotFound
>
> so there must be a compatibility problem with my 4.1sage binary
> installation and the upgrade to Ubuntu 9.10,
> what would you recommend me in my circumstances.

Yep, that's what it looks like...

What I would do is build the latest Sage from source. It may take a
couple of hours, but you can do that in the background while you work
on something else, or while you're sleeping. Given the binary
incompatibility, and that you've had issues before, I wouldn't just -
upgrade. Note that you can install the new sage parallel to the one
you have so you don't have to disrupt your current installation
(though you won't be able to have the notebook open in each unless you
make sure they're in different folders and on different ports. To be
safe, you could just avoid opening the notebook in the new sage until
you're ready to switch over, as it will attempt to update the storage
format because of architectural changes in sage 4.1.2). This way you
can get Cython to work (I hope) and make sure all your other stuff
works fine too before switching over.

- Robert

Jorge E. ´Sanchez Sanchez

unread,
Nov 21, 2009, 11:16:02 PM11/21/09
to sage-s...@googlegroups.com
Hi Robert,

   just to tell you that I have built sage-4.2.1 from source (it took my athlon 64bits 1Gb almost 5 hours), although I cannot embed a main() with cython --embed hw.pyx  I could generate the hw.so file with the setup.py and finally I could run the hw.so but not from the sage shell, because when I try
sage -sh

   /opt/sage-4.2.1
   sage subshell$ ./hw.so
   Segmentation fault

after many tries I found:
   george@george-desktop:/opt/sage-4.2.1$ ls hola*
   hola.c  hola.pyx  hola.so
   george@george-desktop:/opt/sage-4.2.1$ ls hw*
   hw.c  hw.py  hw.pyc  hw.pyx  hw.so
   george@george-desktop:/opt/sage-4.2.1$ rm hw.py hw.pyc  ### I delete the hw.py and hw.pyc to make sure I am taking hw.so
   rm: remove write-protected regular file `hw.py'? y
   rm: remove write-protected regular file `hw.pyc'? y
   george@george-desktop:/opt/sage-4.2.1$ sage
   ----------------------------------------------------------------------
   | Sage Version 4.2.1, Release Date: 2009-11-14                       |
   | Type notebook() for the GUI, and license() for information.        |
   ----------------------------------------------------------------------
   sage: import hola
   Hooooola q tal!!!
   sage: import hw
   sage: from hw import *
   sage: hello_world()
   Welcome to Sage -  2^4 * 37 * 151
   sage: exit
   Exiting SAGE (CPU time 0m0.07s, Wall time 0m43.36s).

So I go back to my sage-4.1 installation and tried to make the corresponding imports:

   george@george-desktop:~$ mv hw.py hw0.py
   george@george-desktop:~$ sage0
   ----------------------------------------------------------------------

   | Sage Version 4.1, Release Date: 2009-07-09                         |
   | Type notebook() for the GUI, and license() for information.        |
   ----------------------------------------------------------------------
   sage: from hw import *
   sage: hello_world()
   Welcome to Sage -  2^4 * 37 * 151
   sage: !ls helloworld*
   helloworld.c  helloworld.pyx  helloworld.so
   sage: import helloworld
   Hooooola q tal!!!
   sage: exit
   Exiting SAGE (CPU time 0m0.06s, Wall time 2m21.93s).

Then there is something I have not understood about how to run this *.so files. However this is all what I need.

A barely connected question I have is that: now I have the new sage-4.2 installed I would like to just define a python path in the new site-packages pointing to the old one but I don't know what happens with the other files in /opt/sage4.1/local/bin and /../lib, do I need to make a logical link also?, is this a correct fast and easy way to do this? or I need to recompile and built again all the stuff.

Ii rests me just want to thank you very much again for your help.
Jorge



> make sure they're in different folders and on different ports. To be
> safe, you could just avoid opening the notebook in the new sage until
> you're ready to switch over, as it will attempt to update the storage
> format because of architectural changes in sage 4.1.2). This way you
> can get Cython to work (I hope) and make sure all your other stuff
> works fine too before switching over.
>
> - Robert
>


Robert Bradshaw

unread,
Nov 23, 2009, 1:01:02 PM11/23/09
to sage-s...@googlegroups.com
On Nov 21, 2009, at 8:16 PM, Jorge E. ´Sanchez Sanchez wrote:

> Hi Robert,
>
> just to tell you that I have built sage-4.2.1 from source (it
> took my athlon 64bits 1Gb almost 5 hours), although I cannot embed a
> main() with cython --embed hw.pyx I could generate the hw.so file
> with the setup.py and finally I could run the hw.so but not from the
> sage shell, because when I try
> sage -sh
>
> /opt/sage-4.2.1
> sage subshell$ ./hw.so
> Segmentation fault

You can't "run" an .so file, it's a shared object library, meant to be
loaded into another program. (That's why we have --embed or
cython_freeze stuff.)

> after many tries I found:
> george@george-desktop:/opt/sage-4.2.1$ ls hola*
> hola.c hola.pyx hola.so
> george@george-desktop:/opt/sage-4.2.1$ ls hw*
> hw.c hw.py hw.pyc hw.pyx hw.so
> george@george-desktop:/opt/sage-4.2.1$ rm hw.py hw.pyc ### I
> delete the hw.py and hw.pyc to make sure I am taking hw.so
> rm: remove write-protected regular file `hw.py'? y
> rm: remove write-protected regular file `hw.pyc'? y
> george@george-desktop:/opt/sage-4.2.1$ sage
>
> ----------------------------------------------------------------------
> | Sage Version 4.2.1, Release Date:
> 2009-11-14 |
> | Type notebook() for the GUI, and license() for
> information. |
>
> ----------------------------------------------------------------------
> sage: import hola
> Hooooola q tal!!!
> sage: import hw
> sage: from hw import *
> sage: hello_world()
> Welcome to Sage - 2^4 * 37 * 151
> sage: exit
> Exiting SAGE (CPU time 0m0.07s, Wall time 0m43.36s).

So it sounds like you got it working then.

> So I go back to my sage-4.1 installation and tried to make the
> corresponding imports:
>
> george@george-desktop:~$ mv hw.py hw0.py
> george@george-desktop:~$ sage0
>
> ----------------------------------------------------------------------
> | Sage Version 4.1, Release Date:
> 2009-07-09 |
> | Type notebook() for the GUI, and license() for
> information. |
>
> ----------------------------------------------------------------------
> sage: from hw import *
> sage: hello_world()
> Welcome to Sage - 2^4 * 37 * 151
> sage: !ls helloworld*
> helloworld.c helloworld.pyx helloworld.so
> sage: import helloworld
> Hooooola q tal!!!
> sage: exit
> Exiting SAGE (CPU time 0m0.06s, Wall time 2m21.93s).
>
> Then there is something I have not understood about how to run this
> *.so files. However this is all what I need.

Excellent.

> A barely connected question I have is that: now I have the new
> sage-4.2 installed I would like to just define a python path in the
> new site-packages pointing to the old one but I don't know what
> happens with the other files in /opt/sage4.1/local/bin and /../lib,
> do I need to make a logical link also?, is this a correct fast and
> easy way to do this? or I need to recompile and built again all the
> stuff.

I'm not quite sure I understand your question here. You don't just
want to use the site-packages in 4.2.1 from 4.1, as much of the sage
stuff in site-packages depends on having the right libraries outside
of site-packages.

Or is it that you have a bunch of python modules that you have
installed into your old Sage that you want to use from the new one?
The easiest (and safest) way would be to re-install them inside the
new Sage, but you could also try copying the relevant subdirectories
of site-packages over.

- Robert

Jorge E. ´Sanchez Sanchez

unread,
Nov 23, 2009, 2:45:21 PM11/23/09
to sage-s...@googlegroups.com
   Robert:

   Now I am understanding, in the William Stein's example he could get an executable hw:
   Now I can in fact do: cython --embed hw.py without any complains
   but when I try to gcc-compile, I got a message involving the main (see below), so I thought
   that this feature is not working for me:

sage subshell$ rm hw.c
/home/george
sage subshell$ ls hw*
hw0.py    hw.py  hw.so
/home/george
sage subshell$ cython --embed hw.py
/home/george
sage subshell$ ls hw*
hw0.py    hw.c  hw.py  hw.so
/home/george
sage subshell$ gcc -I /opt/sage-4.2.1/local/include/python2.6 /opt/sage-4.2.1/local/lib/libpython2.6.a -o hw hw.c
/tmp/ccGXE0bt.o: In function `__pyx_pf_2hw_hello_world':
hw.c:(.text+0x3c): undefined reference to `_Py_NoneStruct'
hw.c:(.text+0x43): undefined reference to `_Py_NoneStruct'
hw.c:(.text+0x4e): undefined reference to `_Py_NoneStruct'
hw.c:(.text+0x150): undefined reference to `PyObject_GetAttr'
hw.c:(.text+0x1df): undefined reference to `PyObject_GetAttr'
hw.c:(.text+0x262): undefined reference to `PyTuple_New'
hw.c:(.text+0x2d8): undefined reference to `PyObject_Call'
hw.c:(.text+0x396): undefined reference to `PyTuple_New'
hw.c:(.text+0x487): undefined reference to `_Py_NoneStruct'
hw.c:(.text+0x48e): undefined reference to `_Py_NoneStruct'
hw.c:(.text+0x499): undefined reference to `_Py_NoneStruct'
/tmp/ccGXE0bt.o: In function `__Pyx_InitGlobals':
hw.c:(.text+0x5ea): undefined reference to `PyInt_FromLong'
/tmp/ccGXE0bt.o: In function `inithw':
hw.c:(.text+0x692): undefined reference to `PyTuple_New'
hw.c:(.text+0x736): undefined reference to `Py_InitModule4_64'
hw.c:(.text+0x78e): undefined reference to `PyImport_AddModule'
hw.c:(.text+0x7e6): undefined reference to `PyObject_SetAttrString'
hw.c:(.text+0x839): undefined reference to `PyObject_SetAttrString'
hw.c:(.text+0x91a): undefined reference to `PyObject_SetAttr'
hw.c:(.text+0x9f5): undefined reference to `PyObject_RichCompare'
hw.c:(.text+0xb6c): undefined reference to `PyObject_Call'
/tmp/ccGXE0bt.o: In function `main':
hw.c:(.text+0xd22): undefined reference to `Py_SetProgramName'
hw.c:(.text+0xd27): undefined reference to `Py_Initialize'
hw.c:(.text+0xd38): undefined reference to `PySys_SetArgv'
hw.c:(.text+0xd42): undefined reference to `PyErr_Occurred'
hw.c:(.text+0xd53): undefined reference to `PyErr_Print'
hw.c:(.text+0xd58): undefined reference to `Py_FlushLine'
hw.c:(.text+0xd61): undefined reference to `PyErr_Clear'
hw.c:(.text+0xda0): undefined reference to `Py_Finalize'
/tmp/ccGXE0bt.o: In function `__Pyx_Import':
hw.c:(.text+0xdf2): undefined reference to `PyObject_GetAttrString'
hw.c:(.text+0xe1c): undefined reference to `PyList_New'
hw.c:(.text+0xe3e): undefined reference to `PyModule_GetDict'
hw.c:(.text+0xe4e): undefined reference to `PyDict_New'
hw.c:(.text+0xe86): undefined reference to `PyObject_CallFunctionObjArgs'
/tmp/ccGXE0bt.o: In function `__Pyx_GetName':
hw.c:(.text+0xf72): undefined reference to `PyObject_GetAttr'
hw.c:(.text+0xf84): undefined reference to `PyExc_NameError'
hw.c:(.text+0xf93): undefined reference to `PyErr_SetObject'
/tmp/ccGXE0bt.o: In function `__Pyx_GetStdout':
hw.c:(.text+0xfab): undefined reference to `PySys_GetObject'
hw.c:(.text+0xfbd): undefined reference to `PyExc_RuntimeError'
hw.c:(.text+0xfca): undefined reference to `PyErr_SetString'
/tmp/ccGXE0bt.o: In function `__Pyx_Print':
hw.c:(.text+0x1016): undefined reference to `PyFile_SoftSpace'
hw.c:(.text+0x102b): undefined reference to `PyFile_WriteString'
hw.c:(.text+0x1068): undefined reference to `PyFile_WriteObject'
hw.c:(.text+0x109b): undefined reference to `PyString_AsString'
hw.c:(.text+0x10ab): undefined reference to `PyString_Size'
hw.c:(.text+0x110a): undefined reference to `PyFile_SoftSpace'
hw.c:(.text+0x113c): undefined reference to `PyFile_WriteString'
hw.c:(.text+0x1158): undefined reference to `PyFile_SoftSpace'
/tmp/ccGXE0bt.o: In function `__Pyx_AddTraceback':
hw.c:(.text+0x11ae): undefined reference to `PyString_FromString'
hw.c:(.text+0x11ea): undefined reference to `PyString_FromFormat'
hw.c:(.text+0x11fc): undefined reference to `PyString_FromString'
hw.c:(.text+0x121a): undefined reference to `PyModule_GetDict'
hw.c:(.text+0x1238): undefined reference to `PyString_FromStringAndSize'
hw.c:(.text+0x12bf): undefined reference to `PyCode_New'
hw.c:(.text+0x12d1): undefined reference to `_PyThreadState_Current'
hw.c:(.text+0x12e9): undefined reference to `PyFrame_New'
hw.c:(.text+0x130d): undefined reference to `PyTraceBack_Here'
/tmp/ccGXE0bt.o: In function `__Pyx_InitStrings':
hw.c:(.text+0x1490): undefined reference to `PyUnicodeUCS4_DecodeUTF8'
hw.c:(.text+0x14b8): undefined reference to `PyString_InternFromString'
hw.c:(.text+0x14e3): undefined reference to `PyString_FromStringAndSize'
/tmp/ccGXE0bt.o: In function `__Pyx_PyObject_IsTrue':
hw.c:(.text+0x152e): undefined reference to `_Py_TrueStruct'
hw.c:(.text+0x1540): undefined reference to `_Py_ZeroStruct'
hw.c:(.text+0x154f): undefined reference to `_Py_NoneStruct'
hw.c:(.text+0x156b): undefined reference to `PyObject_IsTrue'

collect2: ld returned 1 exit status
/home/george
sage subshell$

   That's why I used setup.py instead, but then I end with a .so file,
    very little in size compared with the one William got:
    sage subshell$ ls -lh hw.so
    -rwxr-xr-x 1 george george 39K 2009-11-21 21:38 hw.so
 .
   And also that's why in his example, within the sage shell he just type:
   ./hw
   and got it running, but when I try this:  
bash: ./hw: No such file or directory
   and I supposed that I had to add the extension and end with the Segmentation fault message.

   Just to get rid of my curiosity, could you see what am I missing to finish the compilation?


> You can't "run" an .so file, it's a shared object library, meant to be
> loaded into another program. (That's why we have --embed or
> cython_freeze stuff.)
>

> Excellent.

>
>
> Or is it that you have a bunch of python modules that you have
> installed into your old Sage that you want to use from the new one?
> The easiest (and safest) way would be to re-install them inside the
> new Sage, but you could also try copying the relevant subdirectories
> of site-packages over.
>
   This last one is my case,
 I'll try to re-install them as soon as I get armed with enough time and patience.
> - Robert

Best regards,
Jorge


Si solo usas tu WL id para Hotmail y Messenger, esto es para ti

Robert Bradshaw

unread,
Nov 23, 2009, 3:04:48 PM11/23/09
to sage-s...@googlegroups.com
On Nov 23, 2009, at 11:45 AM, Jorge E. ´Sanchez Sanchez wrote:

> Robert:
>
> Now I am understanding, in the William Stein's example he could
> get an executable hw:
> Now I can in fact do: cython --embed hw.py without any complains
> but when I try to gcc-compile, I got a message involving the main
> (see below), so I thought
> that this feature is not working for me:
>
> sage subshell$ gcc -I /opt/sage-4.2.1/local/include/python2.6 /opt/
> sage-4.2.1/local/lib/libpython2.6.a -o hw hw.c
> /tmp/ccGXE0bt.o: In function `__pyx_pf_2hw_hello_world':
> hw.c:(.text+0x3c): undefined reference to `_Py_NoneStruct'

[...]

> hw.c:(.text+0x156b): undefined reference to `PyObject_IsTrue'
> collect2: ld returned 1 exit status
> /home/george
> sage subshell$

This means you're missing am -L flag and -lpython2.6 (is there
supposed to be a space there?), or something like that. That's why I
never actually run the command manually--too many flags to remember.
(When using setup.py, you can see the exact gcc command that's used,
and use that one...)

> That's why I used setup.py instead, but then I end with a .so file,
> very little in size compared with the one William got:
> sage subshell$ ls -lh hw.so
> -rwxr-xr-x 1 george george 39K 2009-11-21 21:38 hw.so
> .
> And also that's why in his example, within the sage shell he just
> type:
> ./hw
> and got it running, but when I try this:
> bash: ./hw: No such file or directory
> and I supposed that I had to add the extension and end with the
> Segmentation fault message.

When you compile within Sage it's using library paths, etc. that are
set up differently outside of the sage shell. Maybe it's even using a
different version of Python, which could be bad. You can create the .c
file within the shell, and then manually compile it with gcc out of
the shell, and it should work outside the shell.

> Just to get rid of my curiosity, could you see what am I missing
> to finish the compilation?

See above.

- Robert

Jorge E. ´Sanchez Sanchez

unread,
Nov 23, 2009, 9:58:22 PM11/23/09
to sage-s...@googlegroups.com
Robert:

   I am so sorry for bothering you again with my silly questions but I am still walking in circles around the
correct compilation flags. Here's how I am succeded to build hibehnel.py as executable, where hibehnel.py is:

sage subshell$ more hibehnel.py
def hello_world():
    import sys
    print "Welcome to Python %d.%d!" % sys.version_info[:2]


if __name__ == '__main__':
    hello_world()

-> cython --embed hibehnel.py to get hibehnel.c

 Then I made a batch named xCython to compile the files $1.c with the line:

gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include  -I/opt/sage-4.2.1/local/include/python2.6 -o o$1 $1.c -lpython2.6 -lpthread -lm -lutil -ldl

   on it.

then after giving:

./xCython hibehnel
/opt/sage-4.2.1/local/lib/../lib/libpython2.6.a(posixmodule.o): In function `posix_tmpnam':
/opt/sage-4.2.1/spkg/build/python-2.6.2.p4/src/./Modules/posixmodule.c:7129: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
/opt/sage-4.2.1/local/lib/../lib/libpython2.6.a(posixmodule.o): In function `posix_tempnam':
/opt/sage-4.2.1/spkg/build/python-2.6.2.p4/src/./Modules/posixmodule.c:7084: warning: the use of `tempnam' is dangerous, better use `mkstemp'
/home/george

although those warnings I got the executable "ohibehnel":

sage subshell$ ./ohibehnel
Welcome to Python 2.6!
/home/george

working as it has to do, as expected.

But now trying with the William Stein's hw.py

/home/george
sage subshell$ ./xCython hw
/opt/sage-4.2.1/local/lib/../lib/libpython2.6.a(posixmodule.o): In function `posix_tmpnam':
/opt/sage-4.2.1/spkg/build/python-2.6.2.p4/src/./Modules/posixmodule.c:7129: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
/opt/sage-4.2.1/local/lib/../lib/libpython2.6.a(posixmodule.o): In function `posix_tempnam':
/opt/sage-4.2.1/spkg/build/python-2.6.2.p4/src/./Modules/posixmodule.c:7084: warning: the use of `tempnam' is dangerous, better use `mkstemp'
/home/george

again although those same warnings I got the executable "ohw":
but now ...

/home/george
sage subshell$ ./ohw

Traceback (most recent call last):
  File "hw.py", line 1, in hw (hw.c:561)
    import sage.all
  File "/opt/sage-4.2.1/local/lib/python2.6/site-packages/sage/all.py", line 44, in <module>
    import twisted.persisted.styles
  File "/opt/sage-4.2.1/local/lib/python2.6/site-packages/twisted/__init__.py", line 18, in <module>
    from twisted.python import compat
  File "/opt/sage-4.2.1/local/lib/python2.6/site-packages/twisted/python/compat.py", line 15, in <module>
    import sys, string, socket, struct
  File "/opt/sage-4.2.1/local/lib/python/socket.py", line 46, in <module>
    import _socket
ImportError: /opt/sage-4.2.1/local/lib/python2.6/lib-dynload/_socket.so: undefined symbol: PyExc_ValueError
/home/george

I could not run it.
(I am still missing something)
Best regards,

Robert Bradshaw

unread,
Nov 24, 2009, 12:56:26 AM11/24/09
to sage-s...@googlegroups.com
Good.

> But now trying with the William Stein's hw.py
>
> /home/george
> sage subshell$ ./xCython hw
> /opt/sage-4.2.1/local/lib/../lib/libpython2.6.a(posixmodule.o): In
> function `posix_tmpnam':
> /opt/sage-4.2.1/spkg/build/python-2.6.2.p4/src/./Modules/
> posixmodule.c:7129: warning: the use of `tmpnam_r' is dangerous,
> better use `mkstemp'
> /opt/sage-4.2.1/local/lib/../lib/libpython2.6.a(posixmodule.o): In
> function `posix_tempnam':
> /opt/sage-4.2.1/spkg/build/python-2.6.2.p4/src/./Modules/
That's strange, what happens if you do "import socket" right at the
sage prompt?

In any case, if you do "from sage.all import ..." you might have to
potentially link in every library that Sage builds (trust me, there's
a lot of them) to create a standalone executable with the --embed
option. This will work on OS X where you don't have to explicitly link
dynamic libraries.

- Robert

Robert Bradshaw

unread,
Nov 24, 2009, 1:11:28 AM11/24/09
to sage-s...@googlegroups.com
On Nov 23, 2009, at 9:56 PM, Robert Bradshaw wrote:

> In any case, if you do "from sage.all import ..." you might have to
> potentially link in every library that Sage builds (trust me, there's
> a lot of them) to create a standalone executable with the --embed
> option. This will work on OS X where you don't have to explicitly link
> dynamic libraries.

FYI, http://docs.python.org/extending/embedding.html#linking-requirements

- Robert


Jorge E. ´Sanchez Sanchez

unread,
Nov 24, 2009, 1:43:26 PM11/24/09
to sage-s...@googlegroups.com
Robert:

That was it!!!

   I've just added the flags recommended here (after making the test with distutils.sysconfig) and finally I got hw working.

Best regards,

Jorge

> From: robe...@math.washington.edu
> To: sage-s...@googlegroups.com
> Subject: Re: [sage-support] Can't run Cython's hello world example
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org

Reply all
Reply to author
Forward
0 new messages