ImportError: No module named 'pymongo' - python

I have a problem running pymongo on Win 7 (64) with Python 3.4, mongodb 4.2.10.
The error output is as follows:
import pymongo
ImportError: No module named 'pymongo'
The code is pretty simple:
import pymongo
from pymongo import MongoClient
client=MongoClient()
db=client.test_db
dict={'A':[1,2,3,4,5,6]}
db.test_collection.insert(dict)
to_print=db.test_collection.find()
print(to_print)
I tried already re-installing Python and MongoDB - did not help. It works when I do it manually in cmd, i.e. mongod.exe and mongo.exe work fine. It appears there is problem with pymongo, but I don't know how to fix it.

All you need is to actually install pymongo (currently you just have mongo and python, but they do not know how to speak with each other). This page is telling you exactly what to do:
go to pymongo page
download and run installer.

I am new to Python,
But I think install setuptools is a good idea,
after that:
pip install pymongo

If you have installed pymongo using following command :
sudo pip install pymongo or
sudo -E pip install pymongo
And still you are getting import error then try to run your python script with sudo like :
sudo python example.py
If you are able to run the script this way, but not without sudo.
Then there can be a problem with PYTHON_PATH or Permission issue.
Solving isssue#1 (i.e. PYTHON_PATH) :
Location where pip installs packages and location where python looks for packages do not match.
So how do you find where pip install packages ? Run following command :
sudo pip show pymongo
It shows output like this :
---
Name: pymongo
Version: 3.4.0
Location: /usr/local/lib/python2.7/dist-packages
Now you know where pip install packages. Add following line in your .bashrc :
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/
Run following command to execute .bashrc again :
source .bashrc
Now try to run python script without sudo. It should run.
If not then do the following :
Solving issue#2 (i.e. Permission):
Allow non-root users to read and execute python pacakages.
sudo chmod -R ugo+rX /usr/local/lib/python2.7/
This should solve your all problems. You should be able to run python script without sudo.

I was working on Python 3 but installed the Python 2 version of pymongo with
$ pip install pymongo command. I uninstalled this version of pymongo with
$ pip uninstall pymongo and installed Python 3 version of it via
$ pip3 install pymongo. (after installing pip3 via $ sudo-apt install pip3 in linux terminal). I hope this solves your problem as well as mine.

Try this:
sudo apt-get install python-pip
sudo pip install pymongo

If you have a problem like that probably you didn't two things.
you didn't install pymongo.You can install by below command;
$pip install pymongo
You installed pymongo but You have two python packages location. like below;
C:\Python\Python37-32\Lib\site-packages\pymongo (pymongo installed here)
C:\Anaconda3\Lib\site-packages\ "pymongo is not here"
And you try to work here.
Probably you run Spyder but Spyder is looking to Anaconda\Lib\site-packages\ but pymongo packages are not here.
Sorry for bad english.

For me i am running Flask server so i had to go to the terminal and run the command :
pip install Flask-PyMongo

Make a new folder in your documents like "flask-pymongo"
In your terminal change directory to C:\Users\YOUR_USERNAME\Documents\Flask-PyMongo\
git clone https://github.com/dcrosta/flask-pymongo
cd flask-pymongo
py setup.py develop OR python setup.py develop (depends how python was install to your path)
from anywhere in your terminal use : pip install pymongo
Solution is for windows users

Try the following:
conda install pymongo

For me I had this error after I'd installed pymongo via console/terminal but when I looked in my project's interpreter (for example in PyCharm you go to
Preferences > 'Project: <'name of your project'>' > Project Interpreter
I saw things like pip and setuptools but not pymongo. I clicked the '+' at the bottom of the pane and searched for pymongo and found it and could install it there. After adding this the project ran fine

I had the same error on linux while working on some project,I'll post for you the linux commands for it and you can find the windows equivalent easily
what solved my problem is to first install virtual environment venv
> sudo pacman -S python-virtualenv
create a venv (it's in best practice to keep virtualenv in different dir than the project since you don't need to distribute it) and activate it.
> python -m venv venv/
> . venv/bin/activate
once you are in the venv you should see the command prompt like this one
(venv) [sam#archlinux labs]$
now install pymongo inside your venv using
(venv) [sam#archlinux labs]$ pip install pymongo
then run your files while the virtual environment is active
(venv) [sam#archlinux labs]$ python myfile.py
to install flask also the same inside your venv using
(venv) [sam#archlinux labs]$ pip install flask

I'm working with Python's virtual environment (venv) and for some reason it didn't work for me to just
pip install pymongo
in my venv. It installed the package correctly in venv/Lib/site-packages put I couldn't run the script.
What worked for me was to create a requirements.txt file and write the packages I needed for the project in there
pymongo==4.0.1
and then run the command
python3 -m pip install -r requirements.txt
I can now run my script. Hope this can help any newcomers to this question.

Whenever I've got an issue as this, I open a new terminal and cd into the directory of my project. Do not activate your virtualenv yet. Now, install the missing module, activate your virtualenv, case closed.

Related

Import "pymongo" could not to be resolved

I'm trying to create a database with mongodb and python and I installed pymongo library with the following command:
sudo pip3 install pymongo
But when trying to run the application, the error appears stating that pymongo is not installed:
pymongo error
I use VS Code to program in Python and the python interpreter is located at /home/paulo/Python-3.8.2
But pymongo was installed in this location, but I didn't select it
You have multiple versions of python installed. It is installing to python3.7 instead of the 3.8 you are using.
You can force it to install to your default python environment (which should be 3.8) by using sudo python -m pip install pymongo. I cannot tell if you're using macOS or linux. if you're using linux you can also typically use sudo python3.8 -m pip install pymongo.
This will let you run the pip command with your default python command install.
Using -m will let you use that default python version to run pip instead of the one set in your ~/.bashrc file.
Alternatively you can go into your ~/.bashrc file and change the pip3 alias to the correct version of python. You can find the correct version by using which python which will give you the location of your default python install.

Python - Notion API - Can't import module

I'm trying to use Notion API for some automation processes but I am having some trouble importing the packages and I already try a lot of imports. I've this code:
from notion_database.database import Database
my_token = "my_token"
D = Database(integrations_token=my_token )
print(D.list_databases(page_size=100))
To run this code I made the following imports:
pip install notion-database
pip install notion-py
pip install "notion==0.0.25"
pip install notion
All of them were installed correctly and I don't have my script name as "notion.py" :D
But when I run my code I got:
from notion_database.database import Database
ModuleNotFoundError: No module named 'notion_database'
Does anyone know what I am doing wrong?
Thanks for the help!
It looks your code is fine.
More likely, your python and pip points onto two different python versions.
I would suggest you create an environment (virtualenv or pyenv) and reinstall the packages using this command:
sudo python -m pip install notion-database
sudo python -m pip install notion-py
sudo python -m pip install "notion==0.0.25"
sudo python -m pip install notion
maybe you have not install notion successfully.
try to replace pip install notion
to pip install notion --user
,after that maybe there is no error with run "import notion".
good luck!

Problems of VirtualEnv and pip3

I used VirtualEnv to create a python2 environment without system site packages like this:
virtualenv -p /usr/bin/python2.7 --no-site-packages ENV2.7
And I want to install packages in this environment.
However, I found that my python code is still trying to look for packages out of this environment.
For example, after activate this env, I used:
pip install matplotlib
And in my demo.py, there is
import matplotlib
But this raised an error, and can not find this package
However, when I use python in the terminal and enter the interactive python, import matplotlib dose not raise an error.
Then I started another terminal and tried to install this package out of the environment by pip3:
pip3 install matplotlib
It turned out that my demo.py just work well.
Any idea? Many Thanks!
It sounds like your virtualenv pip version may be using pip3 instead of pip2:
Make sure you are using the correct python version in your project that you mean to, and using the same version of pip in your virtualenv. (Note that you use pip above once, then you used pip3 outside your virtualenv.)
Check your pip version from inside the virtualenv:
workon (your env name)
which pip
pip -V
Output should look something like:
$ which pip
/home/yourname/.virtualenvs/testenv/bin/pip
$ pip -V
pip 9.0.1 from /home/yourname/.virtualenvs/testenv/local/lib/python2.7/site-packages (python 2.7)
It should tell you you're using pip inside your virtualenv, and the correct python version.
If that looks correct, install your packages.
pip install (whatever)
Check they are installed with pip freeze.
Run your project. :)

ImportError: No module named 'ldap' Python 3.5

I'm running Python 3.5 (on Windows) and I have installed python-ldap from https://pypi.python.org/pypi/python-ldap/
I also tried using ldap3 but I keep getting an error saying
"ImportError: No module named 'ldap'
I looked around and saw some people saying there's no python-ldap for 3.5 so I installed 2.6 still getting the same error.
Is there a way to import ldap and make it work for Python 3.5?
Try the command below:
sudo apt-get install libsasl2-dev python-dev libldap2-dev libssl-dev
sudo pip3 install pyldap
Open a command line(cmd, powershell, git bash)
Check you python version
$ pyhton --version
Go to https://www.lfd.uci.edu/~gohlke/pythonlibs/
Download the library according with your python version and windows system
And install it on a command line using
pip install file_downloaded.whl
For example:
If you have python 3.5x and windows x64,
download the file python_ldap‑3.2.0‑cp35‑cp35m‑win_amd64.whl
pip install python_ldap‑3.2.0‑cp35‑cp35m‑win_amd64.whl
I'm running Apache Airflow on an Amazon EC2-Instance and I was getting "ImportError: No module named 'ldap3'. I used these two sites https://www.python-ldap.org/en/latest/installing.html and http://ldap3.readthedocs.io/installation.html to run the commands sudo python -m pip install python-ldap and sudo pip install ldap3 but my pip wasn't working for the last command so after some investigation I found out in my /usr/bin/ directory I had pip, pip-2.7, pip-3.6, and pip-python so I changed the command to pip-3.6 install ldap3 and then everything worked! Hope this helps someone.
I tried multiple approaches but finally, PyPI official documentation fixed this.
I was trying to execute on VS Code and did pip3 install python-ldap, but it didn't solve the issue. So I did the below from VS Code
# %% - This runs the below code as a Jupyter notebook cell
!pip3 install python-ldap
Now, I am able to import ldap and use it
Inside you folder, you can use virtualenv for python 3, example:
/opt/python-ldap-test
virtualenv -p /usr/bin/python3.5 venv
source venv/bin/activate
and then
pip install ldap3
It's extremely complicated to make things from 2.X to work in 3.X. Have you tried using it in a separate, 2.X only script and using it from there? It's not so unusual to combine python 2.X with 3.X in that manner or so I've heard.

No module named 'pymysql'

I'm trying to use PyMySQL on Ubuntu.
I've installed pymysql using both pip and pip3 but every time I use import pymysql, it returns ImportError: No module named 'pymysql'
I'm using Ubuntu 15.10 64-bit and Python 3.5.
The same .py works on Windows with Python 3.5, but not on Ubuntu.
Sort of already answered this in the comments, but just so this question has an answer, the problem was resolved through running:
sudo apt-get install python3-pymysql
Use:
import pymysql
Not:
import PyMySQL
That works for me.
After trying a few things, and coming across PyMySQL Github, this worked:
sudo pip install PyMySQL
And to import use:
import pymysql
If even sudo apt-get install python3-pymysql does not work for you try this:
Go to the PyMySQL page and download the zip file.
Then, via the terminal, cd to your Downloads folder and extract
the folder
cd into the newly extracted folder
Install the setup.py file with: sudo python3 setup.py install
Make sure that you're working with the version of Python that think you are. Within Python run import sys and print(sys.version).
Select the correct package manager to install pymysql with:
For Python 2.x sudo pip install pymysql.
For Python 3.x sudo pip3 install pymysql.
For either running on Anaconda: sudo conda install pymysql.
If that didn't work try APT: sudo apt-get install pymysql.
If all else fails, install the package directly:
Go to the PyMySQL page and download the zip file.
Then, via the terminal, cd to your Downloads folder and extract the folder.
cd into the newly extracted folder.
Install the setup.py file with: sudo python3 setup.py install.
This answer is a compilation of suggestions. Apart from the other ones proposed here, thanks to the comment by #cmaher on this related thread.
To get around the problem, find out where pymysql is installed.
If for example it is installed in /usr/lib/python3.7/site-packages, add the following code above the import pymysql command:
import sys
sys.path.insert(0,"/usr/lib/python3.7/site-packages")
import pymysql
This ensures that your Python program can find where pymysql is installed.
fwiw, for a conda env:
conda install -c anaconda pymysql
For windows or one using google colab, you can try this
!pip install pymysql
import pymysql
sudo apt-get install python3-pymysql
This command also works for me to install the package required for Flask app to tun on ubuntu 16x with WISG module on APACHE2 server.
BY default on WSGI uses python 3 installation of UBUNTU.
Anaconda custom installation won't work.
I had this same problem just now, and found the reason was my editor (Visual Studio Code) was running against the wrong instance of python; I had it set to run again python bundled with tensorflow, I changed it to my Anaconda python and it worked.
Just a note:
for Anaconda install packages command:
python setup.py install
if you are using SPYDER IDE , just try to restart the console or restart the IDE, it works
I also got this error recently when using Anaconda on a Mac machine.
Here is what I found:
After running python3 -m pip install PyMySql, pymysql module is under /Library/Python/3.7/site-packages
Anaconda wants this module to be under /opt/anaconda3/lib/python3.8/site-packages
Therefore, after copying pymysql module to the designated path, it runs correctly.
Another common issue causing the error message to appear is related to conda environments in jupyter notebook and jupyter lab.
After successfully installing a module (pymysql in this case) in one environment, import may seem to fail because the environment has not been correctly registered. To solve this it is necessary to manually add kernels for each environment.
Solution and more details can be found here.
I tried installing pymysql on command prompt by typing
pip install pymysql
But it still dont work on my case, so I decided to try using the terminal IDE and it works.
I ran into the same problem earlier, but solved it in a way slightly different from what we have here. So, I thought I'd add my way as well. Hopefully, it will help someone!
sudo apt-get install mysql-client didn't work for me. However, I have Homebrew already installed. So, instead, I tried:
brew install mysql-client
Now, I don't get the error any more.
You can also visit this lien pip install PyMySQL
You just need to install:
python3 -m pip install PyMySQL
The following pymysql version worked for me:
pip install pymysql==1.0.2
For anaconda user,
I got this error message.
ModuleNotFoundError: No module named 'pymysql'
So tried 'pip install pymysql' but got below.
Requirement already satisfied: pymysql
What worked for me is,
python file.py (NOT py file.py)

Categories

Resources