Unable to import models from the local python file - flask

I have installed flask and flask-sqlalchemy in my base environment of the conda. I defined the models in separate file (model.py), when I am trying to import it in my app.py file it's throwing the following error

When you write from model import User, you cause python to import from app import db, i.e. import app into app, which is not allowed in python
Please, take a look at this answer, I wrote it pretty straightforward how to solve this problem with flask-sqlalchemy:
https://stackoverflow.com/a/62991785/10468419

Related

ImportError while importing models in a different file in same directory

I am new to Django and I am enjoying it a lot. But, yesterday I tried splitting views into different files, but importing the model in that new file shows ImportError: attempted relative import with no known parent package. I am using the same statement as in view
from .models import Email
And they are in the same directory. I even tried from models import Email but it says the same ImportError: attempted relative import with no known parent package

Django Apps aren't loaded yet: How to import models

I have a Django project looking like
> project
> gui
> __init__.py
> models.py
> views.py
> ...
> project
__init__.py
...
I am trying to sync the sqllite db in django with some info I periodically query from other sources. So in project.init.py I spawn a thread that periodically queries data.
However, I am having trouble accessing my models from there and update the database, because when I try to import them into init.py
from gui.models import GuiModel
I get
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Is there a trick to do that or a different way to create a separate thread?
From the Django Official Doc, If you’re using components of Django “standalone” you should follow something like this,
import sys
import os
import django
sys.path.append("/path/to/project") # here project is root folder(means parent).
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings")
django.setup()
from gui.models import GuiModel
# do something here with models
If you send all details correctly, I think you have a circular import in your code. simple way is to to move import into your function.
also you can create a custom command in your project and add a cronjob to your server to do this works.

Django Import Model Error

I have Created an App inside my project folder, in the same directory where manage.py exists.
i have also created model for my app, when i import it in python shell like Import Poll
it gives this error:
ImportError: No module named poll
Supposing your app name is 'App' you have to type:
from App.models import Poll
in order to successfully import your Poll model. Change App with your current application name
Also, two things to bear in mind
1) Poll is different from poll: check that your capitalization is consistent with your model definition
2) You should have created the app using django startapp command. If not, at least make sure you have an empty init.py file inside your App folder.

django automate syncdb

I'm working on a project that includes a django server, and also a setup module.
The user will be configuring their system to run my program, which includes a django webserver element along with other items. I'm working on a setup module that assists the user in getting all of the settings correct and sets up all of the appropriate files. One of the things that I'd like to be during the setup process is essentially a "manage.py syncdb" command that creates an appropriate SQLite file and table from nothing.
I could grab the code found in manage.py and directly stick it into my setup module appropriately, but I'm not sure if there's a better approach that I'm missing - along the lines of two lines consisting of:
import django.something
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.something.syncdb()
Or something of the sort. Am I just missing something here?
This should do it:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core import management
management.call_command('syncdb', interactive=False)
You can also do
import os
import settings
from django.core.management.commands import syncdb
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
syncdb.Command().execute(noinput=True)

Cannot import django.core

I am trying to setup django with fastcgi on apache. Django is installed and seems to be running correctly but I am having problems with setting up fastcgi.
I decided to test out my dispatch.fcgi script in the interactive python shell line by line and the following line:
from django.core.servers.fastcgi import runfastcgi
results in the following error:
ImportError: No module named core.servers.fastcgi
I can import django with no problem but import django.core gives yet another ImportError (No module named core).
How can I go about ensuring that I can import django.core. If I can import django then in must be on my path, and so why can I not import core?
You probably have a file/folder called django somewhere in your path, that isn't the actual path.
try this
import sys
sys.path
And then check everything in that output to see if there is a file/folder called django(.py) somewhere.
If so, change the path (sys.path = ['/path/to/directory/below/django/install'] + sys.path) or move/rename the file.
Probably you have django.py module in your working directory or in any other that is in python path.
For anyone having this problem and coming across this question like I did, it turns out that fastcgi support has been removed as of Django 1.9, thus you will get this import error if trying to import it. Refer Using Django with virtualenv, get error ImportError: No module named 'django.core.servers.fastcgi'