No module named 'pymysql' - python

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)

Related

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!

How to install python packages from CMD?

I want to install urllib2 and JSON but it doesn't seem to let me. I tried using pip install in cmd which didn't work and also tried specifying the Scripts path and then doing the command but that does not work either. I am on 64-bit and I have used some command in pip like pip install python3 (package name) which worked for me but I haven't install anything in some time so I don't remember what the command was exactly that worked for me.
Both of those should be apart of the standard library so there shouldn't be a need to reinstall using pip. Try reinstalling python3 (uninstall then reinstall) and check the "add to path" button at the bottom of the installer before installation.
You can also try pip3 install (package name) and also try to update it to the latest version using simocat's answer
Edit: I believe you don't need to install these packages they come as part of python 3.x
Have you tried using the command pip install (package name)?
You can also try to upgrade pip to the latest version with pip install --upgrade pip and then install a package.
EDIT:
Please note that The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. There is no need to install urllib2. Just import urllib.request into your script
What does:
python -m ensurepip --upgrade
in cmd respond?
(It is always helpful to post the Error output you receive from your commandline) and on these kinds of topics in general you may check: https://stackexchange.com/
Btw. Here is a related q&a:
Installing pip on windows?

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.

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.

ImportError: No module named MySQLdb

I am referring the following tutorial to make a login page for my web application.
http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982
I am having issue with the database.
I am getting an
ImportError: No module named MySQLdb
when I execute
http://127.0.0.1:5000/testdb
I have tried all possible ways to install python mysql, the one mentioned in the tutorial, easy_install, sudo apt-get install.
I have installed mysql in my virtual env. My directory structure is just the same as whats explained in the tutorial. The module is sucessfully installed in my system and still I am getting this error.
Please help. What could be causing this.
If you're having issues compiling the binary extension, or on a platform where you cant, you can try using the pure python PyMySQL bindings.
Simply pip install pymysql and switch your SQLAlchemy URI to start like this:
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'
There are some other drivers you could also try.
Or try this:
apt-get install python-mysqldb
may you try
pip install mysqlclient
My issue is :
return __import__('MySQLdb')
ImportError: No module named MySQLdb
and my resolution :
pip install MySQL-python
yum install mysql-devel.x86_64
at the very beginning, i just installed MySQL-python, but the issue still existed. So i think if this issue happened, you should also take mysql-devel into consideration.
Hope this helps.
I got this issue when I was working on SQLAlchemy. The default dialect used by SQLAlchemy for MySQL is mysql+mysqldb.
engine = create_engine('mysql+mysqldb://scott:tiger#localhost/foo')
I got the "No module named MySQLdb" error when the above command was executed. To fix it I installed the mysql-python module and the issue was fixed.
sudo pip install mysql-python
It depends on Python Version as well in my experience.
If you are using Python 3, #DazWorrall answer worked fine for me.
However, if you are using Python 2, you should
sudo pip install mysql-python
which would install 'MySQLdb' module without having to change the SQLAlchemy URI.
So I spent about 5 hours trying to figure out how to deal with this issue when trying to run
./manage.py makemigrations
With Ubuntu Server LTS 16.1, a full LAMP stack, Apache2 MySql 5.7 PHP 7 Python 3 and Django 1.10.2 I really struggled to find a good answer to this. In fact, I am still not satisfied, but the ONLY solution that worked for me is this...
sudo apt-get install build-essential python-dev libapache2-mod-wsgi-py3 libmysqlclient-dev
followed by (from inside the virtual environment)
pip install mysqlclient
I really dislike having to use dev installs when I am trying to set up a new web server, but unfortunately this configuration was the only mostly comfortable path I could take.
In ubuntu 20 , you can try this :
sudo apt-get install libmysqlclient-dev
sudo apt-get install gcc
pip install mysqlclient
While #Edward van Kuik's answer is correct, it doesn't take into account an issue with virtualenv v1.7 and above.
In particular installing python-mysqldb via apt on Ubuntu put it under /usr/lib/pythonX.Y/dist-packages, but this path isn't included by default in the virtualenv's sys.path.
So to resolve this, you should create your virtualenv with system packages by running something like:
virtualenv --system-site-packages .venv
yum install MySQL-python.x86_64
worked for me.
for Ubuntu 20.04 with python3
sudo apt-get install python3-mysqldb
by default, this work for me
Create a sqlite engine instance
engine = create_engine('mysql://username:password#your_host/your_dbname')
OR
pip install pymysql
Create a sqlite engine instance
create_engine('mysql://...
I have the same issue the install mySQL connector and resolved
pip install mysqlclient
more details ->
SQLAlchemy official docs
Got so many errors related to permissions and what not. You may wanna try this :
xcode-select --install
sudo apt-get install python-mysqldb
Here's what I did:
First, install a python package:
pip3 install PyMySQL
Then add the following lines of code to your Project:
import pymysql
pymysql.install_as_MySQLdb()
this should fix your issue by now, the following one is optional and for anyone who is using SqlAlchemy to connect to mysql server:
Replace engine = create_engine("mysql://YOURMYSQLURL")
to:
engine = create_engine("mysql+pymysql://YOURMYSQLURL")
I have faced this type of error when I tray to build python flask eccomerce web-application
ModuleNotFoundError: No module named 'MySQLdb'?
I tray to see the version of python installed on my window 10
it was python 3.10
so MYSQL-PYTHON is compatable with the other version of python 3.8
I delet python 3.10 and I nstalled the version python 3.8
It become ok!
python --version
3.8
pip --version
python 3.8
so install the connector
pip install MYSQL-PYTHON
successfuly installed

Categories

Resources