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.
Related
I'm currently trying to import a module on Python but I'm receiving the following message:
Traceback (most recent call last):
File "test_db_conn.py", line 8, in <module>
from config import config_reader
ModuleNotFoundError: No module named 'config'
Here's the folder structure:
config
- __init.py
- config_reader.py
- config.ini
db_functions
- test_db_conn.py
And the code on 'test_db_conn.py':
# Import modules
from config import config_reader
import pymongo
# Query database and print result
db_client = pymongo.MongoClient(db_url)
db_status = db_client.test
print(db_status)
Are you able to help me with this?
Thank you in advance
You need to append the parent path of your project to the Python module search directory. I replicated your folder structure locally on my dev system, and was able to access a function named config_reader in the config_read.py module, within the config package.
import sys
import pathlib
parent_dir = pathlib.Path(__file__).parent.parent
sys.path.append(parent_dir.__str__())
from config.config_reader import config_reader
config_reader()
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?
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
)
...
Getting the lovely error:
ERROR 2015-09-23 13:14:12,500 cgi.py:122] Traceback (most recent call last):
File "public/run.py", line 2, in <module>
from src import app
File "public/src/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I've installed flask into public/lib with pip install -t lib -r requirements.txt.
public/appengine_config.py
from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder
vendor.add('lib')
# also does not work
# import os
# vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
public/app.yaml
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /static
static_dir: src/static
- url: /
script: run.py
public/run.py
from google.appengine.ext.webapp.util import run_wsgi_app
from src import app
run_wsgi_app(app)
public/src/__init__.py
from flask import Flask
# import settings
app = Flask('app')
# app.config.from_object('src.settings')
import views
Take a look at the Starter Project. More importantly, you should change your app.yaml to point to the the flask wsgi app.
- url: .*
script: src.app
The run.py script and run_wsgi_app() is the old way to run your app and should not be used.
You might also want to check out the gae-init project as it uses Flask and is built on GAE. I found it's a great way to get up and running quickly.
I gave my friends a project. and I use lots of 3rd part packages among the scripts.
How could I generate all the necessary packages automatically.
Otherwise,
my friend should run my script and catch the exception and install the missing package, again and again.
By the way, is it possible to install all the necessary packages at once ?
# -*- coding: utf8 -*-
from flask import request, url_for
from flask import json
from flask import Response
from flask import Flask, request, jsonify
from flask_request_params import bind_request_params
import datetime
import pandas as pd
import pymongo
import pdb
from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import urlparse
from mongo import Mongo
import yaml
import time, functools
from functools import wraps
from pdb import set_trace
from flask import g
from pandas_helper import PandasHelper
import errors
That’s what requirement files in pip are for:
"Requirements files" are files containing a list of items to be installed using pip install
Basically they document your dependencies, so when you do a pip install later, pip will get all the required dependencies to run your script.