Circular import with beanie ODM - python

I need to use a cross-reference in my MongoDB schema. I use beanie as ODM. Here is my models:
entity.py
from beanie import Document
class Entity(Document):
path: List["Folder"] = []
folder.py
from entity import Entity
class Folder(Entity)
pass
init_beanie.py
import beanie
from motor.motor_asyncio import AsyncIOMotorClient
from entity import Entity
from folder import Folder
models = [Entity, Folder]
async def init_beanie():
client = AsyncIOMotorClient("mongo-uri")
Entity.update_forward_refs(Folder=Folder)
await beanie.init_beanie(database=client["mongo-db-name"], document_models=models)
main.py
from fastapi import FastAPI
from init_beanie import init_beanie
my_app = FastAPI()
#my_app.on_event("startup")
async def init():
await init_beanie()
But when I start my app I got an erorr:
...
File "pydantic/main.py", line 816, in pydantic.main.BaseModel.update_forward_refs
File "pydantic/typing.py", line 553, in pydantic.typing.update_model_forward_refs
File "pydantic/typing.py", line 519, in pydantic.typing.update_field_forward_refs
File "pydantic/typing.py", line 65, in pydantic.typing.evaluate_forwardref
File "/usr/local/lib/python3.9/typing.py", line 554, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'Folder' is not defined
what am I doing wrong?

Related

ModuleNotFoundError: No module named '_sqlite3' (SQLAlchemy Flask)

I have used sql-alchemy for so long but I don't know it suddnly stopped working and showing me errors
Code
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# create the extension
db = SQLAlchemy()
# create the app
app = Flask(__name__)
# configure the SQLite database, relative to the app instance folder
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
# initialize the app with the extension
db.init_app(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String)
with app.app_context():
db.create_all()
this is code available on sqlalchemy documentation
Error
(venv) viral#viral-Lenovo:~/Desktop/sql-practice$ python main.py
Traceback (most recent call last):
File "/home/viral/Desktop/sql-practice/main.py", line 11, in <module>
db.init_app(app)
File "/home/viral/Desktop/sql-practice/venv/lib/python3.11/site-packages/flask_sqlalchemy/extension.py", line 326, in init_app
engines[key] = self._make_engine(key, options, app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/viral/Desktop/sql-practice/venv/lib/python3.11/site-packages/flask_sqlalchemy/extension.py", line 614, in _make_engine
return sa.engine_from_config(options, prefix="")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/viral/Desktop/sql-practice/venv/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 817, in engine_from_config
return create_engine(url, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 2, in create_engine
File "/home/viral/Desktop/sql-practice/venv/lib/python3.11/site-packages/sqlalchemy/util/deprecations.py", line 277, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^
File "/home/viral/Desktop/sql-practice/venv/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 605, in create_engine
dbapi = dbapi_meth(**dbapi_args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/viral/Desktop/sql-practice/venv/lib/python3.11/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 504, in import_dbapi
from sqlite3 import dbapi2 as sqlite
File "/usr/local/lib/python3.11/sqlite3/__init__.py", line 57, in <module>
from sqlite3.dbapi2 import *
File "/usr/local/lib/python3.11/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
I expected that it will create a database by creating new folder called instance. but it's only creating instance folder but not database
I have tried creating new venv, reinstalling python and restarting pc but still it's showing the same error.
note: I am using Linux, Ubuntu Kde plasma

FastApi: App startup hangs at Router URL parsing

I'm writing a fastapi app with simple routers, when I try to start the app the start-up hangs at URL parsing in the re module. Please find the stack trace on KeyboardInterrupt below.
Traceback (most recent call last):
File "/Users/ssekar/ssa/code/classifier-api/app/main.py", line 7, in <module>
from app.api.classification_api import router as api_router
File "/Users/ssekar/ssa/code/classifier-api/app/api/classification_api.py", line 22, in <module>
router.include_router(
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/fastapi/routing.py", line 569, in include_router
self.add_api_route(
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/fastapi/routing.py", line 439, in add_api_route
route = route_class(
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/fastapi/routing.py", line 290, in __init__
self.path_regex, self.path_format, self.param_convertors = compile_path(path)
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 123, in compile_path
return re.compile(path_regex), path_format, param_convertors
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/re.py", line 252, in compile
return _compile(pattern, flags)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/re.py", line 304, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_parse.py", line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_parse.py", line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_parse.py", line 529, in _parse
subpatternappend((LITERAL, _ord(this)))
KeyboardInterrupt
The code
from fastapi import APIRouter
from starlette import status
from starlette.responses import JSONResponse
from app.services.metedata_services import get_categories
router = APIRouter()
#router.get(
"/categories",
status_code=status.HTTP_200_OK,
)
def categories(tree: str):
.....
return JSONResponse(content=response)
router.include_router(
router,
tags=["categories"],
prefix="/v1",
)
You should call the include_router(...)--(FastAPI Doc) method of FastAPI class, not APIRouter
# some_app/api/routers.py
from fastapi import APIRouter
from starlette import status
from starlette.responses import JSONResponse
router = APIRouter()
#router.get(
"/categories",
status_code=status.HTTP_200_OK,
)
def categories(tree: str):
response = {"foo": "bar"}
return JSONResponse(content=response)
# main.py
import uvicorn
from fastapi import FastAPI
from some_app.api.routers import router
app = FastAPI()
app.include_router(
router,
tags=["categories"],
prefix="/v1",
)
if __name__ == "__main__":
uvicorn.run(app)

Flask how to get config element from another file (for example: helper.py)

Good day.
I have one question. How to access a configuration item from another .py file
For example.
app/__init__.py
def create_app(config=None):
app = Flask(__name__, instance_relative_config=True)
app._static_folder = './static'
app.config.from_pyfile('flask.cfg')
app.app_context().push()
return app
wsgi.py
from app import create_app
from werkzeug.debug import DebuggedApplication
if __name__ == "__main__":
app = create_app()
application = DebuggedApplication(app, evalex=True)
app.logger.info("Debug status is: " + str(app.config['DEBUG']))
app.run(use_reloader=True, debug=True)
app/core/helper.py
from flask import current_app as app
def get_dir():
return app.config["PATH_CACHE_DIR"]
Try to call method app/core/cached.py
from flask_caching import Cache
from flask import current_app as app
from app.core.helper import get_dir
print get_dir()
So, i receive the following error
Traceback (most recent call last):
File "/home/stanislav/Python/mbgp/wsgi.py", line 1, in <module>
from app import create_app
File "/home/stanislav/Python/mbgp/app/__init__.py", line 4, in <module>
from app.core.cached import cache
File "/home/stanislav/Python/mbgp/app/core/cached.py", line 5, in <module>
print get_dir()
File "/home/stanislav/Python/mbgp/app/core/helper.py", line 14, in get_dir
return app.config["PATH_CACHE_DIR"]
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 51, in _find_app
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current applicat`enter code here`ion object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
Please tell me how to fix this, thank you very much.

Flask-SQLAlchemy unittests failing InvalidRequestError: Table '{table_name}' is already defined for this MetaData instance

I'm building an API service with Flask, SQLAlchemy and more recently integrating the Flask-SQLAlchemy extension. While I can run the app standalone and make API calls successfully, I am hitting a problem attempting to run unittests. I believe the issue is with importing the db.Model types more than once.
The exception is this:
Error
Traceback (most recent call last):
File "/Users/james/.pyenv/versions/2.7.10/lib/python2.7/unittest/case.py", line 322, in run
self.setUp()
File "/Users/james/Documents/workspace/trustmile-backend/trustmile/tests/test_users.py", line 28, in setUp
from trustmile.app.users.model import User, ConsumerUser, CourierUser, AuthSession, Location, UserAddress, db
File "/Users/james/Documents/workspace/trustmile-backend/trustmile/app/users/model.py", line 23, in <module>
class User(db.Model, UniqueMixin, TableColumnsBase, References):
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 536, in __init__
DeclarativeMeta.__init__(self, name, bases, d)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 55, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
_MapperConfig.setup_mapping(cls, classname, dict_)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
cfg_cls(cls_, classname, dict_)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
self._setup_table()
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 394, in _setup_table
**table_kw)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 398, in __new__
"existing Table object." % key)
InvalidRequestError: Table 'tmuser' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
My init.py in /app/ (the top level) is like this:
__author__ = 'james'
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import config
from app.messaging import EmailHandler
app = Flask(__name__, static_folder='api/static', static_url_path='/static')
app.config.from_object(config)
db = SQLAlchemy(app)
EmailHandler.setup(config.EMAIL_API_KEY_DEV)
from app.api.consumer_v1 import bp as blueprint
app.register_blueprint(blueprint, url_prefix = '/consumer/v1')
app.test_request_context()
Running that with:
from app import app
app.run(debug=True, host='0.0.0.0', port=5001)
Works great.
The beginning of my test_users.py looks like this:
__author__ = 'james'
from trustmile.app.exc import InvalidEmailException
from trustmile.app.exc import InsecurePasswordException
from nose.tools import assert_true, raises
from trustmile.app.users.model import User, ConsumerUser, CourierUser, AuthSession, Location, UserAddress, db
from . import TransactionalTest
email_address = 'james#cloudadvantage.com.au'
test_password = 'mypassword'
class UserTest(TransactionalTest):
#classmethod
def setUpClass(cls):
super(UserTest, cls).setUpClass()
def setUp(self):
super(UserTest, self).setUp()
def test_create_user(self):
user = User()
db.session.add(user)
It must be fairly common way to do unit testing and yes I'm sharing the SQLAlchemy object between modules (which I'm sure is bad practice). I'm running it with nosetests and the error occurs as the code is initialised. Sorry for the lengthy question, hopefully someone can help me!
__table_args__ = {'extend_existing': True}
right below __tablename__

How to import django models in scrapy pipelines.py file

I'm trying to import models of one django application in my pipelines.py to save data using django orm. I created a scrapy project scrapy_project in the first involved django application "app1" (is it a good choice by the way?).
I added these lines to my scrapy settings file:
def setup_django_env(path):
import imp, os
from django.core.management import setup_environ
f, filename, desc = imp.find_module('settings', [path])
project = imp.load_module('settings', f, filename, desc)
setup_environ(project)
current_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
setup_django_env(os.path.join(current_dir, '../../d_project1'))
When I try to import models of my django application app1 I get this error message:
Traceback (most recent call last):
File "/usr/local/bin/scrapy", line 4, in <module>
execute()
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 122, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 76, in _run_print_help
func(*a, **kw)
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 129, in _run_command
cmd.run(args, opts)
File "/usr/local/lib/python2.7/dist-packages/scrapy/commands/crawl.py", line 43, in run
spider = self.crawler.spiders.create(spname, **opts.spargs)
File "/usr/local/lib/python2.7/dist-packages/scrapy/command.py", line 33, in crawler
self._crawler.configure()
File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py", line 41, in configure
self.engine = ExecutionEngine(self, self._spider_closed)
File "/usr/local/lib/python2.7/dist-packages/scrapy/core/engine.py", line 63, in __init__
self.scraper = Scraper(crawler)
File "/usr/local/lib/python2.7/dist-packages/scrapy/core/scraper.py", line 66, in __init__
self.itemproc = itemproc_cls.from_crawler(crawler)
File "/usr/local/lib/python2.7/dist-packages/scrapy/middleware.py", line 50, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "/usr/local/lib/python2.7/dist-packages/scrapy/middleware.py", line 29, in from_settings
mwcls = load_object(clspath)
File "/usr/local/lib/python2.7/dist-packages/scrapy/utils/misc.py", line 39, in load_object
raise ImportError, "Error loading object '%s': %s" % (path, e)
ImportError: Error loading object 'scrapy_project.pipelines.storage.storage': No module named dydict.models
Why cannot scrapy access django application models (given that app1 in the installed_app ) ?
In the pipelines you don't import django models, you use scrapy models bounded to a django model.
You have to add Django Settings at scrapy settings, not after.
To use django models in scrapy project you have to use django_Item
https://github.com/scrapy-plugins/scrapy-djangoitem (import to your pythonpath)
My recommended file structure is:
Projects
|-DjangoScrapy
|-DjangoProject
| |-Djangoproject
| |-DjangoAPP
|-ScrapyProject
|-ScrapyProject
|-Spiders
Then in your scrapy project you hace to add pythonpath ull path to the django project:
**# Setting up django's project full path.**
import sys
sys.path.insert(0, '/home/PycharmProject/scrap/DjangoProject')
# Setting up django's settings module name.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'DjangoProject.settings'
Then in your items.py you cand bound your Django models to scrapy models:
from DjangoProject.models import Person, Job
from scrapy_djangoitem import DjangoItem
class Person(DjangoItem):
django_model = Person
class Job(DjangoItem):
django_model = Job
Then u can use the .save() method in pipelines after yeld of an object:
spider.py
from scrapy.spider import BaseSpider
from mybot.items import PersonItem
class ExampleSpider(BaseSpider):
name = "example"
allowed_domains = ["dmoz.org"]
start_urls = ['http://www.dmoz.org/World/Espa%C3%B1ol/Artes/Artesan%C3%ADa/']
def parse(self, response):
# do stuff
return PersonItem(name='zartch')
pipelines.py
from myapp.models import Person
class MybotPipeline(object):
def process_item(self, item, spider):
obj = Person.objects.get_or_create(name=item['name'])
return obj
I have a repository with the minimal code working: (you just have to set the path of your django project in scrapy settings)
https://github.com/Zartch/Scrapy-Django-Minimal
in:
https://github.com/Zartch/Scrapy-Django-Minimal/blob/master/mybot/mybot/settings.py
You have to change my Django Project path to your DjangoProject path:
sys.path.insert(0, '/home/zartch/PycharmProjects/Scrapy-Django-Minimal/myweb')
Try:
from .. models import MyModel
OR
from ... models import MyModel
Every dot represent the location

Categories

Resources