miguelgrinberg / microblog

The microblogging application developed in my Flask Mega-Tutorial series. This version maps to the 2024 Edition of the tutorial.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
MIT License
4.48k stars 1.61k forks source link

Error: Could not import "app. microblog" #155

Closed kapoorkaran closed 4 years ago

kapoorkaran commented 5 years ago

Hi Can you help me with the following C:\Users\KaranKapoor\Desktop\microblog\app>flask run

Error: Could not import "app. microblog".

miguelgrinberg commented 5 years ago

@kapoorkaran it appears you have a space before microblog in your FLASK_APP variable. Make sure that space is removed when you set this environment variable.

Also, you seem to be in the app directory. You should be in the microblog directory, and microblog.py should be there as well, not inside app.

tomjjoy commented 4 years ago

I have a similar error. Please let me know what im doing wrong. Thanks!

(venv) Thomass-MacBook-Air:microblog thomasjoy$ flask run

Error: Could not import "microblog".

miguelgrinberg commented 4 years ago

@tomjjoy do you have a microblog.py file in the current directory?

nagarajnms commented 4 years ago

I see exactly the same issue too. I do have the microblog.py file in the current directory.

(venv) C:\Users\nagar\Desktop\microblog>flask run

Error: Could not import "microblog".

nagarajnms commented 4 years ago

Wonder if this has got anything to do with the Flask Version I'm using. I have Flask 1.0.2

miguelgrinberg commented 4 years ago

@nagarajnms open a python shell (just type python) and then enter import microblog in the prompt. Do you get any errors?

nagarajnms commented 4 years ago

Hey @miguelgrinberg , I figured out the error or rather the mistake I did. :) I Had the microblog.py inside the app package. I brought it one level up and the issue got fixed.! Thanks.

AB-Yusuf commented 4 years ago

Hey @miguelgrinberg , I figured out the error or rather the mistake I did. :) I Had the microblog.py inside the app package. I brought it one level up and the issue got fixed.! Thanks.

This worked!

AB-Yusuf commented 4 years ago

Hey @miguelgrinberg , I figured out the error or rather the mistake I did. :) I Had the microblog.py inside the app package. I brought it one level up and the issue got fixed.! Thanks.

For those that still did not understand the solution being mentioned here, he meant put your microblog.py in your microblog folder or directory not in the app directory. Move the microblog.py file from the app directory to the microblog directory.

flaskTutorial

AB-Yusuf commented 4 years ago

@nagarajnms and anyone that is exploring flask newly like me. Let's communicate and build together using the microblog as a starting point. You can Dm me via twitter:@MaestroAby

CodeWarriors0 commented 4 years ago

!Thanks to the developer! Error: Usage: flask db init [OPTIONS] Error: Could not import "microblog".

解决方法: 给英文不好的朋友留下一个解决方法,【flask db init】这个指令需要在你的项目的根目录下运行,例如你的项目路径是 【C:/migrations】,而你的终端开启的时候路径默认是【C:/】你就需要使用CD migrations进入它根目录来运行该指令

ghost commented 4 years ago

set FLASK_ENV=development if this problem still exists try the above method, including all other answers . It worked for me

RutujaGurav07 commented 3 years ago

Hey @miguelgrinberg , I figured out the error or rather the mistake I did. :) I Had the microblog.py inside the app package. I brought it one level up and the issue got fixed.! Thanks.

thanks it worked !!

saraems commented 3 years ago

I have this problem as well. My react project contains api-rest folder for the backend. But Heroku scresms: Error: Could not import "api".

Screenshot 2021-04-09 at 13 55 11
miguelgrinberg commented 3 years ago

@saraems your api-rest folder needs to be in the Python import path. Did you set this up?

saraems commented 3 years ago

Hm, well yes. But I am not sure if they are in a conflict Dockerfile vs .flaskenv. It works locally with no issues. Screenshot 2021-04-09 at 17 00 51

Screenshot 2021-04-09 at 17 01 13
miguelgrinberg commented 3 years ago

@saraems This is on Heroku, right? Then the Dockerfile and .flaskenv have nothing to do with it, what matters is what's on your Procfile, which I assume has a gunicorn command? You really haven't answered my question. You have your main Flask file in a sub-directory. Have you configured Python to have that sub-directory in the import path? If you didn't, then import api is not going to work.

saraems commented 3 years ago

I think I didn't answer you @miguelgrinberg because I didn't understand the question. Yes, I am trying to deploy on Heroku. My Procfile: web: gunicorn api:app Is is the configuration for Python heroku config:add PYTHONPATH=api-rest ? If so I just added. Or it's something else ?

saraems commented 3 years ago

Ok. Heroku sees my api.py now. I still have some troubles with PORT Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch. Thank you @miguelgrinberg I've been struggling with that since last week.