Python, how can I import modules that are in sub-directory?

Viewed 14878

I am using Python and Flask to build a project.

How can I import modules that are in sub-directory?

here is the directory structure.

/application
  /apis
    settings.py
index.py

and here is the index.py file.

from flask import Flask
import os
import sys
import random
from flask import render_template, request, jsonify, redirect, url_for, send_file, Response, make_response
from werkzeug import secure_filename

app = Flask(__name__)
app.debug = True

sys.path.append("apis")
import settings

here is the settings.py file in apis folder.

from flask import Flask
import pymongo
import datetime
import os
import sys
import random
from flask import render_template, request, jsonify, redirect, url_for, send_file, Response, make_response
from werkzeug import secure_filename

app = Flask(__name__)
app.debug = True

## About Us
@app.route('/apis/settings/aboutus')
def aboutus():
    return render_template('aboutus.html')

So, I tried to import settings module by using sys.path.append.

When all codes was in index.py, it worked fine.

If I hit this address http://domain.com:5000/apis/settings/aboutus, it supposed to show AboutUS page.

However, it says "404 Not Found" after I split modules.

Can you see the problem?

2 Answers
Related