[vtk-developers] Speeding up `import vtk`

Matt McCormick matt.mccormick at kitware.com
Thu Sep 8 13:45:04 EDT 2016


In ITK, we use lazy loading. When a class is requested, its module is
loaded. When a module is loaded, all the dependent modules are loaded
(the module dependencies are exported from the build system). This
greatly reduces import times. It will be released in ITK 4.10.1. The
source tree has the details :-)

HTH,
Matt

On Thu, Sep 8, 2016 at 1:39 PM, David Gobbi <david.gobbi at gmail.com> wrote:
> There could be a way to change the behavior of "import vtk", e.g.
>
> import vtk_settings
> vtk_settings.modules = [ ... ] # whitelist of desired modules?
> import vtk
>
> The .py files for the VTK modules are actually a bit silly, since they just
> waste time moving classes from the .pyd module to the .py module.  We should
> build vtkCommonCore.pyd directly (instead of building
> vtkCommonCorePython.pyd).
>
>  - David
>
>
> On Thu, Sep 8, 2016 at 11:24 AM, Utkarsh Ayachit
> <utkarsh.ayachit at kitware.com> wrote:
>>
>> The problem with that is it will have to be an entirely new package.
>> Further complication, this package cannot use any of *.py files for
>> VTK modules or others from the vtk package since as soon as it does
>> that vtk/__init__.py will import everything. So we'll need to
>> duplicate all the *.py files too.
>>
>> On Thu, Sep 8, 2016 at 1:14 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>> > Hi Utkarsh,
>> >
>> > As usual, I'm a stickler for backwards compatibility and would rather go
>> > in
>> > the other direction,
>> >
>> >   import vtkminimal as vtk
>> >
>> >  - David
>> >
>> >
>> > On Thu, Sep 8, 2016 at 10:55 AM, Utkarsh Ayachit
>> > <utkarsh.ayachit at kitware.com> wrote:
>> >>
>> >> Folks,
>> >>
>> >> This is in reference to this:
>> >> https://gitlab.kitware.com/vtk/vtk/issues/16780
>> >>
>> >> `import vtk` is slow because it imports all of VTK. It isn't the best
>> >> practice, and has no workaround -- given the current implementation
>> >> where all the modules are imported in vtk/__init__.py itself.
>> >>
>> >> I have an MR in progress that addresses this issue:
>> >> https://gitlab.kitware.com/vtk/vtk/merge_requests/1921
>> >>
>> >> This MR does the following:
>> >> * vtk/__init__.py no longer imports all of VTK.
>> >> * vtk/all.py is a new module that imports all of VTK.
>> >>
>> >> This does break old scripts, but provides an easy workaround. For
>> >> users who want to keep previous behavior, they can simply do the
>> >> following:
>> >>
>> >>    from vtk import all as vtk
>> >>
>> >> Thoughts? If this looks reasonable to everyone, I'll update the MR to
>> >> fix all tests accordingly.
>> >>
>> >> Thanks
>> >> Utkarsh
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>


More information about the vtk-developers mailing list