# Python Environment

**URL:** https://discourse.itk.org/t/python-environment/1336
**Category:** Uncategorized
**Created:** [October 7, 2018, 3:37am UTC](https://discourse.itk.org/t/python-environment/1336 "2018-10-07T03:37:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![melrobin](https://discourse.itk.org/user_avatar/discourse.itk.org/melrobin/32/277_2.png) [@melrobin](https://discourse.itk.org/u/melrobin)
#### Post date: [October 7, 2018, 3:37am UTC](https://discourse.itk.org/t/python-environment/1336/1 "2018-10-07T03:37:37Z")

</div>

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  
> > > itk.Image  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > AttributeError: ‘module’ object has no attribute ‘Image’

---

<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: [October 7, 2018, 9:20am UTC](https://discourse.itk.org/t/python-environment/1336/2 "2018-10-07T09:20:28Z")

</div>

Hi @melrobin,

To just use the Python package binaries,

```auto
python -m pip install --upgrade pip
python -m pip install itk

```

will work.

To use the latest ITK 5 Beta release,

```auto
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](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch3.html#x39-440003.7).

---

<div class="post-metadata">

### Author: ![melrobin](https://discourse.itk.org/user_avatar/discourse.itk.org/melrobin/32/277_2.png) [@melrobin](https://discourse.itk.org/u/melrobin)
#### Post date: [October 7, 2018, 2:35pm UTC](https://discourse.itk.org/t/python-environment/1336/3 "2018-10-07T14:35:17Z")

</div>

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.

/home/melrobin/packages/ITK/build/Wrapping/Generators/Python  
/home/melrobin/packages/ITK/build/lib

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

---

<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: [October 8, 2018, 3:17pm UTC](https://discourse.itk.org/t/python-environment/1336/4 "2018-10-08T15:17:11Z")

</div>

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:

```auto
PYTHON_EXECUTABLE
PYTHON_INCLUDE_DIR
PYTHON_LIBRARY
PY_SITE_PACKAGES_PATH

```

point to the files and directories that you want.

---

<div class="post-metadata">

### Author: ![melrobin](https://discourse.itk.org/user_avatar/discourse.itk.org/melrobin/32/277_2.png) [@melrobin](https://discourse.itk.org/u/melrobin)
#### Post date: [October 8, 2018, 4:30pm UTC](https://discourse.itk.org/t/python-environment/1336/5 "2018-10-08T16:30:36Z")

</div>

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?

---

<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: [October 8, 2018, 4:47pm UTC](https://discourse.itk.org/t/python-environment/1336/6 "2018-10-08T16:47:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![melrobin](https://discourse.itk.org/user_avatar/discourse.itk.org/melrobin/32/277_2.png) [@melrobin](https://discourse.itk.org/u/melrobin)
#### Post date: [October 8, 2018, 6:53pm UTC](https://discourse.itk.org/t/python-environment/1336/7 "2018-10-08T18:53:10Z")

</div>

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