Man Wa kileleshwa
Man Wa kileleshwa

Reputation: 459

Module errors in Flask Application structure

Hello I am working in Windows and struggling to understand how to set up my flask application so that I can run it from the command line. Inspiration from here. My actual directory structure looks as follows:

/yourapplication   
        __init__.py
        config.py
        views.py
        start_server.py
        /static
            style.css
        /templates
            layout.html
            index.html
            login.html

my init.py file is as follows

from flask import Flask
app = Flask(__name__)

import yourapplication.views
app.config.from_object('config')

Because I have an init.py the "YourApplication" is a package as shown in Eclipse IDE

my config.py file is as follows

DEBUG=True

my setup.py file is as follows

from setuptools import setup

setup(
    name='yourapplication',
    packages=['yourapplication'],
    include_package_data=True,
    install_requires=[
        'flask',
    ],
)

my start_server.py file is as follows

from yourapplication import app

if __name__ == "__main__":
     app.run()

I can run the start_server.py from Eclipse (IDE) and the webserver starts, but when I cd into the "YOURAPPLICATION" folder from the Windows command line and run start_server.py I get "ImportError: No Module named YOURAPPLICATION"

  1. What mistake am I making in structuring my application?
  2. What mistake am I making in structuring my project?
  3. How do i get it to run from the command line?
  4. How do I package it so it can run on another machine (that doesn't have Eclipse)

I'm using Windows and Python 2.7

Update One (following from the 1st answer)

Just so I am checking that I am doing it right, I going to state step by step what I've done. I have gone into the folder above /yourapplication that directory looks like

/projects_folder
    /yourapplication
    /folder1
    /myfolder2

From the projects folder (Windows CMD line) I have done the following:

c:\joebloggs\projects_folder>

    set FLASK_APP=yourapplication
    set FLASK_ENV=development
    pip install -e .
    flask run

Still get the same error. I do wonder in the linked article, where is the main? I cannot understand where the entry point would be to start the web application. In my case i have start_server.py. Which I run in my Eclipse IDE. Should this file be mentioned in the setup.py?

Just for testing, should I not be able to simply run start_server.py from the command line to get the application to run (that is without doing all the pip install -e and FLASK_APP=yourapplication stuff?

Upvotes: 1

Views: 116

Answers (1)

Srikanth Chekuri
Srikanth Chekuri

Reputation: 2254

What mistake am I making in structuring my application?

You are not making any mistakes with app structuring. Application structuring is important but different project follow different structuring. It is not that you have to follow the same structuring from documentation.

How do i get it to run from the command line?

First you should understand how does Python know where to find packages when you call import?. Python imports work by searching the directories listed in sys.path. sys.path list inside the yourapplication will not contain itself. How to get it run? don't cd into yourapplication and follow the run instructions from documentation you linked.

How do I package it so it can run on another machine (that doesn't have Eclipse)

Eclipse is irrelevant here. Follow the same packaging structure. It should work on other machines given all dependencies are installed.

Upvotes: 1

Related Questions