# Importing Dicom Directory via Temporary Database

**URL:** <https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013>\
**Category:** Support\
**Created:** [April 6, 2020, 3:48pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013 "2020-04-06T15:48:28Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 6, 2020, 3:48pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/1 "2020-04-06T15:48:28Z")

</div>

Hiya! I am trying to get the module I put together to run on the start-up of 3DSlicer. Right now I am getting the following error:

```
  File "Z:/SegmentDicom/SegmentDicom/SegmentDicom/SegmentDicom.py", line 103, in LoadSegment
    with DICOMUtils.TemporaryDICOMDatabase() as db:
  File "Z:\Slicer 4.11.0-2020-03-24\lib\Slicer-4.11\qt-scripted-modules\DICOMLib\DICOMUtils.py", line 306, in __enter__
    self.originalDatabaseDir = openTemporaryDatabase(self.temporaryDatabaseDir)
  File "Z:\Slicer 4.11.0-2020-03-24\lib\Slicer-4.11\qt-scripted-modules\DICOMLib\DICOMUtils.py", line 206, in openTemporaryDatabase
    originalDatabaseDir = settings.value(slicer.dicomDatabaseDirectorySettingsKey)
AttributeError: module 'slicer' has no attribute 'dicomDatabaseDirectorySettingsKey'

```

This is the code snippet where the error is occuring

```
    from DICOMLib import DICOMUtils
    loadedNodeIDs = []

    with DICOMUtils.TemporaryDICOMDatabase() as db:
      DICOMUtils.importDicom(dicomDataDir, db)
      patientUIDs = db.patients()
    for patientUID in patientUIDs:
      loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

```

May the issue have to do with running this module on the start-up? And if it is, then is there a better way to have my module run automatically on startup~ Thank you in advance!!!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 6, 2020, 6:25pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/2 "2020-04-06T18:25:28Z")

</div>

Does the script work well if you launch it manually after Slicer is started?

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 6, 2020, 6:52pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/3 "2020-04-06T18:52:09Z")

</div>

Yes, it works perfectly fine after Slicer has fully loaded.  
I imagine that some elements need to load before running this module. In this case, is there some way I can add a delay to the module or have it load the dicom module before running?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 6, 2020, 7:04pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/4 "2020-04-06T19:04:01Z")

</div>

How do you launch your code at startup?

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 6, 2020, 7:14pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/5 "2020-04-06T19:14:45Z")

</div>

I just went into Application Settings and set the module to be the startup module.  
In code I have the logic running by having the apply button be true. Which to my understanding and the way it usually works is when the module loads the button, then the logic runs.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 6, 2020, 7:23pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/6 "2020-04-06T19:23:40Z")

</div>

At the time a module’s constructor is executed, not all the modules have been initialized yet. If you want to run in your module something at startup then you need to use `startupCompleted` signal of the application:

```
slicer.app.connect("startupCompleted()", self.someMethodToRunAtStartup)
```

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 6, 2020, 8:12pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/7 "2020-04-06T20:12:40Z")

</div>

Ohh! Thank you so much! This is what I have been searching for~

I would place this in the Widget Class of the module I created or somewhere else?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 6, 2020, 8:52pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/8 "2020-04-06T20:52:29Z")

</div>

This connection has to be created when the module is created (in the module class constructor), but it can call methods anywhere. Since widget is only created when you open the module the first time and module logic is created even later, the called method should be in the module class (or a static method in the widget or logic class).

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 8, 2020, 2:00pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/9 "2020-04-08T14:00:55Z")

</div>

Heya! Congrats on your anniversary!!! I believe I did what you suggested and placed the line slicer.app.connect in the Module Class.

I got the following error, which seems like the one I was having before.

> ```
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> File "Z:\Slicer 4.11.0-2020-03-24\lib\Python\Lib\imp.py", line 170, in load_source
> module = _exec(spec, sys.modules[name])
> File "<frozen importlib._bootstrap>", line 618, in _exec
> File "<frozen importlib._bootstrap_external>", line 678, in exec_module
> File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
> File "Z:/SegmentDicom/SegmentDicom/SegmentDicom/SegmentDicom.py", line 11, in <module>
> class SegmentDicom(ScriptedLoadableModule):
> File "Z:/SegmentDicom/SegmentDicom/SegmentDicom/SegmentDicom.py", line 137, in SegmentDicom
> slicer.app.connect("startupCompleted()", LoadSegment())
> File "Z:/SegmentDicom/SegmentDicom/SegmentDicom/SegmentDicom.py", line 40, in LoadSegment
> with DICOMUtils.TemporaryDICOMDatabase() as db:
> File "Z:\Slicer 4.11.0-2020-03-24\lib\Slicer-4.11\qt-scripted-modules\DICOMLib\DICOMUtils.py", line 306, in __enter__
> self.originalDatabaseDir = openTemporaryDatabase(self.temporaryDatabaseDir)
> File "Z:\Slicer 4.11.0-2020-03-24\lib\Slicer-4.11\qt-scripted-modules\DICOMLib\DICOMUtils.py", line 206, in openTemporaryDatabase
> originalDatabaseDir = settings.value(slicer.dicomDatabaseDirectorySettingsKey)
> AttributeError: module 'slicer' has no attribute 'dicomDatabaseDirectorySettingsKey'
> 
> ```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 8, 2020, 3:26pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/10 "2020-04-08T15:26:24Z")

</div>

Maybe the signal is called too early. You can call a singleshot timer with a 1-second delay in the method that is called by startupCompleted to give some time for the DICOM module to initialize itself.

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 8, 2020, 9:20pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/11 "2020-04-08T21:20:03Z")

</div>

Turns out a 0.1 second delay using the solution you provided worked out! Thank you so much!!!

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 13, 2020, 5:41pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/12 "2020-04-13T17:41:46Z")

</div>

Hello there! I thought it be best I post in this thread since it is related.  
I found that when I don’t import the dicom directory manually before running the module it gives me the following error. It does work when I manually add it beforehand, but I would like to fully automate the process.

```
Traceback (most recent call last):
  File "Z:/SegmentDicom/SegmentDicom/SegmentDicom/SegmentDicom.py", line 54, in ProceduralSegmentation
    loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))
  File "Z:\Slicer 4.11.0-2020-03-24\lib\Slicer-4.11\qt-scripted-modules\DICOMLib\DICOMUtils.py", line 41, in loadPatientByUID
    raise IOError('No patient found with DICOM database UID %s' % patientUIDstr)
OSError: No patient found with DICOM database UID 1

```

This is the error that I am getting. Any idea on what I may be missing? Also happy to answer any details needed~

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 13, 2020, 5:54pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/13 "2020-04-13T17:54:49Z")

</div>

You can only load images that have been already imported.

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 13, 2020, 6:11pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/14 "2020-04-13T18:11:36Z")

</div>

I took this code from the Nightly Script Repo for importing a dicom directory

```
    dicomDataDir = "C:/Users/Public/Pictures"
    from DICOMLib import DICOMUtils
    loadedNodeIDs = []

    with DICOMUtils.TemporaryDICOMDatabase() as db:
      DICOMUtils.importDicom(dicomDataDir, db)
      patientUIDs = db.patients()
    for patientUID in patientUIDs:
      loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

```

I put a 10 second delay just to make sure it wasn’t due to it loading right away and I found it gives me the same error and right before it this is posted:

> “DICOM indexer has successfully inserted 81 files [0.05s]”  
> “DICOM indexer has successfully processed 82 files [0.15s]”  
> “DICOM indexer has updated display fields for 81 files [0.02s]”

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 13, 2020, 6:24pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/15 "2020-04-13T18:24:25Z")

</div>

The code above is incorrectly indented, which deletes the DICOM database immediately after import is completed. See the code with correct indentation in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_load_DICOM_files_into_the_scene_from_a_folder). Code indentation is not just formatting in Python, so you need to pay very close attention to it.

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [April 13, 2020, 6:27pm UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/16 "2020-04-13T18:27:01Z")

</div>

Wow, I completely missed that when I was pasting it in. Thanks for spotting that for me! I will do better to pay careful attention!!!

---

<div class="post-metadata">

**Author:** ![BigBrotherHui](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bigbrotherhui/32/13630_2.png) [@BigBrotherHui](https://discourse.slicer.org/u/BigBrotherHui)\
**Post date:** [December 24, 2021, 8:04am UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/17 "2021-12-24T08:04:49Z")

</div>

hello,lassoan.i also have trouble in “AttributeError: module ‘slicer’ has no attribute ‘dicomDatabaseDirectorySettingsKey’” when i just code the below code:

PythonQt::init();  
PythonQtObjectPtr context = PythonQt::self()-\>getMainModule();  
context.evalScript(“import DICOMLib”);  
context.evalScript(“slicer.util.selectModule(‘DICOM’)”);

i want import dicom by python script.but failed.i am troubled in this for 5 days.could you help me please?

---

<div class="post-metadata">

**Author:** ![BigBrotherHui](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bigbrotherhui/32/13630_2.png) [@BigBrotherHui](https://discourse.slicer.org/u/BigBrotherHui)\
**Post date:** [December 24, 2021, 8:07am UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/18 "2021-12-24T08:07:32Z")

</div>

it’s very helpful to have someone’s email to sovle this trouble.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 13, 2022, 5:26am UTC](https://discourse.slicer.org/t/importing-dicom-directory-via-temporary-database/11013/19 "2022-03-13T05:26:43Z")

</div>

`slicer.dicomDatabaseDirectorySettingsKey` is set in the DICOM module. Probably you run this script too early, before the application has been fully started up. If you want to run a script after startup is completed then you can connect a slot to the `startupCompleted` signal of the application.
