Python 3.4.0 with MySQL database - python

I have installed Python version 3.4.0 and I would like to do a project with MySQL database. I downloaded and tried installing MySQLdb, but it wasn't successful for this version of Python.
Any suggestions how could I fix this problem and install it properly?

MySQLdb does not support Python 3 but it is not the only MySQL driver for Python.
mysqlclient is essentially just a fork of MySQLdb with Python 3 support merged in (and a few other improvements).
PyMySQL is a pure python MySQL driver, which means it is slower, but it does not require a compiled C component or MySQL libraries and header files to be installed on client machines. It has Python 3 support.
Another option is simply to use another database system like PostgreSQL.

Use mysql-connector-python. I prefer to install it with pip from PyPI:
pip install --allow-external mysql-connector-python mysql-connector-python
Have a look at its documentation and examples.
If you are going to use pooling make sure your database has enough connections available
as the default settings may not be enough.

Install pip:
apt-get install pip
For acess MySQL from Python, install:
pip3 install mysqlclient

There is a Ubuntu solution available either through the Ubuntu Software Center or through the Synaptic Package Manager. This will connect Python version 3.4.0 to MySQL. Download "python3-mysql.connector" version 1.1.6-1.
Note that the connection syntax does not use "MySQLdb".
Instead read: Connecting to MySQL Using Connector/Python

sudo apt-get install python3-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install zlib1g-dev
sudo pip3 install mysqlclient
that worked for me!

I solved it this way:
download the zipped package from here and follow this set of instructions:
unzip /path/to/downloads/folder/mysql-connector-python-VER.zip
In case u got a .gz u can use ->
tar xzf mysql-connector-python-VER.tar.gz
And then:
cd mysql-connector-python-VER # move into the directory
sudo python3 setup.py install # NOTICE I USED PYTHON3 INSTEAD OF PYTHON
You can read about it here

It seems that at the moment Ubuntu 15.10 has a but with python3 and pip3.
As elaborated in this article.
The problem makes pip3 install to python3.5 while python3 is actually running python3.4 :(
Until a proper solution will be available via the updates you can do one of the following:
run
python3 -m pip install pymysql
instead of
pip3 install pymysql
(or any other package)
Now
import pymysql
should work in python3 and in idle3.
Alternatively, if you explicitly need 3.5 you can use explicit python3.5 instead of python3. but idle3 will still point to 3.4...

mysqlclient is a fork of MySQLdb and can serve as a drop-in replacement with Python 3.4 support. If you have trouble building it on Windows, you can download it from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages

Alternatively, you can use mysqlclient or oursql. For oursql, use the oursql py3k series as my link points to.

Maybe you can use a work around and try something like:
import datetime
#import mysql
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1',user = 'someUser', passwd = 'foobar',db = 'foobardb')
cursor = conn.cursor()

for fedora and python3 use: dnf install mysql-connector-python3

Related

How to install "pymssql" module in python 2.7?

I have already installed pymssql module and it was installed successfully.
But when I try to run my script it still ask to install pymssql module.
Not sure if I am missing anything here.
ReferencePyMSSQL
Try installing FREETDS before running "pip install pymssql"
Run this to install FREETDS > sudo apt-get install freetds-dev
You need to have FreeTDS installed on your computer. However, you can run into an error using the latest FreeTDS version.
If you are using Mac OS, you can run the following:
brew unlink freetds;
brew install freetds#0.91;
brew link --force freetds#0.91;
Then install pymssql: sudo pip install pymssql
This will force a symlink to the older version of freeTDS, and in my opinion, more stable.
Some more information can be found here: https://github.com/pymssql/pymssql/issues/432
Cheers.

pg_config executable not found on Windows 7 with PostgreSQL installed [duplicate]

I'm using virtualenv and I need to install "psycopg2".
I have done the following:
pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
And I have the following messages:
Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
Running setup.py egg_info for package from http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
Complete output from command python setup.py egg_info:
running egg_info
creating pip-egg-info\psycopg2.egg-info
writing pip-egg-info\psycopg2.egg-info\PKG-INFO
writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt
writing dependency_links to pip-egg-info\psycopg2.egg-info\dependency_links.txt
writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'
warning: manifest_maker: standard file '-c' not found
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in C:\Documents and Settings\anlopes\Application Data\pip\p
ip.log
My question, I only need to do this to get the psycopg2 working?
python setup.py build_ext --pg-config /path/to/pg_config build ...
Note: Since a while back, there are binary wheels for Windows in PyPI, so this should no longer be an issue for Windows users. Below are solutions for Linux, Mac users, since lots of them find this post through web searches.
Option 1
Install the psycopg2-binary PyPI package instead, it has Python wheels for Linux and Mac OS.
pip install psycopg2-binary
Option 2
Install the prerequsisites for building the psycopg2 package from source:
Debian/Ubuntu
Python 3
sudo apt install libpq-dev python3-dev
You might need to install python3.8-dev or similar for e.g. Python 3.8.
Python 2
sudo apt install libpq-dev python-dev
If that's not enough, try
sudo apt install build-essential
or
sudo apt install postgresql-server-dev-all
as well before installing psycopg2 again.
CentOS 6
See Banjer's answer
macOS
See nichochar's answer
On CentOS, you need the postgres dev packages:
sudo yum install python-devel postgresql-devel
That was the solution on CentOS 6 at least.
If you're on a mac you can use homebrew
brew install postgresql
And all other options are here: http://www.postgresql.org/download/macosx/
On Mac Mavericks with Postgres.app version 9.3.2.0 RC2 I needed to use the following code after installing Postgres:
sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin pip install psycopg2
I recently configured psycopg2 on a windows machine. The easiest install is using a windows executable binary. You can find it at http://stickpeople.com/projects/python/win-psycopg/.
To install the native binary in a virtual envrionment, use easy_install:
C:\virtualenv\Scripts\> activate.bat
(virtualenv) C:\virtualenv\Scripts\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe
For Python 3 you should use sudo apt-get install libpq-dev python3-dev under Debian.
This is what worked for me (On RHEL, CentOS:
sudo yum install postgresql postgresql-devel python-devel
And now include the path to your postgresql binary dir with you pip install:
sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2
Make sure to include the correct path. Thats all :)
UPDATE: For python 3, please install python3-devel instead of python-devel
The answers so far are too much like magic recipes. The error that you received tells you that pip cannot find a needed part of the PostgreSQL Query library. Possibly this is because you have it installed in a non-standard place for your OS which is why the message suggests using the --pg-config option.
But a more common reason is that you don't have libpq installed at all. This commonly happens on machines where you do NOT have PostgreSQL server installed because you only want to run client apps, not the server itself. Each OS/distro is different, for instance on Debian/Ubuntu you need to install libpq-dev. This allows you to compile and link code against the PostgreSQL Query library.
Most of the answers also suggest installing a Python dev library. Be careful. If you are only using the default Python installed by your distro, that will work, but if you have a newer version, it could cause problems. If you have built Python on this machine then you already have the dev libraries needed for compiling C/C++ libraries to interface with Python. As long as you are using the correct pip version, the one installed in the same bin folder as the python binary, then you are all set. No need to install the old version.
If you using Mac OS, you should install PostgreSQL from source.
After installation is finished, you need to add this path using:
export PATH=/local/pgsql/bin:$PATH
or you can append the path like this:
export PATH=.../:usr/local/pgsql/bin
in your .profile file or .zshrc file.
This maybe vary by operating system.
You can follow the installation process from http://www.thegeekstuff.com/2009/04/linux-postgresql-install-and-configure-from-source/
On Debian/Ubuntu:
First install and build dependencies of psycopg2 package:
# apt-get build-dep python-psycopg2
Then in your virtual environment, compile and install psycopg2 module:
(env)$ pip install psycopg2
Run below commands and you should be fine
$ apt-get update
$ apt install python3-dev libpq-dev
$ pip3 install psycopg2
I've done this before where in windows you install first into your base python installation.
Then, you manually copy the installed psycopg2 to the virtualenv install.
It's not pretty, but it works.
Before you can install psycopg2 you will need to install the python-dev package.
If you're working from Linux (and possibly other systems but i can't speak from experience) you will need to make sure to be quite exact about what version of python your running when installing the dev package.
For example when I used the command:
sudo apt-get install python3-dev
I still ran into the same error when trying to
pip install psycopg2
As I am using python 3.7 I needed to use the command
sudo apt-get install python3.7-dev
Once I did this I ran into no more issues. Obviously if your on python version 3.5 you would change that 7 to a 5.
Besides installing the required packages, I also needed to manually add PostgreSQL bin directory to PATH.
$vi ~/.bash_profile
Add PATH=/usr/pgsql-9.2/bin:$PATH before export PATH.
$source ~/.bash_profile
$pip install psycopg2
For MacOS,
Use the below command to install psycopg2, works like charm!!!
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
On windows XP you get this error if postgres is not installed ...
I installed Postgresql92 using the RedHat / CentOS repository on PG's downloads site http://www.postgresql.org/download/linux/redhat/
To get pg_config, I had to add /usr/pgsql-9.2/bin to PATH.
On Fedora 24: For Python 3.x
sudo dnf install postgresql-devel python3-devel
sudo dnf install redhat-rpm-config
Activate your Virtual Environment:
pip install psycopg2
Psycopg2 Depends on Postgres Libraries.
On Ubuntu You can use:
apt-get install libpq-dev
Then:
pip install psycopg2
I've been battling with this for days, and have finally figured out how to get the "pip install psycopg2" command to run in a virtualenv in Windows (running Cygwin).
I was hitting the "pg_config executable not found." error, but I had already downloaded and installed postgres in Windows. It installed in Cygwin as well; running "which pg_config" in Cygwin gave "/usr/bin/pg_config", and running "pg_config" gave sane output -- however the version installed with Cygwin is:
VERSION = PostgreSQL 8.2.11
This won't work with the current version of psycopg2, which appears to require at least 9.1. When I added "c:\Program Files\PostgreSQL\9.2\bin" to my Windows path, the Cygwin pip installer was able to find the correct version of PostgreSQL, and I was able to successfully install the module using pip. (This is probably preferable to using the Cygwin version of PostgreSQL anyway, as the native version will run much quicker).
On OpenSUSE 13.2, this fixed it:
sudo zypper in postgresql-devel
For lowly Windows users were stuck having to install psycopg2 from the link below, just install it to whatever Python installation you have setup. It will place the folder named "psycopg2" in the site-packages folder of your python installation.
After that, just copy that folder to the site-packages directory of your virtualenv and you will have no problems.
here is the link you can find the executable to install psycopg2
http://www.lfd.uci.edu/~gohlke/pythonlibs/
On Ubuntu I just needed the postgres dev package:
sudo apt-get install postgresql-server-dev-all
*Tested in a virtualenv
I could install it in a windows machine and using Anaconda/Spyder with python 2.7 through the following commands:
!pip install psycopg2
Then to establish the connection to the database:
import psycopg2
conn = psycopg2.connect(dbname='dbname',host='host_name',port='port_number', user='user_name', password='password')
In Arch base distributions:
sudo pacman -S python-psycopg2
pip2 install psycopg2 # Use pip or pip3 to python3
On OSX 10.11.6 (El Capitan)
brew install postgresql
PATH=$PATH:/Library/PostgreSQL/9.4/bin pip install psycopg2
On OSX with macports:
sudo port install postgresql96
export PATH=/opt/local/lib/postgresql96/bin:$PATH
if pip is not working than you can download .whl file from here https://pypi.python.org/pypi/psycopg2
extract it..
than python setup.py install
I was having this problem, the main reason was with 2 equal versions installed. One by postgres.app and one by HomeBrew.
If you choose to keep only the APP:
brew unlink postgresql
pip3 install psycopg2
Installation on MacOS
Following are the steps, which worked for me and my team members while installing psycopg2 on Mac OS Big Sur and which we have extensively tested for Big Sur. Before starting make sure you have the Xcode command-line tool installed. If not, then install it from the Apple Developer site. The below steps assume you have homebrew installed. If you have not installed homebrew then install it. Last but not the least, it also assumes you already have PostgreSQL installed in your system, if not then install it. Different people have different preferences but the default installation method on the official PostgreSQL site via Enterprise DB installer is the best method for the majority of people.
Put up the linkage to pg_config file in your .zshrc file by: export PATH="$PATH:/Library/PostgreSQL/12/bin:$PATH". This way you are having linkage with the pg_config file in the /Library/PostgreSQL/12/bin folder. So if your PostgreSQL installation is via other means, like Postgres.app or Postgres installation via homebrew, then you need to have in your .zshrc file the link to pg_config file from the bin folder of that PostgreSQL installation as psycopg2 relies on that.
Install OpenSSL via Homebrew using the command brew install openssl. The reason for this is that libpq, the library which is the basis of psycopg2, uses openssl - psycopg2 doesn't use it directly. After installing put the following commands in your .zshrc file:
export PATH="/usr/local/opt/openssl#1.1/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl#1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl#1.1/include"
By doing this you are creating necessary linkages in your directory. These commands are suggested by brew while you install openssl and have been directly picked up from there.
Now comes the most important step, which is to install libpq using the command brew install libpq. This installs libpq library. As per the documentation
libpq is the C application programmer's interface to PostgreSQL. libpq is a set of library functions that allow client programs to pass queries to the PostgreSQL backend server and to receive the results of these queries.
Link libpq using brew link libpq, if this doesn't work then use the command: brew link libpq --force.
Also put in your .zshrc file the following export PATH="/usr/local/opt/libpq/bin:$PATH". This creates all the necessary linkages for libpq library .
Now restart the terminal or use the following command source ~/.zshrc.
This works even when you are working in conda environment.
N.B. pip install psycopg2-binaryshould be avoided because as per the developers of the psycopg2 library
The use of the -binary packages in production is discouraged because in the past they proved unreliable in multithread environments. This might have been fixed in more recent versions but I have never managed to reproduce the failure.

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

installing packages over pythonbrew

I installed python 2.7 with python brew. How do I install packages to work with it? I installed MySQLdb with synaptic, but I am unable to import it in python 2.7.
Switch to 2.7:
pythonbrew switch 2.7
Curl and run get-pip to get the correct version of pip for 2.7:
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python get-pip.py
This will install the version of pip for 2.7. Check it by doing:
pip --version
Turn off pythonbrew:
pythonbrew off
Check the version of pip again, and it should be using the one for your default Python:
pip --version
If all is good, then switch back to 2.7 in pythonbrew and install mysql-python for 2.7:
pythonbrew switch 2.7
pip install mysql-python
Check to see that it is installed for 2.7:
pip freeze
Pip freeze will give you a listing of all installed libraries for the current active version of Python.
You should try to install pip, which is a recursive acronym: Pip Installs Packages. This thread talks about installing it on windows, on Ubuntu I did sudo apt get install pip.
Ok, your problem is that "mysqldb" is not a python package. You need to use MySQLdb as a backend, or simply install sqlite3 and import that into Python, which is a module that mimics SQL. If you end up using an actual full-on database, like MySQLdb or PostgreSQL, you'll probably need to install SQLAlchemy, which is a Python module to interface with those.
You need to install a version of pip for each Python version. Do you have easy install available? If so you can do
easy_install-2.7 pip
Is there a specific reason that you are installing Python via home brew though? You do know that Ubuntu has as a package.
sudo apt-get install python2.7
Will give you a version of Python that is already nicely set up.
I also believe that you should be trying to install the Python package called mysql-python.
pip install mysql-python
MySQLDB is not a Python package. It's the actual database.
Agree with #leta-rogers. However, I didn't have to install pip separately. Installing python using pythonbrew installed pip (for python 2.7) for me as well:
pythonbrew install 2.7
pythonbrew switch 2.7
pip install mysql-python

How to install a python package via yum without all the dependencies

I have already compiled couchdb 1.2 and I am using it. I now need to use couchdb python and I am trying to do yum install python-couchdb, but yum wants to install couchdb 1.1.1 because it propably doesnt find my compiled version. How can I tell yum to install only the python-couchdb package without the deps?
Python installs with easy_install for getting python eggs like python-couchdb. However, I and many people prefer a wrapper called PIP that allows better management of eggs.
On linux you can install it like so.
Or if you prefer windows.
Once you have pip you can install python-couchdb by calling:
sudo pip install couchdb
That shouldn't bork your couch installation.

Categories

Resources