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!'
>>>
Related
I'm working through the "Python Testing with Pytest" book.
I'm importing tinydb module to run assertions against it and terminal is giving
me error:
test_add_variety.py:1: in <module>
import pytest, tasks, tinydb
E ModuleNotFoundError: No module named 'tinydb'
however it is installed:
pip3 freeze:
click==7.1.2
six==1.16.0
tasks==0.1.0
tinydb==3.15.1
and I can also confirm by firing up Python interpreter:
(venv) mark#fid:~/p_test/ch2$ python
Python 3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tinydb
>>>
Please note that I'm running these with virtual environment activated.
Any suggestions why Pytest can't import tinydb module ?
When i use python in terminal ;
pi#raspberrypi:~ $ python
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
2020-07-26 12:52:03.075590: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory
>>>
But i tried run my py file
pi#raspberrypi:~ $ sudo python /home/pi/tfdeneme.py
pygame 1.9.4.post1
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/pi/tfdeneme.py", line 17, in <module>
import tensorflow
ModuleNotFoundError: No module named 'tensorflow'
Im using tensorflow 2.2.0 on my windows machine, on raspberry pi tensorflow version =
pi#raspberrypi:~ $ python
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-07-26 12:53:49.504578: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory
>>> tf.__version__
'1.14.0'
Whay should i do to run my py file ?
I was having issues with TF 2.2.0 with python 3.7 and hence downgraded to python 3.6 and now it seems to be working. Please note i am on a Mac though
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>
I was having trouble about python unicode and so I reinstalled python on /usr/local/bin/python with option "--enable-unicode=ucs4". I added to ~/.bashrc all the paths to python modules and when I run as common user I'm able to import modules, but when I'm as sudo I can't.
iury#buzios:~$ /usr/local/bin/python
Python 2.7.6 (default, Aug 20 2015, 11:57:25)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
iury#buzios:~$ sudo /usr/local/bin/python
Python 2.7.6 (default, Aug 20 2015, 11:57:25)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
As their name indicates, Environmental Variables are assigned to your own user. That means that if you are running program as root (using sudo), they won't be "assigned" to the program because it is running in the environment of the root user. A work around is to set the environment variables after the sudo command like this: sudo env PATH=$PATH VAR1=SOME_VALUE VAR2=SOME_VALUE
As the answer in this question indicates, you can add this export in your ./bashrc as a workaround:
alias sudo='sudo env PYTHONPATH=[PATH] PYTHON=[OTHERPATH] ... ./thescript.py
Also, as mentioned in the comments, make sure you need to run python as sudo, as it is not recommended when not REALLY needed.
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.