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
        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


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 :

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

I hope this help someone with the same need

Answered By – waghydjemy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published