Python Environment

I am having trouble getting my Python environment set up and I know that I have screwed something up. I am building from sources and install to the system site-packages directory. The install stores many ITK related .py files and an itk directory. I receive the following error when running Python from any directory other than the build directory. Interestingly enough “import itk” works from any directory, but none of the attributes are correctly seen. The inspect module shows that itk.pyc contains the import code. I know that I should not run Python from the build directory, but can you tell me the best practices for setting PYTHONPATH, if it needs to be set at all? I did set it to the itk subdirectory of site-packages, but this did not work. It seems that the problem should be very easy to solve and maybe it just involves my removing some cruft from the lib64 directories.

import itk
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘module’ object has no attribute ‘Image’

Hi @melrobin,

To just use the Python package binaries,

python -m pip install --upgrade pip
python -m pip install itk

will work.

To use the latest ITK 5 Beta release,

python -m pip install --upgrade pip
python -m pip install --pre itk

To use Python from your build tree, copy the WrapITK.pth file into your site-packages directory.

These steps are described in greater detail in the Python Setup section of the ITK Software Guide.

Thanks for your reply Matt. I moved the WrapITK.pth to the site-packages directory, but the problem still persists. Here is the content of WrapITK.pth

Python pth file to add WrapITK’s path to sys.path.


It references paths in the local build directory instead of system paths. Is this correct behavior?

Yes, the WrapITK.pth file is for using your local build ITK tree.

If you want to build your own ITK, install it, and use the installed version, make sure that the CMake build configuration variables:


point to the files and directories that you want.

These are set to system locations, but the WrapITK.pth file still refers to the build directory. There are numerous ITK related .py files and an itk directory that was installed in the system directory.

How is WrapITK.pth created and why would it be pulling paths from the local build directory instead of directories created with those variables?

The WrapITK.pth file is for when you want to use a build tree. You should not need it for an install tree.

Matt, thank you very much for your help and vigilance. The error was ultimately that I had an (also itk.pyc) file in the directory that I was working from. This screwed things up pretty good.

1 Like