#7372 python ipaplatform import error
Closed: worksforme 6 years ago Opened 6 years ago by slev.

There is an error while using python 2.x and importing ipaplatform python module.

python
Python 2.7.14 (default, Nov  7 2017, 17:07:09) 
[GCC 6.3.1 20170118 (ALT 6.3.1-alt2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaplatform
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named ipaplatform
>>>

This is because of skipping of installation of __init__.py file by setuptools:

Skipping installation of /usr/src/tmp/freeipa-buildroot/usr/lib/python2.7/site-packages/ipaplatform/__init__.py (namespace package)

An installation is skipped because of setuptools ( init is not needed for namespace packages and python3.3+), but python2 cannot import such modules.

https://github.com/pypa/pip/issues/1924

Workaround is create __init__.py file by manual or use touch in spec file.

touch ~/tmp/freeipa-buildroot/usr/lib/python2.7/site-packages/ipaplatform/__init__.py
python
Python 2.7.14 (default, Nov  7 2017, 17:07:09) 
[GCC 6.3.1 20170118 (ALT 6.3.1-alt2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaplatform
>>> type(ipaplatform)
<type 'module'>
>>> 

How did you install FreeIPA's Python packages? The RPM build system includes and install ipaplatform/__init__.py. If you use pip to install the wheels, then pip and setuptools are creating an ipaplatform*-nspkg.pth file. Python only processes pth files in site package directories. Neither PYTHONPATH members nor the current working directory are handled as site package directories.

I'm packing FreeIPA on ALTLinux.
As i described above - using setuptools we have:

ls -la /usr/lib/python2.7/site-packages/ipaplatform*
-rw-r--r-- 1 root root  564 янв 19 16:58 /usr/lib/python2.7/site-packages/ipaplatform-4.6.2-py2.7-nspkg.pth

/usr/lib/python2.7/site-packages/ipaplatform:
итого 124
drwxr-xr-x   8 root root  4096 янв 22 09:25 .
drwxr-xr-x 111 root root 12288 янв 22 09:19 ..
drwxr-xr-x   2 root root  4096 янв 22 09:19 altlinux
drwxr-xr-x   2 root root  4096 янв 22 09:19 base
-rw-r--r--   1 root root   243 дек 12 16:47 constants.py
-rw-r--r--   2 root root   378 янв 19 16:58 constants.pyc
-rw-r--r--   2 root root   378 янв 19 16:58 constants.pyo
drwxr-xr-x   2 root root  4096 янв 22 09:19 debian
drwxr-xr-x   2 root root  4096 янв 22 09:19 fedora
-rw-r--r--   1 root root  4392 дек 12 16:47 _importhook.py
-rw-r--r--   2 root root  4713 янв 19 16:58 _importhook.pyc
-rw-r--r--   2 root root  4713 янв 19 16:58 _importhook.pyo
-rw-r--r--   1 root root    22 янв 19 16:58 override.py
-rw-r--r--   2 root root   175 янв 19 16:58 override.pyc
-rw-r--r--   2 root root   175 янв 19 16:58 override.pyo
-rw-r--r--   1 root root   235 дек 12 16:47 paths.py
-rw-r--r--   2 root root   366 янв 19 16:58 paths.pyc
-rw-r--r--   2 root root   366 янв 19 16:58 paths.pyo
drwxr-xr-x   2 root root  4096 янв 22 09:19 redhat
drwxr-xr-x   2 root root  4096 янв 22 09:19 rhel
-rw-r--r--   1 root root   241 дек 12 16:47 services.py
-rw-r--r--   2 root root   375 янв 19 16:58 services.pyc
-rw-r--r--   2 root root   375 янв 19 16:58 services.pyo
-rw-r--r--   1 root root   235 дек 12 16:47 tasks.py
-rw-r--r--   2 root root   366 янв 19 16:58 tasks.pyc
-rw-r--r--   2 root root   366 янв 19 16:58 tasks.pyo

/usr/lib/python2.7/site-packages/ipaplatform-4.6.2-py2.7.egg-info:
итого 40
drwxr-xr-x   2 root root  4096 янв 22 09:19 .
drwxr-xr-x 111 root root 12288 янв 22 09:19 ..
-rw-r--r--   1 root root     1 янв 19 16:58 dependency_links.txt
-rw-r--r--   1 root root    12 янв 19 16:58 namespace_packages.txt
-rw-r--r--   1 root root  1274 янв 19 16:58 PKG-INFO
-rw-r--r--   1 root root    33 янв 19 16:58 requires.txt
-rw-r--r--   1 root root   869 янв 19 16:58 SOURCES.txt
-rw-r--r--   1 root root    12 янв 19 16:58 top_level.txt
Python 2.7.14 (default, Nov  7 2017, 17:07:09) 
[GCC 6.3.1 20170118 (ALT 6.3.1-alt2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaplatform
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named ipaplatform
>>> 

It works fine for me with a recent development build of 4.6 from last week. I don't have a __init__.py.

$ ls /usr/lib/python2.7/site-packages/ipaplatform*
/usr/lib/python2.7/site-packages/ipaplatform-4.6.90.dev201801151340+gitebce0c86f-py2.7-nspkg.pth

/usr/lib/python2.7/site-packages/ipaplatform:
base          constants.pyc  debian  _importhook.py   _importhook.pyo  override.pyc  paths.py   paths.pyo  rhel         services.pyc  tasks.py   tasks.pyo
constants.py  constants.pyo  fedora  _importhook.pyc  override.py      override.pyo  paths.pyc  redhat     services.py  services.pyo  tasks.pyc

/usr/lib/python2.7/site-packages/ipaplatform-4.6.90.dev201801151340+gitebce0c86f-py2.7.egg-info:
dependency_links.txt  namespace_packages.txt  PKG-INFO  requires.txt  SOURCES.txt  top_level.txt

There might be a problem with your auto-generated nspk.pth file.

$ python
Python 2.7.14 (default, Jan 11 2018, 14:45:11) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.modules['ipaplatform']  # from ipaplatform-*-nspkg.pth
<module 'ipaplatform' (built-in)>
>>> import ipaplatform
>>> import ipaplatform.constants
>>> ipaplatform.constants
<module 'ipaplatform.fedora.constants' from '/usr/lib/python2.7/site-packages/ipaplatform/fedora/constants.pyc'>

The problem was with python-setuptools.
As you said wrong generated nspkg.pth file.
Thank you for a kindly support!

You are welcome! :)

I'm glad you were able to fix the issue.

Metadata Update from @cheimes:
- Issue close_status updated to: worksforme
- Issue status updated to: Closed (was: Open)

6 years ago

Login to comment on this ticket.

Metadata