Home > database >  Python how to import mod2
Python how to import mod2

Time:08-14

Summary: My goal: working on upload from mod1.py. I expect the uploaded file at /static/img/. However, there is nothing happens when tried

I have never done this procedure before. What I want is to be able to import and reuse function from mod2 in mod1. The mod1 is main. Both modules are in a same root folder. I don't use django. I put an empty init.py on the same directory. Please help suggest a specific ideas relate to mod2.

the structure

root/
    __init__.py

    mod1.py

    mod2.py

mod1 (a working .py):

from flask import Flask, redirect, render_template, request, url_for

app = Flask(__name__)
app.config["DEBUG"] = True

comments = []

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("main_page.html", comments=comments)

    comments.append(request.form["contents"])
    return redirect(url_for('index'))

mod2 (a working .py):

import os
#from flask import Flask, flash, request, redirect, url_for
from flask import Flask, flash, redirect, render_template, request, url_for

from werkzeug.utils import secure_filename
from flask import Request
from flask_uploads import IMAGES, UploadSet, configure_uploads



UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'docx'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024


photos = UploadSet("photos", IMAGES)
app.config["UPLOADED_PHOTOS_DEST"] = "static/img"
app.config["SECRET_KEY"] = os.urandom(24)
configure_uploads(app, photos)


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        photos.save(request.files['photo'])
        flash("Photo saved successfully.")
        return render_template('upload.html')
    return render_template('upload.html')


from flask import send_from_directory

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


from werkzeug.middleware.shared_data import SharedDataMiddleware
app.add_url_rule('/uploads/<filename>', 'uploaded_file', build_only=True)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
    '/uploads':  app.config['UPLOAD_FOLDER']
})

What I tried on mod1:

from flask import Flask, redirect, render_template, request, url_for
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, EqualTo, Length
from mod2 import upload
from mod2 import photos
from mod2 import UploadSet
from mod2 import IMAGES
from mod2 import configure_uploads
photos
app = Flask(__name__)
app.config["DEBUG"] = True
app.secret_key = "neverforgetyoursurvivor"

comments = ['import mod']

UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'docx'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
photos = UploadSet("photos", IMAGES)
app.config["UPLOADED_PHOTOS_DEST"] = "static/img"
configure_uploads(app, photos)


class CreateUserForm(FlaskForm):
    submit = SubmitField(label=('Submit'))


@app.route("/", methods=["GET", "POST"])
def index():
    form = CreateUserForm(request.form)
    if request.method == "GET":
        return render_template("main_page.html", comments=comments)
    if form.validate_on_submit():
        upload
        #return render_template("main_page.html", photos=photos)

    return redirect(url_for('index'))


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

from flask import send_from_directory

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

from werkzeug.middleware.shared_data import SharedDataMiddleware
app.add_url_rule('/uploads/<filename>', 'uploaded_file', build_only=True)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
    '/uploads':  app.config['UPLOAD_FOLDER']
})

There is no error. When I open the /static/img, nothing happens. My research on these pages are not solved: What is __init__.py for? And https://realpython.com/python-modules-packages/

Am I in the right direction? What are your suggestions? Thank you very much for your specific suggestion.

CodePudding user response:

Firstly, I don't see where you've tried to import mod2 in mod1. But python, allows relative imports. For your case from .mod2 import xxx should work.

If you want to compose your you Flask application, you should not create an app in each file, but instead blueprints, and register those in your single Flask app.

CodePudding user response:

I think you are trying to import variables from mod2 which is not possible, you might want to turn that into a function like this:

photos = UploadSet("photos", IMAGES)
def getPhotos(photos = photos):
    return(photos)

Then you can import the getPhotos function and use it like so:

from mod2 import getPhotos
photos = getPhotos()

Instead of importing UploadSet and configure_uploads from mod2, import them from flask.

from flask_uploads import UploadSet, configure_uploads
  • Related