# Torch import time

**URL:** <https://discourse.itk.org/t/torch-import-time/6354>\
**Category:** Beginner Questions\
**Tags:** python\
**Created:** [December 11, 2023, 2:39pm UTC](https://discourse.itk.org/t/torch-import-time/6354 "2023-12-11T14:39:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbaudier](https://discourse.itk.org/user_avatar/discourse.itk.org/tbaudier/32/3795_2.png) [@tbaudier](https://discourse.itk.org/u/tbaudier)\
**Post date:** [December 11, 2023, 2:39pm UTC](https://discourse.itk.org/t/torch-import-time/6354/1 "2023-12-11T14:39:38Z")

</div>

Hi,  
In a new virtual environment, I installed itk with pypi and I tested the import time:

 ![image](https://discourse.itk.org/uploads/default/original/2X/f/f550f02429ee906aa50e7310acffccc65cd1cc3f.png)

ITK is fast to import. But now, I installed torch with pypi:

 ![image](https://discourse.itk.org/uploads/default/original/2X/e/ee23c66397cf81dcb03ab6dff64fcc98d6309a98.png)

The import time is huge now, due to torch. I do not need torch type for itk Image. I do not see anything in the code [https://github.com/thewtex/ITK/blob/master/Wrapping/Generators/Python/itk/support/types.py#L34-L40](https://github.com/thewtex/ITK/blob/master/Wrapping/Generators/Python/itk/support/types.py#L34-L40) to avoid the import torch. Do you know a workaround please?

Tested with macOs, itk==5.3.0 and torch==2.1.1

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [December 11, 2023, 7:36pm UTC](https://discourse.itk.org/t/torch-import-time/6354/2 "2023-12-11T19:36:08Z")

</div>

Hi @tbaudier ,

Welcome to the ITK community ! 🌞

With ITK 5.4, we are support Python 3.8+ → we could transition the package detection to something like:

```py
from importlib.metadata import metadata

[...]
try:
    metadata('torch')
    _HAVE_TORCH = True
except importlib.metadata.PackageNotFoundError:
    pass

```

if you have a chance to test, a [patch would be appreciated](https://docs.itk.org/en/latest/contributing/index.html).

---

<div class="post-metadata">

**Author:** ![tbaudier](https://discourse.itk.org/user_avatar/discourse.itk.org/tbaudier/32/3795_2.png) [@tbaudier](https://discourse.itk.org/u/tbaudier)\
**Post date:** [December 14, 2023, 5:07pm UTC](https://discourse.itk.org/t/torch-import-time/6354/3 "2023-12-14T17:07:24Z")

</div>

Hi @matt.mccormick , thank you for your answer.

I did the PR here:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/4369>
>
> https://discourse.itk.org/t/torch-import-time/6354
> 
> Torch takes time to import…. In types.py torch is imported to check if it's present or not in the environment, but it's never used.
> 
> Now, torch is detected with importlib.metadata.
> 
> \<!-- The text within this markup is a comment, and is intended to provide
> guidelines to open a Pull Request for the ITK repository. This text will not
> be part of the Pull Request. --\>
> 
> 
> \<!-- See the CONTRIBUTING (CONTRIBUTING.md) guide. Specifically:
> 
> Start ITK commit messages with a standard prefix (and a space):
> 
> \* BUG: fix for runtime crash or incorrect result
> \* COMP: compiler error or warning fix
> \* DOC: documentation change
> \* ENH: new functionality
> \* PERF: performance improvement
> \* STYLE: no logic impact (indentation, comments)
> \* WIP: Work In Progress not ready for merge
> 
> Provide a short, meaningful message that describes the change you made.
> 
> When the PR is based on a single commit, the commit message is usually left as
> the PR message.
> 
> A reference to a related issue or pull request (https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests)
> in your repository. You can automatically
> close a related issues using keywords (https://help.github.com/articles/closing-issues-using-keywords/)
> 
> @mentions (https://help.github.com/articles/basic-writing-and-formatting-syntax/#mentioning-people-and-teams)
> of the person or team responsible for reviewing proposed changes. --\>
> 
> \## PR Checklist
> \- \[X\] No \[API changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#breaking-changes) were made (or the changes have been approved)
> \- \[X\] No \[major design changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#design-changes) were made (or the changes have been approved)
> \- \[\] Added test (or behavior not changed)
> \- \[\] Updated API documentation (or API not changed)
> \- \[\] Added \[license\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/KWStyle/ITKHeader.h) to new files (if any)
> \- \[\] Added Python wrapping to new files (if any) as described in \[ITK Software Guide\](https://itk.org/ItkSoftwareGuide.pdf) Section 9.5
> \- \[\] Added \[ITK examples\](https://github.com/InsightSoftwareConsortium/ITKSphinxExamples) for all new major features (if any)
> 
> Refer to the \[ITK Software Guide\](https://itk.org/ItkSoftwareGuide.pdf) for
> further development details if necessary.

It works well because:  
without torch in the environment (with the python itk wrapping now)

 ![Screenshot 2023-12-14 at 18.05.13](https://discourse.itk.org/uploads/default/original/2X/e/ed35275145370a83d0a88fb302882bcf27aa7e4a.png)

with torch in the environment (with the python itk wrapping now)

 ![Screenshot 2023-12-14 at 18.06.42](https://discourse.itk.org/uploads/default/original/2X/2/2ba83b5a348396cd74570a53c3af99485f257054.png)

And with the modification:

 ![Screenshot 2023-12-14 at 18.07.10](https://discourse.itk.org/uploads/default/original/2X/f/f9ca7b3b9ba3d21d7bec5c8c524020d32d4bdb86.png)
