Forums

Another import error

Hi, I already managed to get rid of all errors by searching the forum. But this one is kinda killing me.

In my file run.py which usually is peoples app.py I am initializing Flask-Mail by mail = Mail(app)

and in /home/appboost1/mysite/app/home/routes.py I am importing it by from run import mail, app which is in the path /home/appboost1/mysite/app/run.py.

On my local machine it is working.

But the server logs show following error, any idea/help appreciated!

2021-01-21 00:04:40,027: Error running WSGI application
2021-01-21 00:04:40,037: ImportError: cannot import name 'mail' from 'run' (/home/appboost1/mysite/run.py)
2021-01-21 00:04:40,037:   File "/var/www/appboost1_pythonanywhere_com_wsgi.py", line 16, in <module>
2021-01-21 00:04:40,037:     from run import app as application  # noqa
2021-01-21 00:04:40,037: 
2021-01-21 00:04:40,037:   File "/home/appboost1/mysite/run.py", line 28, in <module>
2021-01-21 00:04:40,037:     app = create_app(config_mode)
2021-01-21 00:04:40,037: 
2021-01-21 00:04:40,038:   File "/home/appboost1/mysite/app/__init__.py", line 81, in create_app
2021-01-21 00:04:40,038:     register_blueprints(app)
2021-01-21 00:04:40,038: 
2021-01-21 00:04:40,038:   File "/home/appboost1/mysite/app/__init__.py", line 23, in register_blueprints
2021-01-21 00:04:40,038:     module = import_module('app.{}.routes'.format(module_name))
2021-01-21 00:04:40,038: 
2021-01-21 00:04:40,038:   File "/home/appboost1/mysite/app/base/routes.py", line 14, in <module>
2021-01-21 00:04:40,038:     from app.home.routes import send_mail_async
2021-01-21 00:04:40,038: 
2021-01-21 00:04:40,039:   File "/home/appboost1/mysite/app/home/routes.py", line 26, in <module>
2021-01-21 00:04:40,039:     from run import mail, app
2021-01-21 00:04:40,039: ***************************************************
2021-01-21 00:04:40,039: If you're seeing an import error and don't know why,
2021-01-21 00:04:40,039: we have a dedicated help page to help you debug: 
2021-01-21 00:04:40,039: https://help.pythonanywhere.com/pages/DebuggingImportError/ 
2021-01-21 00:04:40,039: ***************************************************

[formatted by admin]

It looks like a circular import to me -- you're importing from app in run.py but the code in app.py leads to routes.py which again imports from run.py...

Hi thank you for you answer. actually there is no app.py in my project. My base project folder is called app and it is on the same dir level as run.py

If it would be circular I would get also an error on my local machine. But actually I dont. <br><br>

database.db
config.py    
run.py
app/
├── base/
   ├── .DS_Store
   ├── __init__.py
   ├── __pycache__/
      ├── __init__.cpython-37.pyc
      ├── feed_classes.cpython-37.pyc
      ├── forms.cpython-37.pyc
      ├── models.cpython-37.pyc
      ├── routes.cpython-37.pyc
      └── util.cpython-37.pyc
   ├── feed.py
   ├── feed_classes.py
   ├── forms.py
   ├── models.py
   ├── routes.py
   ├── static/
   └── util.py
├── firebase_utils/
   ├── firebase_key.json
   ├── get_current_app_data.py
   ├── onload_data.pickle
   ├── onload_data_cln.pickle
   ├── tag_mapping.py
   └── test.json
├── home/
   ├── .DS_Store
   ├── bkp_serverside_appboost_processing.py
   ├── firebase_utils.py
   ├── local_parser.py
   ├── routes.py
   └── templates/
       ├── ___index.html
       ├── ads.txt
       ├── app-news.html
       ├── blog-posts.html
       ├── index.html
       ├── new-post.html
       ├── page-403.html
       ├── page-404.html
       ├── page-500.html
       ├── page-blank.html
       ├── ui-components.html
       ├── ui-tables.html
       └── user-profile.html
└── setup.py

The name run.py suggests that that is what you are running on your local machine to get the dev server running. The fact that it's the entry point may mean that that imports are ok when the dev server is started using run and not when it is imported (as it is on PythonAnywhere).

Hi Glenn,

exactly, I am running run.py on my local machine which contains following:

from flask import ......
app = create_app(config_mode)
Migrate(app, db)
mail = Mail(app)

other stuff....

if __name__ == "__main__":

app.run()

What do I need to change in order to get it working on pythonanywhere.com?

You do not need to change anything as long as app.run() is under if __name__ == "__main__"

How does your wsgi file look like?

Thats the content of my wsgi file

import sys

# add your project directory to the sys.path
project_home = '/home/appboost1/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from run import app as application  # noqa

You import app from run, app imports routes, and routes try to import app from run again.

So shall I rename my base project folder name from app to something like myapp? I did not understand what to do to be honest

Perhaps we could take a look at your code? We can see it from our admin interface, but we always have to ask for permission first.

Hi Giles,

sure! Thank you very much for your support.

question- do you by any chance have more than one file or variable called run? and also when you are running locally, which directory and you doing the run command from? I would try to set that as your working directory on PythonAnywhere

If you take a look at the traceback, it is failing at

run.py, line 28, app = create_app(config_mode)

which eventually tries to do

2021-01-22 10:24:52,414:   File "/home/appboost1/mysite/app/home/routes.py", line 26, in <module>
2021-01-22 10:24:52,414:     from run import mail, app

However, in your run.py, you have

mail = Mail(app)

at line 39.

ie. at this point (line 28) mail doesn't exist yet.

That is why I'm wondering if for example you have multiple run.py's, or the import order or working directory was different when you were running locally.

Hi conrad.

I have doublechecked there is no other run.py or any variable called run.

I am running the project with following command: <br><br>

/Users/umuzo/Documents/appboost/env/bin/python /Users/umuzo/Documents/appboost/run.py

and this is the printed CWD in "if __ name__ == "__ main.py":

/Users/umuzo/Documents/appboost

The create_app function is located in __ init__.py and contains the following: <br><br>

def create_app(config, selenium=False):
    app = Flask(__name__, static_folder='base/static')

    app.config.from_object(config)
    if selenium:
        app.config['LOGIN_DISABLED'] = True
    register_extensions(app)
    register_blueprints(app)
    configure_database(app)
    configure_logs(app)
    apply_themes(app)
    return app

Does this helps you a bit?

I think it would be easier to debug seeing the files in-place -- however, there don't appear to be any in your account right now. Are you in the process of trying to deploy again?

Hi giles,

thank you for your efforts. I tried a lot stuff and just gave up. I moved everything to a cloud provider and it worked immediately.

Please don't get me wrong. I love pythonanywhere.com. I had several projects running here from time to time. But currently im in a little time delay and need to proceed asap :)

Fair enough. Thanks for letting us know!