ImportError: cannot import name 'app'

23,805

After being prompted by abarnert I realized that it wasn't a problem in the larger structure of Flask and Apache but simply with the structure of my import. So my solution that got it working was to make sure first that I did

from web_tool import web_tool as application

Because my project structure looks like

/path/web_tool/src/web_tool/
    web_tool.py
    web_tool.wsgi

I think naming the folder the same as the main python script the same confused but then I got it straight. Now the above import still didn't work because I got a module error but what did work was:

from web_tool import web_tool
application = web_tool.app

I didn't see this structure anywhere else online or in any other SO answers but that is probably because of my particular project structure.

Share:
23,805
clifgray
Author by

clifgray

PhD Student at Duke in Marine Science focused on satellite and drone based remote sensing for understanding the spatial and temporal variability of ocean biology and ecology. This includes lots of scientific computing, machine learning approaches to remote sensing analysis, and geospatial analysis. Good overview of general techniques here: https://github.com/patrickcgray/open-geo-tutorial Broad background in computer science and space systems. Lots of Python thrown in there.

Updated on July 19, 2022

Comments

  • clifgray
    clifgray almost 2 years

    I have been working on this for 5+ hours and it seems like it should be rather simple. I followed the instructions to set up a flask application on apache via mod_wsgi and after finishing them I am getting an error that

    ImportError: cannot import name 'app'
    

    from the line:

    from web_tool import app as application
    

    of my wsgi file that is called web_tool.wsgi and looks like:

    import sys  
    sys.path.insert(0, "/path/web_tool/src/")
    print(sys.version)
    print(sys.path)
    
    from web_tool import app as application
    

    My python version and system path are correct at least so I believe.

    And then my virtualhost file, which is stored in apache2/sites-available/default looks like:

    WSGIPythonHome /usr/local/opt/python-3.4.1
    
    <VirtualHost *:4000>
    
        ServerName my.servername.com
        WSGIDaemonProcess web_tool user=my_username group=my_group threads=5
        WSGIScriptAlias / /path/web_tool/src/web_tool/web_tool.wsgi
        WSGIApplicationGroup %{GLOBAL}
    
        <Directory /path/web_tool/src/web_tool/>
            WSGIProcessGroup cnt_tool
            Order deny,allow
            Allow from all
        </Directory>
    </VirtualHost>
    

    Do you see any errors, should my wsgi file be called the same as my python file? I'm running python 3.4 and mod_wsgi 3.4.

  • renatov
    renatov almost 9 years
    That worked for me too, but I had to add a blank __init__.py file in that folder in order to work.