Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually install Flask extensions?

I have a Flask project which I've put the flask module (version 0.9) directly beside my app.py file. I've done this so that I can bundle everything into a version control repository that won't require anyone else using it to install additional Python modules.

I want to use flask-login so I've tried to manually install it by downloading the latest version and putting the flask_login.py file in my "local" flask/ext/ directory. However, while I can import flask and import flask.ext, I am unable to import flask.ext.login with Python throwing ImportError: No module named flask.ext.login. import flask.ext.flask_login throws an import error as well.

Is there something I have to do differently if Flask and it's extensions are local to the app.py?

like image 393
Soviut Avatar asked Feb 18 '23 17:02

Soviut


2 Answers

The solution is to put the flask_login.py file in the same directory as my app.py file. No modification of the flask/ext/__init__.py file is necessary.

The flask.ext module is intended only as a an extension importer, not a repository for installed extensions. Based on the import path, I thought the flask/ext folder was where extensions were to be copied. This was incorrect. Extensions simply need to be somewhere on the python path.

like image 151
Soviut Avatar answered Feb 27 '23 09:02

Soviut


Python doesn't throw the exception in your case, this Flask module is responsible for modifying the standard import hook in the ext folder: https://github.com/mitsuhiko/flask/blob/master/flask/exthook.py

So, I don't think putting flask_login.py into flask/ext is the right way to use extensions in Flask. The documentation recommends to use pip install flask-login or python setup.py install. After that you can do:

import flask_login

If you still want to do it manually, remove the setup() call from ext/__ init__.py. It probably has a good reason why the Flask guys did it this way though :-)

like image 25
Fabian Avatar answered Feb 27 '23 09:02

Fabian