2

I am writing an app to do something with DICOM images with Python (using Pydicom library). Unfortunately I am stuck at the very beginning with a problem that shouldn't exist, according to the tutorials I read (f.e. here ).

The code is simple and goes like that:

import dicom
dicomImage = dicom.read_file(MRI_img.dcm)
(...) other commands

I installed Pydicom 0.9.9 with pip (it is visible when I use pip list. I am writing code in Ecplipse with PyDev plugin.

Text editor finds "read_file" part invalid, with "Undefined variable from import: read_file" comment, and compiler says:

AttributeError: 'module' object has no attribute 'read_file'

The weird thing is even when I directly copy some code from one of tutorials available on the Net, it stays the same. Using solution from that topic won't work either. I'm slowly running out of reasonable solutions, and I still have completly no idea what is wrong.

P.S. Opening example dicom reading code from dicom/examples folder:

import sys
import dicom

# check command line arguments make sense
if not 1 < len(sys.argv) < 4:
print(__doc__)
sys.exit()

# read the file
filename = sys.argv[1]
dataset = dicom.read_file(filename)
(...)

When executed from command line - it works with no problems. That leaves me with even less idea, what's wrong with my code.

Edit: well it seems that only Eclipse/PyDev has that problem, when I execute any code from command lines, or Qt IPython command line it all works without any prolem. So what could be the problem with Eclipse? Does any of you ever encounered it?

Community
  • 1
  • 1
jakubkrol
  • 65
  • 1
  • 9
  • can you add `eclipse` tag? also have you installed since restarting eclipse? I know I have that issue with IDLE – Tadhg McDonald-Jensen Jan 26 '16 at 13:02
  • Yes, I restarted eclipse, reinstalled pydicom library, everything except pydicom modules works fine (numpy, simpleITK, etc.) – jakubkrol Jan 26 '16 at 14:32
  • Sorry I'm having issues just installing PyDev for eclipse, wish you luck resolving your problem though! – Tadhg McDonald-Jensen Jan 26 '16 at 18:44
  • Well, thank you, I will notify if I will find any solution, now it is 100% sure that problem is with pydicom library, something is wrong with it on Eclipse. – jakubkrol Jan 26 '16 at 20:02
  • 1
    alright, just found out there is also a `PyDev` tag which you should add to attract more knowledgeable people, I couldn't even install `PyDev` on my computer :( Good luck finding a solution! – Tadhg McDonald-Jensen Jan 26 '16 at 20:04
  • Tag added. And it seems I have found a solution installing pydicom 1.0.0 from the [official site](http://www.pydicom.org/). It works now, but importing and using as "pydicom" not "dicom" like 0.9.9 – jakubkrol Jan 26 '16 at 20:45

4 Answers4

2

i just doing some research also and i got the exactly same error as you do, i found out that the reason is simple, and what happen to me that raise that error is, I named my file as dicom.py..... this dicom.py is the reason and you know what happen next, just rename it to testdicom.py or any name as long as not dicom.py and will solve your problem

Hans Yulian
  • 1,080
  • 8
  • 26
  • absolutely true, if you name your file similar to the library it creates a conflict, thanks for pointing this out – pepe May 31 '17 at 23:47
1

For future information:

I haven't found a reason, but got a solution, if you have similar problem (Windows version):

First uninstall your current Python version, and delete remaining python folder
Then download and install fresh Python(x,y) here (version 2.7.10 as for 26 jan 2016)
Then enter Scripts folder in command prompt and enter pip uninstall pydcom
Then download Pydicom from official site (1.0.0 as for today)
Unzip, enter folder in command prompt and type python setup.py install
Wait a few moments until library installs.

After that you can (at least I could) use pydicom library in PyDev without any problems, following code:

import pydicom
import numpy

dicomLoc = "C:\\MR000000.dcm"
ds = pydicom.read_file(dicomLoc)
print(ds.pixel_array)

Resulted in printing proper pixel array to console:

[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 1]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 3 0 ..., 0 2 5]
 [0 9 3 ..., 1 4 4]
 [0 2 0 ..., 0 7 1]]

No further Pydicom or PyDev problems spotted since that moment.

jakubkrol
  • 65
  • 1
  • 9
0

I think you missed something quotes, so the error exist.

The code should like this:

dicomImage = dicom.read_file("MRI_img.dcm");
GYaN
  • 2,327
  • 4
  • 19
  • 39
0

You want to use pydicom and your code is

import dicom
dicomImage = dicom.read_file(MRI_img.dcm)

Actually, the correct code should like this

import pydicom
dicomImage = pydicom.read_file('MRI_img.dcm')
Milo Chen
  • 3,617
  • 4
  • 20
  • 36