Changing name of generated .so via disutils

910 views
Skip to first unread message

Seth Shannin

unread,
Jun 8, 2011, 12:46:25 PM6/8/11
to cython...@googlegroups.com
Hello all,

I just wanted to check to see if any of you knew how to do the following:

I have a module foo (foo.pyx) with a cython header file (foo.pxd) that has some public functions and I want to export it as a shared object file (foo.so).

Disutils provides a very nice way of doing that (in my setup.py):
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension(foo, ["foo.pyx"])]

setup(
  name = 'FooMod',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

I run `python setup.py build_ext --inplace` and I magically end up with foo.so, which is great.  However, sometimes I want to make a couple changes and then build it under a different filename and swap it in for the vanilla implementation live.  This too works almost perfectly except for one thing: the initfoo function comes out with a name related to the filename I build under.  Example: if I change the above to [Extension(foo_tmp, ["foo.pyx"])], I get foo_tmp.so, but I also get the function initfoo_tmp().

So, all of that was leadup to this question: is there anyway for me to just specify that I want the output filename to be different?  (Basically, change the -o option on the last gcc call that gets made for me)?  If anybody knows how to do this, it would be really cool.

Thanks.

Robert Bradshaw

unread,
Jun 8, 2011, 1:30:16 PM6/8/11
to cython...@googlegroups.com
On Wed, Jun 8, 2011 at 9:46 AM, Seth Shannin <ssha...@gmail.com> wrote:
> Hello all,
>
> I just wanted to check to see if any of you knew how to do the following:
>
> I have a module foo (foo.pyx) with a cython header file (foo.pxd) that has
> some public functions and I want to export it as a shared object file
> (foo.so).
>
> Disutils provides a very nice way of doing that (in my setup.py):
> from distutils.core import setup
> from distutils.extension import Extension
> from Cython.Distutils import build_ext
>
> ext_modules = [Extension(foo, ["foo.pyx"])]
>
> setup(
>   name = 'FooMod',
>   cmdclass = {'build_ext': build_ext},
>   ext_modules = ext_modules
> )
>
> I run `python setup.py build_ext --inplace` and I magically end up with
> foo.so, which is great.  However, sometimes I want to make a couple changes
> and then build it under a different filename and swap it in for the vanilla
> implementation live.  This too works almost perfectly except for one thing:
> the initfoo function comes out with a name related to the filename I build
> under.  Example: if I change the above to [Extension(foo_tmp, ["foo.pyx"])],
> I get foo_tmp.so, but I also get the function initfoo_tmp().

The initX function relates to the module name you build it under.

> So, all of that was leadup to this question: is there anyway for me to just
> specify that I want the output filename to be different?  (Basically, change
> the -o option on the last gcc call that gets made for me)?  If anybody knows
> how to do this, it would be really cool.

When python looks up module X, it looks for a file X.so which must
contain a function initX. Changing the name of the .so file without
changing the name of the module would break Python's ability to import
it.

For your usecase I would recommend a level of indirection at the
Python level, either by adding import hooks, injecting foo_tmp into
sys.modules under the name foo, or a conditional "import foo_tmp as
foo."

- Robert

Seth Shannin

unread,
Jun 8, 2011, 1:40:26 PM6/8/11
to cython...@googlegroups.com
True.  The thing is, in my specific case, I build all the python together into one .so and then swap it in and out of a c-driver program.  Currently, stuff works if I manually rename the .so files after they have been built.  I just thought it would be fun to build it with the name I wanted initially.

Thanks.

Robert Bradshaw

unread,
Jun 8, 2011, 1:44:20 PM6/8/11
to cython...@googlegroups.com
On Wed, Jun 8, 2011 at 10:40 AM, Seth Shannin <ssha...@gmail.com> wrote:
> True.  The thing is, in my specific case, I build all the python together
> into one .so and then swap it in and out of a c-driver program.  Currently,
> stuff works if I manually rename the .so files after they have been built.
> I just thought it would be fun to build it with the name I wanted initially.

My hunch is that using cp will be easier than fighting with distutils
for this usecase :)

Reply all
Reply to author
Forward
0 new messages