Import "pymongo" could not to be resolved - python

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.

Related

How do I deal with pip in different python versions?

I had python 3.8 installed and then I installed python 2.7. I am trying to run a python program with py -2 program.py in vs code using with python 2.7 as selected environment and I am getting an error, ImportError: No module named googlemaps even though I have already installed.
If I run the program using Python3 then it would run fine. Also when I open vs code using python 2.7 as selected runtime environment then I would get a warning Linter Pylint is not installed. If I click on install then I would get another warning There's no Pip installer available in the selected environment.
Also even though I have changed the python path from 3.7 to 2.7, default python version will still show up as 3.7 when I runPython in command line.
Things that I have tried to install the googlemaps module for python 2 after googling for solutions,
pip2 install googlemaps--upgrade
py -2 -m pip install googlemaps
If you have your python2 binary located, you can just call it directly:
/usr/bin/python2 -m pip install googlemaps
And if you're not sure where your python binary is, you can use
import sys
print(sys.executable)
to locate it.
And if you don't have pip, you should install it by downloading this file:
https://bootstrap.pypa.io/get-pip.py
then running:
/usr/bin/python2 get-pip.py
It is recommended to install Python 3.8 using Pyenv and also when you are using different versions of python, it is very useful
curl https://pyenv.run | bash
pyenv install 3.8.1
pyenv virtualenv 3.8.1 venv
pyenv local venv
with pyenv local you set your version for use.
If after this you run
pyenv version
It will output to 3.8.1
With regards to pip installation, run
whereis python
and if it outputs to
usr/bin/python2
then you can use pip for installing python2 packages and pip3 for packages compatible to python3.

How does the python -m prefix in command work?

I am very new to Python programming. I can use pip install without any trouble. I've seen a lot of posts in online which prefix pip install with python -m. When I use python -m before the pip command nothing seems to happen. Please explain.
If you run python -h, the -m flag is described as:
-m mod : run library module as a script (terminates option list)
In this case, when you run python -m pip Python runs the pip module as a script (i.e. it executes the code inside it). If you have added pip to your PATH you can then just run pip directly without the need to invoke it via python.
python -m pip install <some module> and pip install <some module> should perform identically. If you have multiple versions of Python installed on your system, using pip might install a module for the wrong version of Python. You can check the version using pip -V which will output something like
pip 18.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
here the python version is python3.7.
If the version of the pip command doesn't match with the version of Python used to execute a program when the program is executed it will not run. You can check your Python version with python -V, which will output something like
Python 2.7.16
If the pip version doesn't match your Python version you can either update your path's pip, or you can use python -m pip. To select the correct version (i.e. if python runs Python 2 and you want to use Python 3) just execute python3 -m pip or python3.7 -m pip.

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.

Cannot install pip on Python 2.7

I'm a new Python user. I'm having issues installing new modules for python 2.7. When I try installing a new module from PyCharm, I get the following error
Error: Python packaging tool 'pip' not found
Moreover, I'm a bit confuse about which Python version I'm actually using...
This is what I get when I type the following commands in the terminal.
$ which python
/usr/bin/python
$ echo $PYTHONPATH
PYTHONPATH:/usr/local/lib/python3/dist-packages/
$ python -V
Python 2.7.6
Everything seems a bit messed up to me...all the procedures I follow to install pip result in failure. The command
$ python get-pip.py
returns
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command
However, when I run that command, it tells me that there is no module named pip.
Please, how can I fix it? I need to work with Python 2.7 but I'm completely unable to install packages. Thanks.
EDIT I am using Ubuntu 14.04 LTS
You may be trying to install pip wrong. Depending on your version of linux there are several install methods that can be found here
for your version judging by your use of get.
try this one:
sudo apt-get install python-pip

ImportError: No module named 'pymongo'

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.

Categories

Resources