I am having trouble running my run.py file. My file structure looks like this:
With another python file called 'run.py' located in flask/bin along with python3. My run.py file is simply:
#!flask/bin/python3
from app import app
app.run(debug=True)
However running 'python3 run.py' throws the error:
$ python3 run.py
Traceback (most recent call last):
File "run.py", line 2, in <module>
from app import app
ModuleNotFoundError: No module named 'app'
app.py looks like:
from flask import Flask
app = Flask(__name__)
from app import views
I am confused about how to solve this as I have been messing with the directories such as putting app.py into the flask/bin folder and putting it outside of all folders shown in my directory above, but these methods have not worked for me.
your run.py is not able to import app as it can not see app within the bin folder, what happens with python is that all python files are treated as modules and the folders with an init.py file are treated as packages so run.py will start looking for the app package to import the app module however it will search within the bin directory. Read through Python documentation to fully understand the modules and packages. for now you might want to reorganize your application directory to look like this:
dir app
file app.py
dir flask
file run.py
By ensuring that run.py and app directory are at the same level in the directory run.py will be able to import from app now.
I hope that helps
Related
I am running a Flask server in Docker and cannot import create_app from app.py into my integration tests. I've tried a variety of naming approaches but Python is unable to find app.py.
The directory structure is as follows
/server
/test/integration/test.py
app.py
test.py has this import
from app import create_app
I tried relative imports as well but there was a "parent" error. I then played around with empty __init__.py files in an attempt to use relative imports. That didn't work. I am not sure why this is so involved to do really. What is the solution for this?
In test.py add:
# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/app/folder')
import app
from app import create_app
This add the directory of the source file to the system so Python will know to go there when searching the things to import. This is true when both files are local on your computer. If there is a server you need to see how you modify the in-server Python environment to know the folder of the app.py file.
See: Importing files from different folder
You can try with relative imports
from ..app import create_app
the error im getting: ImportError: attempted relative import with no known parent package
My folder structure:
-Backend
__init__.py
run.py
procfile
__init.py:
has the create app method
run.py:
from . import create_app
if __name__ == "__main__":
app = create_app("config")
app.run(debug=True)
procfile:
web: gunicorn run:app
EDIT:
I rearranged the app structure by which:
Backend
init.py
procifle
extensions.py
src
init.py
run.py
init.py:
empty
src/init.py:
from ..extensions import db,migrate
has the create app method
src/run.py:
from src import create_app
if __name__ == "__main__":
app = create_app("config")
app.run(debug=True)
so now the new error is:
from ..extensions import db
ImportError: attempted relative import beyond top-level package
Edit #2:
another thing to point out is, that suppose in the run.py i do the following:
from Backend import create_app()
I get the following error: no module named "Backend" why could that be?
Has anyone faced a similar issue and what can I do to solve it?
Update based on new information I think we can get you home.
1.) Move run.py next to the procfile (same dir). That "run file" should be in that top level dit at /app/run.py in Heroku.
A good basic pattern for file organization is to "module" everything (a dir with a __init__.py file). Then all imports are from the top level run.py [where the process is running from] walking subfolders. As long as you traverse down into imports things stay pretty stable. If you are trying to navigate up dirs it becomes problematic.
Resolve any imports, it can be tricky at first but soon becomes a way of thinking.
2.) Move extensions.py into src (for now) and import from src import foo
The following is the pattern in all my active Django and similar apps running in Heroku. Same layout I have been using for many years. In the following screenshot manage.py is the Django equivalent of your run.py. I am not using the docker image on this app, so that is not in the equation, just code and dependencies.
Your problems are totally understandable, it's easy to take for granted all the times other Python devs do this.
I'm going through the Flask mega-tutorial with the following project structure
my-project/
app/
__init__.py
routes.py
config.py
With my __init__.py file :
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
from app import routes
While running __init__.py from Spyder, I had the following error
ModuleNotFoundError: No module named 'config'
I'm new to Flask and Python, and the problem must be that python, run from Spyder, doesn't find the config.py file in the top-level directory. Would you have any recommendations on how to setup Spyder or structure my project? Thank you
ps : the project ran fine when running Flask run from the command line, but I'd like to use Spyder for debug functionalities
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 suggests the following file layout:
runflaskapp.py
flaskapp/
__init__.py
runflaskapp.py contains:
from flaskapp import app
app.run(debug=True)
flaskapp/init.py contains:
from flask import Flask
app = Flask(__name__)
Running this with 'python3 runflaskapp.py' works fine. However it
seems to me that a more Python3onic way of doing this would be to
rename 'runflaskapp.py' as 'flaskapp/main.py' and then run
the whole thing as 'python3 -m flaskapp'. Unfortunately this doesn't
work:
$ python3 -m flaskapp
* Running on http://127.0.0.1:5000/
* Restarting with reloader
Traceback (most recent call last):
File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
from flaskapp import app
ImportError: No module named 'flaskapp'
Does anyone know why and how to fix it?
The -m flag does this:
Search sys.path for the named module and execute its contents as the __main__ module… When a package name is supplied instead of a normal module, the interpreter will execute <pkg>.__main__ as the main module.
In other words, flaskapp is not imported as flaskapp, but as __main__, just like a script would be, and then its __main__ module gets executed.
This means from flaskapp import app will not work, because there is nothing named flaskapp.
But the relative import from . import app will. So, as long as there are no absolute imports anywhere in flaskapp except in your new __main__.py file, that one-liner should do it.