0

I have an installed app called 'publications' and I am trying to call the 'publications' views from within my Django project. I keep getting the following error:

from citations_app import views as citations_app_views
   ImportError: No module named citations_app

So, I can't "load" the external app's views and, if I could, am not sure if I'm calling it correctly. I'm trying to use my "display_info" view function to get a list of data and then pass that data to the external app's "list_view" view function. Here is my citations.py view file:

from django.db.models import get_app
citations_app = get_app("publications")
from citations_app import views as citations_app_views
from citations_app_views import list as list_view

def display_info(request):
    citations = request.GET.getlist('citation-selection')
    return list_view(request, citations)

Any idea why I get the import error and if I am calling the "list_view" view function correctly? Thank you!!

DIRECTORY STRUCTURE:

MyApp1/
  models/ 
    publications.py
    citations.py
    items.py
  views/
    publications.py
    citations.py
    items.py
MyApp2/
Publications/ (not physically present at this location but installed as part of requirements.txt file)

I am working in views/items.py.

5
  • It seems you don't have any app called citations_app... Did you add citations_app in your settings.py in INSTALLED_APPS?? Commented Oct 28, 2014 at 16:26
  • Please show your directory structure. Have you really got an app called "citations_app", rather than just "citations"? And note that the line after that makes no sense at all, you should remove it. Commented Oct 28, 2014 at 16:31
  • The installed app is actually "publications". I'm trying to import "publications" by calling get_app("publications") and saving that as citations_app. (I have already included the app "publications" in INSTALLED_APPS)... Commented Oct 28, 2014 at 16:32
  • Why are you using get_app()—a private API that has been deprecated—instead of Python's regular import mechanism (from publications import views as citations_app_views)? Commented Oct 28, 2014 at 18:57
  • I'm using get_app() because I have files named publications.py in both the views and models directories (unrelated to the installed publications app) and I couldn't get those regular imports to work. I think it's a naming issue but couldn't figure it out. So, citations_app = get_app("publications") works in my models/items.py file to import & use the models correctly but it doesn't work in my views/items.py to get the views. In views/items.py, if I try from publications import views as citations_app_views, I get an ImportError: cannot import name views. Commented Oct 28, 2014 at 19:15

1 Answer 1

1

Forget about get_app(), you just need to get your directory structure and import paths working properly.

It sounds like you're saying that the problem is that the local publications.py is shadowing the publications app? You must be on Python 2, then, since this ambiguity is no longer possible in Python 3. To remove the ambiguity in Python 2, add from __future__ import absolute_import to the top of the file. Once you do that, from publications import ... will always refer to the top-level app. To access the local publications.py you'll use either from .publications import ... or from myapp1.models.publications import ....

See PEP 328 if you want to learn more about the absolute / relative import issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, thank you!! Yes, I am using Python 2.7: I knew it wasn't 'grabbing' the correct '"publications" but I couldn't understand why, since the external publications app is in a different directory. Wow, days spent trying to figure this out- thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.