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?
Related
I have an import error while coding a flask web application. Here are my imports:
from flask import Flask
from flask import session, request
from flask import render_template, redirect
from flask_sqlalchemy import sqlalchemy
Although the last line of code gives me the error:
from flask_sqlalchemy import sqlalchemy
I have successfully installed Flask, SQLAlchemy, and Flask-SQLAlchemy using pip, and I have checked whether these are installed on the system using
python -c "import Flask"
etc. These checks resulted in a 0, meaning that it was correctly installed. NOTE: I have already read other posts, and tried flask.ext.sqlalchemy, flaskext_sqlalchemy, etc. as import variations. Here are the installed packages in the sitepackages directory for python 2.7.
Flask_OAuth-0.12.dist-info httplib2
Flask_SQLAlchemy-2.3.2.dist-info httplib2-0.10.3.dist-info
SQLAlchemy-1.2.0.dist-info oauth2
flask_oauth.py oauth2-1.9.0.post1.dist-info
flask_oauth.pyc sqlalchemy
flask_sqlalchemy tests
I have also structured my files, so the imports are in the init.py file. My other files included views.py, config.py, run.py, and a templates folder containing html files.
What am I doing wrong here, and how is my problem different from others?
Module imports are indeed case sensitive.
The way to check this is to first confirm if it's possible to:
import flask_sqlalchemy
If that succeeds, the package is installed correctly. Once the package is confirmed to be installed correctly, then this should succeed.
from flask_sqlalchemy import SQLAlchemy
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.
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
)
...
I can run the server in flask development mode correctly.
I think I missing some params to run the uwsgi server so that got the error message.
run uwsgi command
uwsgi --socket 127.0.0.1:4245 --module web --callable app --processes 4 --threads 2
Error log
*** Operational MODE: preforking+threaded ***
Traceback (most recent call last):
File "./web.py", line 19, in <module>
from model.release_schedule import ReleaseSchedule
ImportError: No module named model.release_schedule
web.py
#!/usr/bin/env python3
# -*- coding: utf8 -*-
from flask import request, url_for
from flask import Flask, request, jsonify
from flask_request_params import bind_request_params
from flask import g
import datetime
import pandas as pd
import pymongo
import json
from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import yaml
import time, functools
from pdb import set_trace
from pandas_helper import PandasHelper
import errors
from app_helper import *
from model.release_schedule import ReleaseSchedule
from model.history import History
from model.report_type_symbol import ReportTypeSymbol
from model.weekly_history import WeeklyHistory
from mongo import Mongo
# load config file
APP_CFG = yaml.load(open("app.yml", "r"))
MSG = yaml.load(open("message.yaml", "r"))
You need to add an __init__.py in the model folder if you want to use it as a python package. It was probably working for in debug mode because the parent directory was in your PYTHONPATH.
I am created an api for OpenERP using bottle and when i try to configure wsgi with apache.
While importing in apache server log it shows
ImportError: No module named api
I checked for current directory it prints cwd and the import file is in same directory still it shows error
Here i uploaded my code for wsgi
import os
os.chdir(os.path.dirname(__file__))
import bottle
print os.getcwd()
import api as application
application = bottle.defaut_app()
What is your sys.path?
It must include the directory of your .py file, not the file name itself. E.g.,
sys.path.append('/var/www/api')
# and then, to see its new value:
print sys.path.append
Also note how I printed the value of sys.path there; the way you did it in one of your comments (printing the return value from append) is incorrect.
I think i had a similar problem and I ended up doing this in my wsgi:
import sys
import os
import bottle
sys.path.append('%path to module%')
import %modulename%
application = bottle.default_app()
In your py you have to import:
from bottle import default_app
for this to work.