0

I must be missing something but I look around and couldn't find reference to this issue.

I have the very basic code, as seen in flask-mongoengine documentation. test.py:

from flask import Flask
from flask_mongoengine import MongoEngine

When I run

python test.py ...

from flask_mongoengine import MongoEngine
ImportError: cannot import name 'MongoEngine'

Module in virtual environment contain (requirements.txt):

click==6.7
Flask==1.0.2
flask-mongoengine==0.9.5
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mongoengine==0.15.3
pymongo==3.7.1
six==1.11.0
Werkzeug==0.14.1
WTForms==2.2.1

My interpreter is Python 3.6.5 Any help would be appreciated. Thanks.

2 Answers2

1

Since your using a virtual environment did you try opening your editor from your virtual environment? For example opening the vscode editor from command-line is "code". Go to your virtual environment via the terminal and activate then type "code" at your prompt.

terminal:~path/to/virtual-enviroment$ source bin/activate
(virtual-enviroment)terminal:~path/to/virtual-enviroment$ code

If that doesn't work I, myself, haven't used flask-mongoengine. I was nervous of any issues that would come from the abstraction of it and instead just used Mongoengine with Flask.

I'm assuming you're only using this library for connection management so if you can't solve your issue with flask-mongoengine but are still interested in using mongoengine this was my approach. ~

I would put this in a config file somewhere and import it where appropriate-

from flask import Flask

MONGODB_DB = 'DB_NAME'
MONGODB_HOST = '127.0.0.1' # or whatever your db address
MONGODB_PORT = 27017  # or whatever your port

app = Flask(__name__) # you can import app from config and it will keep its configurations

then I would connect and disconnect from the database within each HTTP request function like this-

from config import MONGO_DB, MONGODB_HOST, MONGODB_PORT

# to connect
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)

# to close connection before any returns
db.close()

Hope this helps.

Joseph Vargas
  • 772
  • 5
  • 17
0

I had this issue and managed to fix it by deactivating, reinstalling flask-mongoengine and reactivating the venv (all in the Terminal):

deactivate
pip install flask-mongoengine 

# Not required but good to check it was properly installed    
pip freeze

venv\Scripts\activate
flask run
Jamey Hart
  • 31
  • 3