Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: No module named 'pyopencl.IPython' #41

Closed
cgohlke opened this issue May 29, 2014 · 6 comments
Closed

ImportError: No module named 'pyopencl.IPython' #41

cgohlke opened this issue May 29, 2014 · 6 comments

Comments

@cgohlke
Copy link
Contributor

cgohlke commented May 29, 2014

On Python 3.x for Windows %load_ext pyopencl.ipython fails with ImportError: No module named 'pyopencl.IPython'. Apparently the 2to3 tool converts from IPython.core.magic import ... to from .IPython.core.magic import...:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-83a3f95db82d> in <module>()
----> 1 get_ipython().magic('load_ext pyopencl.ipython')

X:\Python33\lib\site-packages\IPython\core\interactiveshell.py in magic(self, arg_s)
   2203         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2204         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205         return self.run_line_magic(magic_name, magic_arg_s)
   2206 
   2207     #-------------------------------------------------------------------------

X:\Python33\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line)
   2124                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2125             with self.builtin_trap:
-> 2126                 result = fn(*args,**kwargs)
   2127             return result
   2128 

X:\Python33\lib\site-packages\IPython\core\magics\extension.py in load_ext(self, module_str)

X:\Python33\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

X:\Python33\lib\site-packages\IPython\core\magics\extension.py in load_ext(self, module_str)
     61         if not module_str:
     62             raise UsageError('Missing module name.')
---> 63         res = self.shell.extension_manager.load_extension(module_str)
     64 
     65         if res == 'already loaded':

X:\Python33\lib\site-packages\IPython\core\extensions.py in load_extension(self, module_str)
     96             if module_str not in sys.modules:
     97                 with prepended_to_syspath(self.ipython_extension_dir):
---> 98                     __import__(module_str)
     99             mod = sys.modules[module_str]
    100             if self._call_load_ipython_extension(mod):

X:\Python33\lib\site-packages\pyopencl\ipython.py in <module>()
      1 
      2 
----> 3 from .IPython.core.magic import (magics_class, Magics, cell_magic)
      4 
      5 import pyopencl as cl

ImportError: No module named 'pyopencl.IPython'
@inducer
Copy link
Owner

inducer commented May 29, 2014

Fixed (I believe) in 2bb0d72 by renaming to ipython_ext. Can you please verify and report back? I'll release 2014.1.1 with the fix once you confirm.

Thx,
Andreas

@cgohlke
Copy link
Contributor Author

cgohlke commented May 29, 2014

This is a Windows specific issue in lib2to3 that has not been fixed yet (http://bugs.python.org/issue18477).
Rather than deprecating pyopencl.ipython one could try import ipython_ext as ipython in __init__.py or (ugly) monkey patch FixImport.probably_a_local_import in setup.py, e.g.:

    try:
        from distutils.command.build_py import build_py_2to3 as build_py
    except ImportError:
        # 2.x
        from distutils.command.build_py import build_py
    else:
        # Monkey patch FixImport.probably_a_local_import 
        # to work around http://bugs.python.org/issue18477
        from os.path import dirname, join, exists, sep, split, isdir
        from os import listdir

        def probably_a_local_import(self, imp_name):
            if imp_name.startswith("."):
                return False
            imp_name = imp_name.split(".", 1)[0]
            base_path = dirname(self.filename)
            base_path = join(base_path, imp_name)
            if not exists(join(dirname(base_path), "__init__.py")):
                return False
            path, filename = split(base_path)
            if isdir(base_path) and filename in listdir(path):
                return True
            for ext in [".py", sep, ".pyc", ".so", ".sl", ".pyd"]:
                if (filename + ext) in listdir(path):
                    return True
            return False

        from lib2to3.fixes.fix_import import FixImport
        FixImport.probably_a_local_import = probably_a_local_import

@cgohlke
Copy link
Contributor Author

cgohlke commented May 29, 2014

I see that the renaming to ipython_ext was done already. Never mind the monkey patch then. The monkey patch also breaks building on Python 2.

@cgohlke
Copy link
Contributor Author

cgohlke commented May 29, 2014

2bb0d72 does not work for me because the ipython.py file is still present.

@inducer
Copy link
Owner

inducer commented May 30, 2014

Too bad. Deleted in d3f3184. If this works (please let me know), I'll release 2014.1.1 soon so that as few people as possible start relying on pyopencl.ipython.

@inducer
Copy link
Owner

inducer commented Nov 23, 2015

The IPython extension has been called ipython_ext for a while now, which should address this. Closing.

@inducer inducer closed this as completed Nov 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants