O
O
Oscar Django2014-12-28 16:50:22
Django
Oscar Django, 2014-12-28 16:50:22

Pycharm 4 and django-oscar incompatibility?

The problem is specific and only concerns the Pycharm 4 bundle and the django-oscar project. django-oscar has the get_classes()
function :

def get_classes(module_label, classnames):
    ...
    oscar_module_label = "oscar.apps.%s" % module_label
    oscar_module = _import_module(oscar_module_label, classnames)

    installed_apps_entry, app_name = _find_installed_apps_entry(module_label)
    if installed_apps_entry.startswith('oscar.apps.'):
        # The entry is obviously an Oscar one, we don't import again
        local_module = None
    else:
        # Attempt to import the classes from the local module
        # e.g. 'yourproject.dashboard.catalogue.forms'
        sub_module = module_label.replace(app_name, '')
        local_module_label = installed_apps_entry + sub_module
        local_module = _import_module(local_module_label, classnames)

    if oscar_module is local_module is None:
        # This intentionally doesn't raise an ImportError, because ImportError
        # can get masked in complex circular import scenarios.
        raise ModuleNotFoundError(
            "The module with label '%s' could not be imported. This either"
            "means that it indeed does not exist, or you might have a problem"
            " with a circular import." % module_label
        )

    # return imported classes, giving preference to ones from the local package
    return _pluck_classes([local_module, oscar_module], classnames)

def _import_module(module_label, classnames):
    try:
        return __import__(module_label, fromlist=classnames)
    except ImportError:
        __, __, exc_traceback = sys.exc_info()
        frames = traceback.extract_tb(exc_traceback)
        if len(frames) > 1:
            raise

The purpose of the function is to import classes from a module
my_class_1, my_class_2 = get_classes('myapp.mymodule', ['MyClass1', 'MyClass2'])
analogously
from path_to_myapp.myapp.mymodule import MyClass1 as my_class_1
from path_to_myapp.myapp.mymodule import MyClass2 as my_class_2

The logic of the function is simple:
First, we look for the module among oscar's applications.
If it is not found in oscar's applications (or redefined in user ones), then among user applications.
If it is not there, we throw an exception.
When running in debugging mode, an error occurs that the module does not exist.
Cause of the error in the last four lines of the _import_module() function
__, __, exc_traceback = sys.exc_info()
frames = traceback.extract_tb(exc_traceback)
if len(frames) > 1:
    raise

On error, frames is:
frames = [
('/home/dfdf/.virtualenvs/myenv/local/lib/python2.7/site-packages/oscar/core/loading.py', 159, '_import_module', 'return __import__(module_label, fromlist=classnames)'), 
('/opt/pycharm-4.0/helpers/pydev/_pydev_imps/_pydev_pluginbase.py', 449, 'plugin_import', 'fromlist, level)')]

Those. the program logic waits for one import exception (and this is normal), but the debugger adds its own exception
. Thus, the problem is solved by a simple fix to But since I did not write all of the above in order to show how smart I am, the question is: Is this a bug in the debugger? Or is the problem in the logic of the program? Well, or no one is to blame, and this is just a misunderstanding between the developers of two projects (pycharm and django-oscar)? After all, the idea is not "comme il faut" to edit the source code of the project. And won't it come out sideways later? if len(frames) > 1:if len(frames) > 2:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2015-12-29
@winordie

Once the sources are open, you can fork and add the change there. In addition, you can make a pull request with a description of this problem. I guess the app developers will be better able to comment on this.
You can also write to Pycharm support , but you need to keep in mind that the answer will not be soon)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question