python no module named 'A' __init__ vs package - python

I have looked up several answers on google but none of them made my problem correct.
I made my python file in directory A
in my __init__.py I have app = Flask(__name__)
When I want to get app from other file I use:
from A import app
but error says no module named 'A' so I changed to
from __init__ import app
now it works
Can anyone tell me why from A import app does not work?

Related

python flask import. import error unknown location

i am trying to import create_app from init.py that is located in the website file
but everytime I try to run the code I get
ImportError: cannot import name 'create_app' from 'website' (unknown location)
this is my files
.vscode
env
website
--static
--templates
--__init__.py
--auth.py
--views.py
--models.py
main.py
init.py
from flask import Flask
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hello'
return app
main.py
from website import create_app
app = create_app
if __name__ == '__main__':
app.run(debug=True)
thought this is the only method that isn't working I tried this method to check if the error is from vscode but it is just from this method
I tried
app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "hello Flask"
and when I write in the terminal `python -m flask run
I would get a website that says "hello Flask"
but when I press the run icon I get nothing
unlike the first one if I run it I would get an import error unknown location
and if I use python -m flask run I would get
Error: could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module not found in the current directory.
thought everything is sync to Github
in both of them I am working in a 'script' environment
A fix I came up with was adding name to the function defining create_app inside the init file.
Example:
from flask import Flask
def create_app(name):
app = Flask(name)
When importing, try from website.templates import create_app. So basically after you put it to import from website, add . following the sub-folder that you desired to import from (in this case it is templates).
I ran into the same thing, but it appears that these files "models, views, etc" isn't in website folder, they are inside static folder which is in website folder, that's why it's not working.
I just fixed them manually.
For someone getting this error later (like myself), be sure to check that your main package's
__init__.py
file is in the correct place.
You forgot create environment variable.
if you using mac or linux export FLASK_APP=main.py
if you windows set FLASK_APP=main.py
And last in main.py should be app = create_app()
try this
in your main.py
from website.init import create_app
or
change file init to init.py
then in your main.py
from website._init_ import create_app

Module import Error and NameError in Flask using Heroku

I do have this issue when deploying a small app to heroku, the requirements.txt is very minimal, the app crash with Cannot import module 'NewsApiClient' when using
from newsapi import NewsApiClient
and the app actually start with a 500 Internal Error when using from newsapi import *
NameError: name 'NewsApiClient' is not defined
newsapi = NewsApiClient(api_key='xxxxxxxxxx')
What I've done so far is create an empty init file on the root folder
It appears you are importing the incorrect package in your requirements.txt file. You have imported the newsapi package which is different then the python-newsapi package.
Changing the newsapi==0.1.1 to newsapi-python==0.2.3 should fix your problem with the importing error as long as you reinstall the package dependencies.

How to import python files on Google app engine project?

I recently started playing with Google app engine (GAE) and I'm failing to do something very simple.
The GAE provides a main.py file which is where I'm trying to import a file I created util.py.
I saw some examples saying that you need to create a module for that in order to be able to include it.
So I did
main.py
utilities/
__init__.py
util.py
In main.py I imported import utilities.util, I still can't call the functions inside of util.py.
Any ideas where I could learn more about these things?
import utilities.util as myutil
myutil.function_name() #Using the module name you can access the functions
from utilities.util import function_name()
function_name()
Ref: https://docs.python.org/3/tutorial/modules.html#modules

How to run flask app after reorganization as a package

I'm trying to restructure a flask app to a package based on http://flask.pocoo.org/docs/0.10/patterns/packages/. My app is based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 . I've now changed it to the screenshot above and am trying to run at command line.
runserver.py:
from app import intro_to_flask
if __name__ == '__main__':
intro_to_flask.routes.run(debug=True)
At the command line:
/mini/app (master) $ python runserver.py
Traceback (most recent call last):
File "runserver.py", line 1, in <module>
from app import intro_to_flask
ImportError: No module named app
What am I doing wrong?
Since the Flask documentation mentions only one __init__.py file in the subdirectory with the Flask application and you have two of them, I think you're confused by the __init__.py file and the usage of imports. In your case the subdirectory with the Flask application is intro_to_flask.
Basically it works like this: from <module> import <object>
The module is a .py file and the object is defined inside the module. In this special case there is a __init__.py file so the module you have to reference to has the name of the directory that contains the __init__.py file.
Assuming your app/intro_to_flask/__init__.py looks like this:
from flask import Flask
app = Flask(__name__)
import intro_to_flask.routes
Your app/runserver.py should look like this:
from intro_to_flask import app
app.run(debug=True)
intro_to_flask is the imported module, app is a public object defined inside the imported module.
I believe you're looking for from intro_to_flask import app if I read the links you posted correctly, and assuming you've setup __init__.py correctly inside the intro_to_flask folder.

Flask Blueprint AttributeError: 'module' object has no attribute 'name' error

My API is being built to allow developers to extend it's functionality. My plan is to do this by providing an "extensions" directory where they can drop in Blueprints and they will be dynamically loaded. This is the code I am utilizing to import (modifed from this tutorial)
from flask import Flask
import pkgutil
import sys
app = Flask(__name__)
EXTENSIONS_DIR = "extensions"
modules = pkgutil.iter_modules(path=[EXTENSIONS_DIR])
for loader, mod_name, ispkg in modules:
if mod_name not in sys.modules:
# It imports fine
loaded_mod = __import__(EXTENSIONS_DIR+"."+mod_name+"."+mod_name, fromlist=[mod_name])
# It does not register
app.register_blueprint(loaded_mod)
This is the directory layout of my project. The extensions directory is where developers drop in their expanded functionality.
/root
/extensions
/extension1
__init__.py
extension1.py
/extension2
__init__.py
extension2.py
simple_example.py
The problem is that I get this error and am not sure what it is telling me.
>python simple_example.py
Traceback (most recent call last):
File "simple_example.py", line 14, in <module>
app.register_blueprint(loaded_mod)
File "C:\Python27\lib\site-packages\flask\app.py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\flask\app.py", line 880, in register_blueprint
if blueprint.name in self.blueprints:
AttributeError: 'module' object has no attribute 'name'
A simple extension looks like this
from flask import Blueprint
extension1 = Blueprint('extension1', __name__)
#extension1.route("/my_route")
def treasure_list():
return "list of objects"
How do I solve the AttributeError in a way that allows my app.register_blueprint call to succeed?
You are trying to register the module and not the contained Blueprint object.
You'll need to introspect the module to find Blueprint instances instead:
if mod_name not in sys.modules:
loaded_mod = __import__(EXTENSIONS_DIR+"."+mod_name+"."+mod_name, fromlist=[mod_name])
for obj in vars(loaded_mod).values():
if isinstance(obj, Blueprint):
app.register_blueprint(obj)
When I got this error my code looked like this:
from blueprints import api
...
app.register_blueprint(api)
I fixed this by doing this:
app.register_blueprint(api.blueprint)
I have also experienced the same effect in a project. The origin of the problem was an incorrect import of the blueprint file.
Make sure the import statement imports the real blueprint and not the module on which it is defined.
In other words, you may be doing
from .blueprint import blueprint
while you meant
from .blueprint.blueprint import blueprint
As a side recommendation, name the module on which the blueprint is defined with a different name than the blueprint itself, in order to clarify the import. An example:
from .blueprint.views import blueprint
I got an error saying AttributeError: 'Blueprint' object has no attribute 'register_blueprint'
For this I simply uninstalled Flask using pip uninstall flask
and then installed flask[async] pip install flask[async]
I was importing blueprint directly from flask import Flask. I resolved this error by installing flask again using pip3 install flask. Sometimes, if flask version is greater than or equal to 0.12.3 you might face this issue.
Changing the flask version will help. I was using Flask==1.1.2 and changing to Flask==2.0.2 resolves the error.
pip install Flask==2.0.2

Categories