1

So i learning flask web dev with mongodb. I am getting an error on the following code,

import pymongo

I am using virtualenv and the following are configured on it, obtained via pip3 freeze,

Flask==1.0.2
Flask-MongoAlchemy==0.7.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
MongoAlchemy==0.19
pymongo==2.8.1
Werkzeug==0.14.1

I am getting the error unable to import 'pymongo' via pylint on Visual studio code.

FYI i was getting a similar error when using MongoAlchemy. I thought i'd switch and give it a go with pymongo.

from flask.ext.mongoalchemy import MongoAlchemy

I have run python3 -m pip install pymongo but as you may correctly guess i am getting Requirement already satisfied: pymongo in ./venv/lib/python3.7/site-packages (2.8.1)

How could i solve this ? Thank you.

dcrosta
  • 26,009
  • 8
  • 71
  • 83
Ayubx
  • 597
  • 2
  • 9
  • 19
  • are you using python3 everywhere? Perhaps you have installed it with `python3` and see it in `pip3 freeze`, but are running your program with a different version of python (or even the same version, but installed somewhere else) – lucidbrot Nov 24 '18 at 10:52
  • @lucidbrot I am using python3 everywhere. My virtualenv is using python3.7 – Ayubx Nov 24 '18 at 10:55
  • And the virtualenv is activated? – lucidbrot Nov 24 '18 at 11:10
  • @lucidbrot yes it is. What i have done now is deleted the virtualenv directory and reinstalled everything. I am now using 'from flask_pymongo import PyMongo'. That seems to be working but i am still getting unable to import pylint error on VsCode. Weird. – Ayubx Nov 24 '18 at 11:46
  • What is the Flask version you're running? Could you try doing: `from flaskext.mongoalchemy import MongoAlchemy` and tell the result? – Rahul Bharadwaj Nov 24 '18 at 13:39
  • 1
    @RahulBharadwaj Thank you. I downloaded all the latest versions via pip. Will try this and let you know. Not on my personal mac atm. – Ayubx Nov 25 '18 at 15:18
  • @RahulBharadwaj The solution you proposed does not work. Looking at it again it looks like the library import which I initially tried but yours is flaskext which I think is a typo. – Ayubx Nov 26 '18 at 07:42
  • For using flask-mysql I hadd to do `flaskext` instead of `flask.ext`, so I thought it might help. – Rahul Bharadwaj Nov 26 '18 at 10:33
  • Take a look at [flake8](https://stackoverflow.com/questions/54160207/using-flake8-in-vscode) instead if you can, it'll work with vscode and was much more stable in my experience. – ti7 Aug 13 '20 at 21:50

3 Answers3

0

so it seems like flask.ext.. is deprecated. so from flask_pymongo import PyMongo is the way to go if i want to use pymongo. As for the error vscode was showing it seems like it's related to linting.

Ayubx
  • 597
  • 2
  • 9
  • 19
0

I had the same issue and was able to fix it by restarting my virtualenv.

I hope this will help somebody.

a.lavrov
  • 31
  • 4
-2

unable to import 'pymongo' via pylint on Visual studio code

Disabling linting worked for me; you can find the checkbox in settings by searching pylint

ti7
  • 16,375
  • 6
  • 40
  • 68
  • Please answer the question in full and only use links supplementally. See the [tour] for how to answer questions so that they are usefull into the future. – Albert Rothman Aug 13 '20 at 17:43