No module named ‘openpyxl’ when trying to import it from flask app

No module named ‘openpyxl’ when trying to import it from flask app , pointing that the module is installed in “/home/ubuntu/.local/lib/python3.8/site-packages/” and it works fine in other scripts here what i am using

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/upload_file', methods=['GET','POST'])
def upload_file():
    try :
        import sys
        sys.path.append('/home/ubuntu/.local/lib/python3.8/site-packages/')
        import openpyxl
    except Exception as e: 
        return render_template('index.html',msg=str(e))
    return render_template('index.html',msg=request.method)

i’ve seen a lot of questions about how to import a module in flask since i’ve found it as a common error , so i’ve tried to create a module that does nothing but import openpyxl but no luck either i had the same error

I am using ubuntu 20.04 python3.8

Thanks in advance

Answer

I found two solutions :

1 . Install the module manually in /usr/local/lib/python3.8/dist-packages using python3 -m pip3 install openpyxl in that directory

2 . change the directory where the modules are installed using pip by creating the config file ~/.config/pip/pip.conf , the content must be your desired install location like this :

[global]
target = /usr/local/lib/python3.8/dist-packages

I hope this help someone with the same need