Python - ImportError: cannot import name 'db' - python

I have a project with the following directories and files structure:
I have this line of code in application.py:
db = MongoEngine()
Which i'd like to import to models.py using:
from application import db
But i get the error:
ImportError: cannot import name 'db'
How can i fix it?

I found the problem.
It's a Flask project and i was using deprecated imports:
from flask.ext.mongoengine import MongoEngine
from flask.ext.script ...
Which are now:
flask_script and flask_mongoengine.

Related

attempted relative import with no known parent package python

After saving my main.py file for FastAPI web server, I have received this error:
from . import models
ImportError: attempted relative import with no known parent package
Inside main.py, I was trying to import models.py file to main.py. Both files are under same directory:
from . import models

ImportError: cannot import name 'app' from 'app' (app.ipynb) in jupyter notebook

Here is the directory structure
▾ 1projet/
▾ apps/
__init__.ipynb
Statistics.ipynb
SWEDEN.ipynb
UAE.ipynb
app.ipynb
index.ipynb
The code I wrote in index.ipynb is
import import_ipynb
from app import app
from app import server
from apps import UAE,SWEDEN,Statistics
which gives me this error
ImportError: cannot import name 'app' from 'app' (app.ipynb)
the code I wrote in app.ipynb is :
import dash
app= dash.Dash(__name__, suppress_callback_exceptions=True,
meta_tags=[{'name': 'viewport',
'content': 'width=device-width, initial-scale=1.0'}]
)
server = app.server
and the file __init__.ipynb is empty.
You are trying to import other .ipynb files from a .ipynb file.
Please beware that this is not the same as importing a python module (.py).
Here there is already an answer which might be interesting for you, there is already mentioned that all the notebooks have to be in the same directory for succesful import.
In your case you would have to install ipynb, and then import app and server with following lines:
from ipynb.fs.full.app import app
from ipynb.fs.full.app import server
In order to succesfully import the modules UAE, SWEDEN, Statistics you either have to put the .ipynb files into the same directory as index.ipynb, or you have to put the code in those files into .py files to support absolute import. In that case you would have to add __init__.py files to each folder hierachy.
In summary you have two options for your folder structure, first option:
▾ 1projet/
__init__.py
▾ apps/
__init__.py
Statistics.py
SWEDEN.py
UAE.py
app.ipynb
index.ipynb
with imports:
from ipynb.fs.full.app import app
from ipynb.fs.full.app import server
from 1projet.apps import UAE, SWEDEN, Statistics
Second option:
app.ipynb
index.ipynb
Statistics.ipynb
SWEDEN.ipynb
UAE.ipynb
with imports:
from ipynb.fs.full.app import app
from ipynb.fs.full.app import server
from ipynb.fs.full.Statistics import *
from ipynb.fs.full.SWEDEN import *
from ipynb.fs.full.UAE import *

ModuleNotFound Error on Flask app where modules on same dir

Here is my current directory stucture
My FLASK_APP environment variable is set to app.py
and on that app.py I have this import variables
from flask import Flask
from flask import request
from flask import jsonify
from dataproviders import provider
from usecase import useCases
However, upon running the flask run I always end up getting this error.
File "C:\Users\USER\projects\project\lambda\app.py", line 5, in <module>
from dataproviders import provider
ModuleNotFoundError: No module named 'dataproviders'
The issue can be fixed by doing
from .dataproviders import provider
from .usecase import useCases
But is there anyway for us to import it using
from dataproviders import provider
from usecase import useCases
Without encountering any error like ModuleNotFoundError?

flake8: import statements are in the wrong order

PEP8 suggests that:
Imports should be grouped in the following order:
standard library imports
related third party imports
local application/library specific imports
You should put a blank line between each group of imports.
I am using Flake8Lint which Sublime Text plugin for lint Python files.
My code as below:
import logging
import re
import time
import urllib
import urlparse
from flask import Blueprint
from flask import redirect
from flask import request
from flask.ext.login import current_user
from flask.ext.login import login_required
from my_application import one_module
it will show the warning as below:
import statements are in the wrong order, from my_application should be before from from flask.ext.login
but flask is the third party library, it should before my my_application import. This is why? How to fix it?
The flake8-import-order plugin needs to be configured to know which names should be considered local to your application.
For your example, if using a .flake8 ini file in your package root directory, it should contain:
[flake8]
application_import_names = my_application
Alternatively you can use only relative imports for application local imports:
from __future__ import absolute_import
import os
import sys
import requests
from . import (
client
)
...

Flask import error: No module named app. While creating the database with Sqlite

Hi I am very new to flask and I am trying to set up a database using sqlite with my app. I have the file structure like this
app
|--Static(folder)
|--Templates(folder)
|--__init__.py (empty python file)
|--models.py(containes table classes)
|--app.py (application module)
inside my app.py file I have the following code
import os
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, url_for, request, redirect, flash
from datetime import datetime
from logging import DEBUG
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
db = SQLAlchemy(app)
So when I try to run the command
from app.app import db
in the python shell, I get the error saying that "no module named app".
Can anyone tell me what I am doing wrong here?
You shouldn't need to run
from app.app import db
This command would import a module from a "app" class called "db" in the file "app", which you don't have and I don't imagine you're trying to do.
You should just be executing your app from the terminal via
python app.py
This will just execute the code that is in the app.py file.
You can find more about this by following the flask tutorial here

Categories