Forums

Error:ModuleNotFoundError: No module named 'wsgi' when trying to configure a new website?

I tried to have a new website. Here is the relevant structure of files:

microblog / wsgi.py
microblog / app / main

wsgi.py:

import os

from dotenv import load_dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path)

from app.main import bp

/var/www_my_domian_com_wsgi.py configuration as follows:

import sys

path = '/home/my_username/microblog'
if path not in sys.path:
    sys.path.append(path)

from wsgi import bp as application

but I tried to visit the website domain after all step is completed, an error happend:

error.log

2023-11-19 07:54:16,271: Error running WSGI application
2023-11-19 07:54:16,273: ModuleNotFoundError: No module named 'wsgi'
2023-11-19 07:54:16,273:   File "/var/www/my_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-19 07:54:16,273:     from wsgi import bp as application
2023-11-19 07:54:16,273: ***************************************************
2023-11-19 07:54:16,273: If you're seeing an import error and don't know why,
2023-11-19 07:54:16,274: we have a dedicated help page to help you debug: 
2023-11-19 07:54:16,274: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-19 07:54:16,274: ***************************************************
2023-11-19 08:08:27,926: Error running WSGI application
2023-11-19 08:08:27,927: TypeError: 'Blueprint' object is not callable
2023-11-19 08:08:54,589: Error running WSGI application
2023-11-19 08:08:54,590: TypeError: 'Blueprint' object is not callable

I have tried to debug using some way. For example, I runned python wsgi.py on command line, as a result, no error reports. I used python -i /var/www_my_domain_com_wsgi.py, no error reports likewise. And Everything is running successfully on local host . Can anyone give a favor?

Did you follow https://help.pythonanywhere.com/pages/DebuggingImportError/ If so, what was the result?

Indeed, I have followed the debugging instruction in such above link, but nothing reports, I have adjusted the markdown format to be readable. By the way, I do not know why the intial markdown format created by html tags like <pre><code> was in a mess.

My question is that I have wsgi.py in microblog working directory, but the error still send such message.

I don't think that the error "ModuleNotFoundError: No module named 'wsgi'" is the problem here -- if you check out the timestamps in the log file, you were getting that at 07:54 server time, but then later, at 08:08 (which is 48 minute before you posted here in the forums) it changed to this:

2023-11-19 08:08:27,926: Error running WSGI application
2023-11-19 08:08:27,927: TypeError: 'Blueprint' object is not callable

That means that by that later point, the issue with the wsgi module must have been resolved. Instead, it's complaining that you have some code that is trying to call an object of type "Blueprint", which is presumably happening in your "main" module. Do you have code that's catching and printing exceptions? Normally I'd expect an error like that to be accompanied by a stack trace so that you can see where it's happening.

Indeed, I have called blueprint in api submodule, The code are similar:

from flask import Blueprint

bp = Blueprint('api', __name__)

from app.api import users, errors, tokens

It runs successfully on flask local host, Just before a few seconds, It gave me more info when web url was entered

error.log

2023-11-19 07:54:16,271: Error running WSGI application
2023-11-19 07:54:16,273: ModuleNotFoundError: No module named 'wsgi'
2023-11-19 07:54:16,273:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-19 07:54:16,273:     from wsgi import bp as application
2023-11-19 07:54:16,273: ***************************************************
2023-11-19 07:54:16,273: If you're seeing an import error and don't know why,
2023-11-19 07:54:16,274: we have a dedicated help page to help you debug: 
2023-11-19 07:54:16,274: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-19 07:54:16,274: ***************************************************
2023-11-19 08:08:27,926: Error running WSGI application
2023-11-19 08:08:27,927: TypeError: 'Blueprint' object is not callable
2023-11-19 08:08:54,589: Error running WSGI application
2023-11-19 08:08:54,590: TypeError: 'Blueprint' object is not callable
2023-11-19 08:19:54,446: Error running WSGI application
2023-11-19 08:19:54,448: ModuleNotFoundError: No module named 'dotenv'
2023-11-19 08:19:54,448:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-19 08:19:54,450: we have a dedicated help page to help you debug: 
2023-11-19 08:19:54,450: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-20 11:11:09,496: Error running WSGI application
2023-11-20 11:11:09,502: ModuleNotFoundError: No module named 'dotenv'
2023-11-20 11:11:09,502:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-20 11:11:09,502:     from wsgi import bp as application
2023-11-20 11:11:09,503: 
2023-11-20 11:11:09,503:   File "/home/JTPing/microblog/./wsgi.py", line 3, in <module>
2023-11-20 11:11:09,503:     from dotenv import load_dotenv
2023-11-20 11:11:09,503: ***************************************************
2023-11-20 11:11:09,503: If you're seeing an import error and don't know why,
2023-11-20 11:11:09,504: we have a dedicated help page to help you debug: 
2023-11-20 11:11:09,504: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-20 11:11:09,504: ***************************************************
2023-11-20 11:11:10,446: Error running WSGI application
2023-11-20 11:11:10,447: ModuleNotFoundError: No module named 'dotenv'
2023-11-20 11:11:10,447:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-20 11:11:10,447:     from wsgi import bp as application
2023-11-20 11:11:10,447: 
2023-11-20 11:11:10,447:   File "/home/JTPing/microblog/./wsgi.py", line 3, in <module>
2023-11-20 11:11:10,447:     from dotenv import load_dotenv
2023-11-20 11:11:10,448: ***************************************************
2023-11-20 11:11:10,448: If you're seeing an import error and don't know why,
2023-11-20 11:11:10,448: we have a dedicated help page to help you debug: 
2023-11-20 11:11:10,448: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-20 11:11:10,448: ***************************************************
2023-11-21 11:01:33,951: Error running WSGI application
2023-11-21 11:01:33,955: ModuleNotFoundError: No module named 'dotenv'
2023-11-21 11:01:33,955:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-21 11:01:33,955:     from wsgi import bp as application
2023-11-21 11:01:33,955: 
2023-11-21 11:01:33,955:   File "/home/JTPing/microblog/./wsgi.py", line 3, in <module>
2023-11-21 11:01:33,955:     from dotenv import load_dotenv
2023-11-21 11:01:33,955: ***************************************************
2023-11-21 11:01:33,956: If you're seeing an import error and don't know why,
2023-11-21 11:01:33,956: we have a dedicated help page to help you debug: 
2023-11-21 11:01:33,956: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-21 11:01:33,956: ***************************************************
2023-11-21 11:01:38,492: Error running WSGI application
2023-11-21 11:01:38,493: ModuleNotFoundError: No module named 'dotenv'
2023-11-21 11:01:38,493:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-21 11:01:38,493:     from wsgi import bp as application
2023-11-21 11:01:38,493: 
2023-11-21 11:01:38,493:   File "/home/JTPing/microblog/./wsgi.py", line 3, in <module>
2023-11-21 11:01:38,493:     from dotenv import load_dotenv
2023-11-21 11:01:38,494: ***************************************************
2023-11-21 11:01:38,494: If you're seeing an import error and don't know why,
2023-11-21 11:01:38,494: we have a dedicated help page to help you debug: 
2023-11-21 11:01:38,494: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-21 11:01:38,494: ***************************************************
2023-11-21 11:01:53,633: Error running WSGI application
2023-11-21 11:01:53,634: ModuleNotFoundError: No module named 'dotenv'
2023-11-21 11:01:53,634:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-21 11:01:53,635:     from wsgi import bp as application
2023-11-21 11:01:53,635: 
2023-11-21 11:01:53,635:   File "/home/JTPing/microblog/./wsgi.py", line 3, in <module>
2023-11-21 11:01:53,635:     from dotenv import load_dotenv
2023-11-21 11:01:53,635: ***************************************************
2023-11-21 11:01:53,636: If you're seeing an import error and don't know why,
2023-11-21 11:01:53,636: we have a dedicated help page to help you debug: 
2023-11-21 11:01:53,636: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-21 11:01:53,636: ***************************************************
2023-11-21 11:02:21,901: Error running WSGI application
2023-11-21 11:02:21,902: ModuleNotFoundError: No module named 'dotenv'
2023-11-21 11:02:21,902:   File "/var/www/jtping_pythonanywhere_com_wsgi.py", line 44, in <module>
2023-11-21 11:02:21,902:     from wsgi import bp as application
2023-11-21 11:02:21,902: 
2023-11-21 11:02:21,902:   File "/home/JTPing/microblog/./wsgi.py", line 3, in <module>
2023-11-21 11:02:21,903:     from dotenv import load_dotenv
2023-11-21 11:02:21,903: ***************************************************
2023-11-21 11:02:21,903: If you're seeing an import error and don't know why,
2023-11-21 11:02:21,903: we have a dedicated help page to help you debug: 
2023-11-21 11:02:21,903: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-11-21 11:02:21,903: ***************************************************

I have tried this on base console, the code from dotenv import load_dotenv works as follows:

>>> from dotenv import load_dotenv
>>> import os

Looks like you are running your app in the environment where python-dotenv is not installed

I had python-dotenv installed, to confirm this, pip show python-dotenv shows exact package messages.

And I dout the statement from app.main import bp is true or not in wsgi.py, or it should be from app import app where app is returned by create_app function in app / __init__.py rather than blueprint?

It's possible that it is installed with a different version of python than the one you're using in your web app.

Yes, you are right, I have python version 3.9 in my own web app, and happen to find bash consle using python 3.10.5, what should I do?

I'd use virtualenvs for all your projects if you arent already - https://help.pythonanywhere.com/pages/Virtualenvs/

Sorry, I have already used virtualenv on my web app

Then make sure you have installed the packages that you need into the virtualenv that you are using to run your web app and, in the console, activate the virtualenv.

I have activated virtualenv in bash console, I'm sure each package including python-dotenv was installed. but i do not understand why such above error stiil reports.

Are you checking the bottom of your error log? It's possible that your app has more issues than a missing import. If so, new errors are appended at the end of the error log.

Yes, it reported that TypeError: Blueprint is not callable , Meanwhile no such error reports on my local host, I am confused.

Do you have a file called "Blueprint.py", or perhaps "flask.py"?

No, the only thing associated with blueprint in the whole directory is like that:

from flask import Blueprint

bp = Blueprint('api', __name__)

from app.api import users, errors, tokens

from app.main import bp
those code are listed above at the top of question.

Can we take a look at your files? We can see them from our admin interface, but we always ask for permission first.

Yes, you can, actually it is public and crude, Actually I can visit my logging page now, but a predefined error message reports: "The administrator has been notified. Sorry for the inconvenience!

So I then tried to use flask db upgrade, but the following error comes:

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade dc96d9b0d0bf -> e949960c17e9, let user be able to follow and unfollow
Traceback (most recent call last):
  File "/home/JTPing/microblog/venv/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
    self.dialect.do_execute(
  File "/home/JTPing/microblog/venv/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: table user already exists

It sounds like your migrations have got out of sync somehow. Do you have anything important in your app.db database right now? If not, it might be easiest to just delete it and then run the migration again.

Is there only this way to solve it? I had some posts on it, I do not want to delete those data

I found that new register is also not working, same error "The administrator has been notified. Sorry for the inconvenience!

I think I’m having a pretty similar issue

Error running WSGI application 2023-11-28 06:25:58,676: ModuleNotFoundError: No module named 'openai' 2023-11-28 06:25:58,676: File "/var/www/buzzy2427_pythonanywhere_com_wsgi.py", line 16, in <module> 2023-11-28 06:25:58,676: from flask_app import app as application # noqa 2023-11-28 06:25:58,676: 2023-11-28 06:25:58,677: File "/home/Buzzy2427/ai/flask_app.py", line 8, in <module> 2023-11-28 06:25:58,677: from openai import OpenAI

I def have openAI installed

@JTPing Are you getting the same error on a fresh database? @Buzzy2427 I believe we already answered you in a different thread, please keep the conversation in one place.

What should I do for deleting app.db database? delete all files of migrations and all data in app.db?

You'd just need to delete the file app.db itself -- or perhaps, for safety, just move it to a different location. Something like this would work:

mv app.db app.db.OLD

I runned command mv app.db app.db.OLD on bash console, but I am still getting same error

One puzzle for me is that I don't know why it runs smoothly on my local host but fail to work online

The most likely difference is that different migrations were applied to the different databases and the one on PythonAnywhere is not consistent.

I have only used one database flask-sqlalchemy on it and also installed celey, redis, rq.