Traceback (most recent call last):
File "C:\AppServ\www\pythonWeb\MySqlConn.py", line 1, in <module>
import mysql.connector
ImportError: No module named 'mysql'
I got the above error message while trying to import mysql.connector
I'm using a Window with python 3.4.2 and Appserv 2.5.10 for Window
I've saved the python file under the folder of AppServ.
Please help.
Error clearly shows you do 'not have the module "Mysql" which you are importing in order to process further.
ImportError: No module named 'mysql'
What you can do depending on OS you are using :
Windows
pip install mysqlclient
If you need 1.2.x versions (legacy Python only), use pip install MySQL-python
Note: Some dependencies might have to be in place when running the above command.
Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)
sudo apt-get install python-pip python-dev libmysqlclient-dev
Fedora 24:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
Mac OS
brew install mysql-connector-c
if that fails, try
brew install mysql
once you install the module successfully re-run your script and it should work
Related
After I upgraded pip from 8.1.1 to 19.0.1 by running
pip install --upgrade pip
I tried to test the version of pip by running
pip -V
But I got the following error
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main'
I set the python3 and pip3 to be default by putting the following in ~/.bashrc
alias python=python3
alias pip=pip3
My system is ubuntu 16.04
This is a common issue as referenced here : https://github.com/pypa/pip/issues/5221
You are trying to use the pip, which is shipped with the Debian system. You better try to avoid using that pip at any cost.
Please use python3 -m pip install package instead of the system pip which you have in the debian system.
I also recommend using venv - virtual environments for keeping your system environment safe.
from flask_mysqldb import MySQL
File "/Library/Python/2.7/site-packages/flask_mysqldb/__init__.py", line 1, in <module>
import MySQLdb
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: #rpath/libmysqlclient.21.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: image not found
this is the error I receive when trying to run my flask app on a local server.
You may have to install the python connector for mysql.
> brew install mysql-connector-c
> pip install mysql-python`
After installation, your import should work fine.
NB: I assume that you have already installed homebrew
It's easy to do, but hard to remember the correct spelling:
pip install mysqlclient
If you need 1.2.x versions (legacy Python only), use pip install MySQL-python
Note: Some dependencies might have to be in place when running the above command. Some hints on how to install these on various platforms:
Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)
sudo apt-get install python-pip python-dev libmysqlclient-dev
Fedora 24:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
Mac OS
brew install mysql-connector-c
if that fails, try this
brew install mysql
I'm on Ubuntu 16.04, and I get:
Traceback (most recent call last):
File "/home/omermazig/.virtualenvs/fixi/bin/pip", line 7, in <module>
from pip import main
File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/site-packages/pip/__init__.py", line 26, in <module>
from pip.utils import get_installed_distributions, get_prog
File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/site-packages/pip/utils/__init__.py", line 23, in <module>
from pip.locations import (
File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/site-packages/pip/locations.py", line 9, in <module>
from distutils import sysconfig
File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/distutils/__init__.py", line 25, in <module>
from distutils import dist, sysconfig
ImportError: cannot import name 'dist'
When I run anything with python. This specifically is for trying to run "pip freeze". What to do?
try it
sudo apt install python3-distutils
My case was when I upgraded Ubuntu 18 -> 19.
So it reinstalled python and what I needed to do is:
remove old virtual environment
create a new one
install requirements via pip into it
I ran into this problem after installing Python 3.8 on Ubuntu (I have version 16.04)
$ lsb_release -d
Description: Ubuntu 16.04.6 LTS
Reproduce the error
Just try to import distutils
$ python3 -c "from distutils import sysconfig"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.8/distutils/__init__.py)
$ sudo apt install python3-distutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python3-distutils
The package python3-distutils cannot be found
$ sudo apt install python3-distutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python3-distutils
list available distutils
What helped was to list all distutil packages using a regexp
$ apt list *distutils*
Listing... Done
python-distutils-extra/xenial,xenial 2.39-1 all
python-stsci.distutils/xenial,xenial 0.3.7-4 all
python3-distutils-extra/xenial,xenial 2.39-1 all
python3-stsci.distutils/xenial,xenial 0.3.7-4 all
python3.7-distutils/xenial,xenial 3.7.8-1+xenial1 all
python3.8-distutils/xenial,xenial 3.8.3-1+xenial1 all
python3.9-distutils/xenial,xenial 3.9.0~b4-1+xenial1 all
Install the correct distutils package
For my Python 3.8 I picked python3.8-distutils and it worked
$ sudo apt-get install -y python3.8-distutils
Since I run into this issue everytime I update my ubuntu version every six months, then stumble on the exact same SO result, here is my solution.
If the other solutions listed here don't work (installing python3-distutils), it might be because of different python versions between the system and the virtualenv.
The easy solution is to destroy your virtualenv, then recreate it from scratch.
Solved: I've just got this issue in a virtual environment installed 2 years ago, using python3.7
Running pip3 or python3.7 -c "from distutils import dist, sysconfig" from the venv, I got the error ImportError: cannot import name 'dist'
Using system python 3.9, this error disappers.
I solved copying the /usr/lib/python3.9/distutils into the python3.7 virtual environment.
Take a loot at this (similar problem):
https://github.com/pypa/pip/issues/5367
Possible fix:
Download Python source from https://www.python.org/
Decompress the source code
Install the following dependencies:
sudo apt-get install zlib1g-dev (needed to compile Python)
and install:
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev (needed by Pip to make SSL requests)
Compile and install Python:
/configure
make
make install
Python 3.6 with Pip should be installed.
Full credit to jonbesga.
I have a PC running Ubuntu 17.10. I want to runn a python script that uses gtk. HoweverI get the following error:
Traceback (most recent call last):
File "/usr/local/bin/selectvm.py", line 3, in <module>
import gtk
ImportError: No module named gtk
I have tried already many things like:
sudo apt install python-gtk2
sudo apt install python-gtk2-dev
but none of them works. APT says they are already installed.
Try:
sudo apt-get install python-gtk2
These are the python bindings for GTK.
Graphic package nessesary to build the software-center UI.
Or:
sudo apt-get install afnix
credits
This question already has answers here:
Python MySQLdb not importing
(7 answers)
Closed 3 years ago.
I'm going to connect to mysql database using python. My python version is 2.7.12 and I've installed MySQL using sudo yum install MySQL-python
Package MySQL-python26-1.2.3-11.14.amzn1.x86_64 already installed and latest version
Nothing to do
Linux version:
$ cat /etc/*-release
NAME="Amazon Linux AMI"
VERSION="2016.09"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2016.09"
But when I try to import MySQLdb package it gives an error:
$python
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
I've tried different ways as long as I found on the internet but it didn't work.
Any help would be appreciated.
Updated:
Required dependency library:
sudo yum install mysql-devel
Try running:
pip install MySQL-python
Make sure you have depending libraries installed.
For that you may run:
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
As in your case sudo yum install mysql-devel did the job for you.
install pip and upgrade to the latest version.
apt-get install python-pip
pip install -U pip
Next, install the required development packages.
apt-get install python-dev libmysqlclient-dev
Then...
pip install MySQL-python
This should complete installation. After running these commands successfully, try importing MySQLdb in python interpreter.
>>> import MySQLdb