IntelliSense isn't aware of my Django apps in PTVS 2.1

Apr 23 at 5:19 PM
I just installed PTVS 2.1 and I opened my Django project that was working with PTVS 2.0.

Now, IntelliSense does not recognize any of my apps. All local import statements have this error:
Unable to resolve "app.module". IntelliSense may be missing for this module.
However, it will not complain if I import "project.app.module".

I deleted the project file and the solution and I tried importing the existing code, but I still have the same problem. How can I fix this?
Coordinator
Apr 23 at 5:33 PM
This is a change made to PTVS 2.1 to correctly resolve import statements. Previously, we were following the Python 2.5 and earlier rules that allowed implicit relative imports, whereas now we are using the absolute import rules that were introduced in Python 2.6 (with an option in 2.5). Since we don't support versions earlier than Python 2.5, we no longer support the old style of imports.

In short, all import statements need to start with a module that is in one of the sys.path paths, or in the same folder as the module doing the importing. For example, if your module is "mod1.mod2a.mod3a", you can import "mod1.mod2a.mod3b" or just "mod3b" or ".mod3b" (same result), but "mod1.mod2b" can only be imported with "mod1" at the start. (Previously, "mod3a" could import mod2b and it would be resolved, but now that has to be spelled import ..mod2b.)

Confusing? Yeah, I spent a lot of time sorting this out as I was making the change, and it's possible that I still got it wrong. Can you post a full directory listing of your project (dir /s/b is ideal) and specify exactly which modules are showing the error, what the import statements show, and which modules they should be getting? Also, does IntelliSense work for the modules with the warnings (typing from app.module import will show this really quickly)?

The solution may be as simple as removing your project\__init__.py file, but I do want to check whether you have a legitimate layout that we're handling incorrectly.

(Finally, if the warnings are always invalid and everything else works fine, you can turn them off through Tools\Options\Python Tools\General.)
Apr 23 at 6:21 PM
Edited Apr 23 at 6:25 PM
Removing "project__init__.py" solves the problems.

The project is pretty big, but it follows more or less the recommended directory structure (except for settings, which we broke down into more files). Another thing I did differently was saving the project file and the solution file outside the project folder.

In "project\app1\models.py" there is an import "from app2.models import ...". If I type "from ...", app2 will not be in the auto complete list. The tooltip on the imported object says "<unknown_type>". If I type the first few letters anywhere after the import statement and then hit Ctrl-Space, the name will be shown in the list.
root/
    project/
        project/
            settings/
                __init__.py
                default.py
                ...
            __init__.py
            urls.py
            wsgi.py
        app1/
            templates/
            __init__.py
            models.py
            views.py
            ...
        app2/
            __init__.py
            models.py
            ...
        ...
        __init__.py
        manage.py
    media/
    static/
    .gitignore
    project.pyproj
    project.sln
Coordinator
Apr 23 at 7:03 PM
I can see why you'd put that __init__.py file there (I assume we're talking about the last one listed), but I don't know that it has any value. It certainly is the reason you need to include project in the full name, though if it is your working directory then Python will not require it.

I think right now PTVS will ignore search paths within the project when analyzing code (the start/working directory is just another search path for this purpose) which is why we find the full name even though Python itself won't require it. I would expect that if you ran "python project\manage.py ..." instead of just "python manage.py" you'd get the import error that PTVS is warning about.

I'll create a work item to try and account for search paths when doing the analysis. There are no doubt other cases that are breaking if we are not handling this properly...
Coordinator
Apr 23 at 7:05 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.