I've read a few questions on here that people are having the same situation as me but no matter what method I tried, it just wouldn't work!
According to MySQL documentation, MySQL-connector-python-2.x.x.x should work with Python 3+. I've downloaded the RPM package and installed it inside my virtualenv. However, upon importing the library, I get the error, "No module named MySQL".
Checking to see if I've installed the package:
(virtual)[centos ~]$ rpm -ql mysql-connector-python-2.1.3-1.el6.x86_64.rpm
package mysql-connector-python-2.1.3-1.el6.x86_64.rpm is not installed
Attempting to install the package:
(virtual)[centos ~]$ sudo rpm -i ~/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm
[sudo] password for <username>:
warning: /home/<username>/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
package mysql-connector-python-2.1.3-1.el6.x86_64 is already installed
This works on Python2.6:
[centos ~]$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> import mysql.connector
>>>
Doesn't on Python3.5:
[centos ~]$ python3.5
Python 3.5.1 (default, Jan 13 2016, 17:43:34)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>>
Verified the package through MySQL documentation:
(virtual)[centos]$ python
Python 3.5.1 (default, Jan 13 2016, 17:43:34)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>>
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>>
Making sure that python3.5 is in the same directory as MySQL.connector
(virtual)[centos]$ which python3.5
~/Documents/redhat-server/python_project/virtual/bin/python3.5
Update:
(virtual)[centos]$ pip install mysql-connector-python
Collecting mysql-connector-python
Could not find a version that satisfies the requirement mysql-connector-python (from versions: )
No matching distribution found for mysql-connector-python
(virtual)[centos]$
What am I doing wrong?
I've figured it out. Anzel was right about the fact that it's installed globally in the default python2.6 directory. Here's how I fixed it:
Launch the default python interpreter, in my case "python" -> python2.6, and run the commands below to see where "MySQL.connector" was installed.
>>> from distutils.sysconfig import get_python_lib
>>> print get_python_lib ()
/usr/lib/python2.6/site-packages
>>>
Navigate to the above directory, "/usr/lib/python2.6/site-packages/" reveal a directory called MySQL, and a file called, MySQL_connector_python-2.1.3-py2.6.egg-info.
Launching python3.5 interpreter leads me to a different path, which I checked and sure enough, the two MySQL and MySQL_connector_python-2.1.3-py2.6.egg-info file/directory wasn't there.
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>>
Next, I copied those 2 files to Python3.5 site-packages directory, tested the import, database connections, and it worked!
Python 3.5.1 (default, Jan 13 2016, 17:43:34)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "copyright", "credits" or "license()" for more information.
>>>
>>> import mysql.connector
>>>
>>> cnx = mysql.connector.connect (user = 'username', password = 'password', host = 'localhost',
database = 'testdb')
>>> cnx
<mysql.connector.connection.MySQLConnection object at 0x7ffa4c1009b0>
Related
I am trying to get the GPIO module working on a Raspberry Pi 3B+. I have installed the libraries as below. It works fine in Python2 but not Python 3 (version 3.9). Any ideas would be appreciated.
pi#raspberrypi:/usr/bin $ sudo apt-get install python-rpi.gpio python3-rpi.gpio
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-rpi.gpio is already the newest version (0.6.5~stretch-1).
python3-rpi.gpio is already the newest version (0.6.5~stretch-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
pi#raspberrypi:/usr/bin $ python2
Python 2.7.13 (default, Aug 22 2020, 10:03:02)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO
>>>
pi#raspberrypi:/usr/bin $ python3
Python 3.9.0 (default, Jan 27 2021, 16:17:29)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'RPi'
>>>
Trying to connect python to MySQL. Seem to be a lot on this issue but nothing seems to be working for me. If I am at the Python prompts I can enter my script line by line and have success.
>>> import mysql.connector as m
>>> m.__version__
'8.0.22'
But when I run the above two lines in my python script (named dbsql.py) I get the error:
File "C:\Users\gbran\PythonCode\dbsql.py", line 1, in <module>
import mysql.connector as m
ModuleNotFoundError: No mdoule named 'mysql'
I am new to Python, but wondering if this is a PATH issue within Widnows. Is there a way in the Python Prompt to see where the file mysql.connector is importing from to ensure the path is available for the script. Or is there something else I am missing here?
Thanks for any direction and help!
Use pip to search the available module
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf
$ pip install mysql-connector-python-rf
Verify
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
For python3 and later use the next command: $ pip3 install mysql-connector-python-rf
You can use sys module to find out the path from where mysql is getting imported.
In the example, I can see that my OS module is getting imported from /usr/lib/python3.8/os.py
>>> import sys
>>> import os
>>> sys.modules['os']
<module 'os' from '/usr/lib/python3.8/os.py'>
>>>
Then add that path to your PYTHONPATH so that python can import modules from there.
See another example below:
user#user-Inspiron:~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample_module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sample_module'
>>>
user#user-Inspiron:~$ echo $PYTHONPATH
user#user-Inspiron:~$ export PYTHONPATH=/tmp/
user#user-Inspiron:~$ echo $PYTHONPATH
/tmp/
user#user-Inspiron:~$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample_module
>>> sample_module.does_it_work
'It Works!'
>>>
We have a setup based on CentOS 6.4 but with Python 2.7 (due to historical reasons). Note that CentOS 6 brings Python 2.6. Python 2.7 has been compiled from a Fedora 20 SRPM. In addition, to make it possible to freely use Python RPMs from CentOS 6 along with the 2.7 interpreter, we have created a /usr/lib/python2.7/site-packages/setup.pth file with the contents:
/usr/lib64/python2.6/site-packages
/usr/lib/python2.6/site-packages
Things used to work fine till the need to use protobuf-python-2.3.0-9.el6.x86_64 arose. This RPM has been downloaded from the EPEL repo and is available: here.
$ /usr/bin/python2.7
Python 2.7.4 (default, Mar 17 2015, 00:48:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
One thing that could be helpful is that the import error does not come with Python 2.6:
$ /usr/bin/python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
This error does not come with other import statements (e.g. import pip.vcs). It is somehow related to protobuf-python. What could be the reason? Any insight will be appreciated.
EDIT: The protobuf-python RPM installs files into /usr/lib/python2.6/site-packages/google/protobuf and the the 2.7 interpreter's sys.path is:
$ /usr/bin/python2.7 -c 'import sys; print sys.path' | sed -e 's/,/\n/g'
[''
'/usr/lib64/python27.zip'
'/usr/lib64/python2.7'
'/usr/lib64/python2.7/plat-linux2'
'/usr/lib64/python2.7/lib-tk'
'/usr/lib64/python2.7/lib-old'
'/usr/lib64/python2.7/lib-dynload'
'/usr/lib64/python2.7/site-packages'
'/usr/lib/python2.7/site-packages'
'/usr/lib64/python2.6/site-packages'
'/usr/lib/python2.6/site-packages']
how can i fix this, or find the logs to investigate it?
$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import symbols
>>> from sympy.plotting import plot
OpenGL Warning: Failed to connect to host. Make sure 3D acceleration is enabled for this VM.
>>> x = symbols('x')
>>> p1 = plot(x*x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> import sympy
>>> sympy.__version__
'0.7.1.rc1'
it looks like the plotting module has not been fully installed?
this seems to be a very old version of sympy. the api has changed. in sympy 0.7.1.rc1, plot is a module, not a function.
i solved the issue by removing the existing versions of mpmath and sympy (which had been installed with apt) and installing the latest versions like so:
$ sudo apt-get remove --purge python-mpmath python-sympy
$ sudo python -m easy_install mpmath
$ python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mpmath
>>> mpmath.__version__
'0.19'
$ sudo python -m easy_install sympy
$ python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> sympy.__version__
'0.7.6'
now plot() shows the graph.
in future, use python -m easy_install to install the latest version, and not apt-get which has old versions it seems.
user#ubuntu:~/Documents/MongoDB$ python2
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import Connection
>>>
user#ubuntu:~/Documents/MongoDB$ python3
Python 3.1.2 (r312:79147, Sep 27 2010, 09:45:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import Connection
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pymongo
Question> I don't know why pymongo doesn't work with my python 3. Any idea?
// Updated solution for this OP based on the helps below //
First, still don't understand why this post got down-vote!
Step1> http://pypi.python.org/pypi/pymongo3#downloads
Step2> Download pymongo3-1.9b1.tar.gz
Step3> unzip it by using tar xzf pymongo3-1.9b1.tar.gz
Step4> cd pymongo3-1.9b1
Step5> sudo python3 setup.py install
If you followed all above instructions, the pymongo should be ready
for your P3:)
Probably because you didn't install it for Python 3. You have to install a module for each version of Python that you have in order to access it from that version.This is all assuming that the module is compatible with each version of Python that you have.