flask run importerror raised - python

I was running flask app using app.run till now. But when I was trying to use flask-migrate, it throws an error when I am trying 'flask run'
File directory is like this:
project/
├─ project/
│ ├─ static/
│ │ ├─ app.js
│ ├─ templates/
│ │ ├─ base.html
│ ├─ routes.py
│ ├─ __init__.py
│ ├─ forms.py
│ ├─ models.py
├─ run.py
├─ setup.py
├─ MANIFEST.in
I was following documentation, but it did not work. 'python run.py' works but 'flask run' is not working.
export FLASK_APP=project
export FLASK_ENV=development
Error: While importing 'project', an ImportError was raised.
How can I solve this problem?

try, to change the FLASK_APP value
export FLASK_APP=run
...
As far as I understood from the Flask docs - the FLASK_APP value needs to be the same as the "main app" filename (just without the .py)
docs for ref: https://flask.palletsprojects.com/en/2.0.x/quickstart/#a-minimal-application3

Related

Pytest - inject test from outside the module being tested

├─ alpha
|
├─ beta_folder
│ ├─ __init__.py
| ├─ main.py
│ └─ beta.py
├──── conftest.py
└────gamma_folder
├─ __init__.py
├─ main.py
└─ gamma.py
How can I, when testing gamma_folder module call a single test from beta_folder module using pytest?
I've tried injecting a function through hooks, but never successful.

Define setuptools path for each import module

Given the following project structure:
root
└── dir1
├─ setup.py
├─ script1.py
├── processor
│ ├─ preprocessor.py
│ └─ postprocessor.py
│
├──src_dir
│ ├─ predictor.py
│ └─ script2.py
│
I would like the custom package to ONLY contain the two scripts inside the processor folder and the predictor.pyscript. Moreover, the scripts should be accessible via from processor.preprocessor import ... while the predictor to be in the main root and therefore importable as from predictor import ....
I I should then run the following setup script: python setup.py sdist --formats=gztar:
from setuptools import setup
setup(name="processors",
version="0.1",
...
)

Is there something like esbuild for serverless python?

I come from working with nodejs and serverless framework, where I can configure esbuild so that functions that require other functions are in a single bundle file.
Now I am working python with serverless framework, however, when I import an external function, and deploy. In the function bundle the whole project is uploaded. Clearly this would increase costs and delay the startup of the functions.
I have been reading about mock plugins serverless-python-individually, serverless-package-python-functions but i have not been able to package the function and its dependencies individually.
Is there any way to package the dependencies of the lambda functions individually?
example:
In nodejs I have the following structure:
api/
├─ v1/
│ ├─ handler/
│ │ ├─ fileHandler.js
│ ├─ anotherlambda/
│ │ ├─ index.js
│ ├─ s3Storage/
│ │ ├─ index.js
serverless.yml
When I deploy, it looks like this:
api/
├─ v1/
│ ├─ handler/
│ │ ├─ fileHandler.js
Where in fileHandler.js the code of the s3Storage/index.js dependency is inserted.
In python I have the following structure:
api/
├─ v1/
│ ├─ handler/
│ │ ├─ fileHandler.py
│ ├─ anotherlambda/
│ │ ├─ hello.py
│ ├─ s3Storage/
│ │ ├─ index.py
I would like to know if there is a way to package it in the same way as with nodejs esbuild.

Getting ImportError Using "flask run"

I'm creating a Flask RESTful API. Here's the project structure:
│ .env
│ .flaskenv
│ .gitignore
│ <App Name>.code-workspace
│ <App Name>.py
│ dev_start
│ LICENSE
│ Pipfile
│ Pipfile.lock
│ README.md
│
├───<App Name>_api
│ errors.py
│ settings.py
│ __init__.py
│
├───crypto
│ password.py
│ __init__.py
│
├───models
│ blacklisted_token.py
│ company.py
│ user.py
│ __init__.py
│
├───resources
│ company.py
│ messages.py
│ user.py
│ __init__.py
│
└───schemas
company.py
user.py
__init__.py
Here's the .flaskenv file:
FLASK_APP=<App Name>:create_app()
FLASK_ENV=development
FLASK_DEBUG=1
FLASK_RUN_PORT=5000
FLASK_RUN_HOST=127.0.0.1
The "App Name" folder's "init.py" file has the "create_app" function defined. When I try to do a "pipenv run flask run" I get the following error:
Loading .env environment variables...
* Serving Flask app '<App Name>' (lazy loading)
* Environment: development
* Debug mode: on
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing '<App Name>', an ImportError was raised.
What's really confusing is that there isn't an explanation of the ImportError.
Thanks in advance for any help. I've been searching for hours trying to find a solution to this.
In your .ENV:
FLASK_APP=<App Name>:create_app()
Try to replace with:
FLASK_APP=<App Name>:create_app
The possible reason is when you load the module you need to point on app variable or callable that return app.

cannot import name 'a' from 'tools'

I can run test_pkg1.py and test_pkg2.py
but when I run test.py
Error occurred at test_pkg1.py
Exception has occurred: ImportError
cannot import name 'a' from 'tools' (unknown location)
root
├─ pkg1
│ ├─ tools
│ │ ├─ __init__.py
│ │ └─ a.py
│ ├─ __init__.py.py
│ └─ test_pkg1.py
├─ pkg2
│ ├─ tools
│ │ ├─ __init__.py
│ │ └─ b.py
│ ├─ __init__.py.py
│ └─ test_pkg2.py
├─ test.py
└─ d.py
test_pkg1.py
from tools import a
test_pkg2.py
from tools import b
test.py
from pkg2 import test_pkg2
from pkg1 import test_pkg1
In Python, import paths are always relative to the directory where the main program is executed unless you explicitly use a relative import by preceding the package name with a dot, which then makes the interpreter look for the package in the same directory as the module where the import is made:
test_pkg1.py:
from .tools import a
You also need to create an __init__.py file in the directory where the main program runs in order for that directory to be considered a package under which relative imports can be made.

Categories

Resources