"No module named" error when importing models from other apps - django

I'm trying to import a model from another app.
I have a folder named "apps" where all my applications live.
so in the models.py of app2, i have the line:
from apps.app1.models import Book
but for some reason, i get the "No module named app1.models" error.
I'm using django1.9 if that matters.
Any advice?
Thanks.

I think from ..app1.models import Book will help you. It's a var of relative path to import.

Related

Why can't I use "profile" as an app name?

I need a profile to extend the Django User object, so I thought to create an app to contain the profile object and related views then proceed as per the Django doc, but I get
./manage.py startapp profile
CommandError: 'profile' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
I don't have an app called profile in INSTALLED_APPS and I don't know what context this message should be interpreted in. Can anybody help?
(Yes, I could call it userprofile or xxx_profile instead, but I'd like to understand why I need to)
It conflicts with std lib module profile:
https://docs.python.org/3/library/profile.html
It conflicts with an already excisting library, not made by you
Any other name that isnt a library should be fine
think with me if you have app called math when python tries to import it what it will import your app or the math module in python the modules must have unique names
You Can name it profile_app

Could not import 'cookbook.schema.schema'

I am going through the official tutorial provided by graphene-python for their library.
I, like a few others I have seen online, am having some serious issues trying to simply import the schema file within the project folder (project_name/schema.py). For reference, the project_name is cookbook as it is denoted within the tutorial.
This is within my settings.py:
GRAPHENE = {
'SCHEMA': 'cookbook.schema.schema'
}
and this is in the schema file tiself (project_name/schema.py):
import graphene
import cookbook.schema
class Query(cookbook.schema.Query, graphene.ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass
schema = graphene.Schema(query=Query)
The error that I am getting is:
Could not import 'cookbook.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: module 'cookbook' has no attribute 'schema'.
I have also tried a few other tutorials as well, but haven't had any luck. My project is on django 2.0.2 and graphene 2.0.1. Any help would be much appreciated.
Did you add this to your installed apps?
INSTALLED_APPS = [
'graphene_django',
]
the error says that cookbook has no attribute schema.
Therefore the import cookbook.schema is not working in your schema.py
the example says
import cookbook.ingredients.schema
I actually had incompatible versions of Django, Graphene and Django-environ.
To solve, I made a virtualenv using mkvirtualenv. After that, I was able to follow this tutorial without any issues. It is capable of being stood up without a virtual environment, but it was far easier to just define one and get moving with a clean slate.

Import Django models from outside the project

I'm working on an app which uses two tables from different databases.I manage to make the connection and make the tables structures in models.py, but now one I change the models.py file, I copy one of the tables in another python script, and I put the file elsewhere for other people to use it.My question it is possible in Django to import a model from outside the project? or the package?
The App is called banner_manager and in views.py I want to import a model called user from another project called django_models
when I try to import like this:
from ....models_django import models.py(in models.py it's the class "user" defined) it says: ValueError: Attempted relative import beyond top-level package
You can add this directory to PYTHONPATH for example:
export PYTHONPATH=$PYTHONPATH:/var/python/your-libs
And then just import package as normal:
import models_django

Reading existing Django models from inside Scrapy spider

I am working on a project where urls are put into a Django model called UrlItems. The models.py file containing UrlItems is located in the home app. I typed scrapy startproject scraper in the same directory as the models.py file. Please see this image to better understand my Django project structure.
I understand how to create new UrlItems from my scraper but what if my goal is to get and iterate over my Django project's existing UrlItems inside my spider's def start_requests(self) function?
What I have tried:
1) I followed the marked solution in this question to try and see if my created DjangoItem already had the UrlItems loaded. I tried to use UrlItemDjangoItem.objects.all() in my spider's start_requests function and realized that I would not be able to retrieve my Django project's UrlItems this way.
2) In my spider I tried to import my UrlItems like this from ...models import UrlItem and I received this error ValueError: attempted relative import beyond top-level package.
Update
After some consideration I may end up having the Scrapy spider query my Django application's API to receive a list of the existing Django objects in JSON.

Trouble importing function from one views.py to another app in django

I have two apps login and someother
I am trying to import a function like
From login.views import save
In someother.views.py
But getting ImportError: No module named views
I found the answer to my question.
Here it is.
My app name was "login" and have feature.py in the same.
when i tried to import feature using
from login import feature
IDE was not showing any issues. intellisense was working fine.
but when i tried to runserver it thrown ImportError: No module named feature.
and as soon as i changed login to extended_login it worked.
So i think some Keywords are not acceptable as app names or there is already a (system) app named with that keywords which actually do not have a module you trying to import.