How to fix "Can't find local Berkeley DB" error? - python

I'm trying to install scrapy-deltafetch in a virtual-environment (as described here) on my new raspberry pi 3 with Raspbian.
When I'm running pip install scrapy-deltafetch in my virtualenv, I'm getting something like this:
python setup.py egg_info:
Can't find a local Berkeley DB installation
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ib6d93/bsddb3/
However when I'm running sudo pip install scrapy-deltafetch outside of my virtual-environment everything works fine.
Does anybody has an idea of how to install scrapy-deltafetch in the virtualenvironment?

Your system is missing Berkeley DB, which is used by DeltaFetch to store requests data.
So, first install Berkeley DB in your system (found this tutorial in a quick search).
After that, you have to install the bsddb3 Python package (you can follow the instructions from this answer).
Edit (2020):
This should work:
$ sudo apt install libdb-dev
$ pip install bsddb3

In case of my Ubuntu 18.04 missing system library is called libdb-dev.
> sudo apt-get install libdb-dev
This fixed the problem for me.

Related

Docker: pg_config is required to build psycopg2 from source [duplicate]

I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2:
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 in /tmp/pip-build/psycopg2
But the problem is pg_config is actually in my PATH; it runs without any problem:
$ which pg_config
/usr/pgsql-9.1/bin/pg_config
I tried adding the pg_config path to the setup.cfg file and building it using the source files I downloaded from their website (http://initd.org/psycopg/) and I get the following error message!
Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'
But it is actually THERE!!!
I am baffled by these errors. Can anyone help please?
By the way, I sudo all the commands. Also I am on RHEL 5.5.
pg_config is in postgresql-devel (libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)
On Mac OS X, I solved it using the homebrew package manager
brew install postgresql
Have you installed python-dev?
If you already have, try also installing libpq-dev
sudo apt-get install libpq-dev python-dev
From the article: How to install psycopg2 under virtualenv
Also on OSX. Installed Postgress.app from http://postgresapp.com/ but had the same issue.
I found pg_config in that app's contents and added the dir to $PATH.
It was at /Applications/Postgres.app/Contents/Versions/latest/bin. So this worked: export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH".
You can install pre-compiled binaries on any platform with pip or conda:
python -m pip install psycopg2-binary
or
conda install psycopg2
Please be advised that the psycopg2-binary pypi page recommends building from source in production:
The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources
To use the package built from sources, use python -m pip install psycopg2. That process will require several dependencies (documentation) (emphasis mine):
A C compiler.
The Python header files. They are usually installed in a package such as python-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.
The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them.
The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config --version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH:
$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
You only need pg_config to compile psycopg2, not for its regular usage.
Once everything is in place it’s just a matter of running the standard:
$ pip install psycopg2
or, from the directory containing the source code:
$ python setup.py build
$ python setup.py install
For ubuntu users, this is the solution:
sudo apt install libpq-dev
It worked for me.
On alpine, the library containing pg_config is postgresql-dev. To install, run:
apk add postgresql-dev
This is what worked for me on CentOS, first install:
sudo yum install postgresql postgresql-devel python-devel
On Ubuntu just use the equivilent apt-get packages.
sudo apt-get install postgresql postgresql-dev python-dev
And now include the path to your postgresql binary dir with you pip install, this should work for either Debain or RHEL based Linux:
sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2
Make sure to include the correct path. Thats all :)
You have to install libpq-dev/postgresql-libs, which is the header files and static library for compiling C programs to link with the libpq library in order to communicate with a PostgreSQL database backend.
On Arch this will run:
$ sudo pacman -S postgresql-libs
On Debian and Ubuntu:
$ sudo apt-get install libpq-dev
On Mac OS X:
$ brew install postgresql
On Red Hat/CentOS/Fedora:
$ sudo yum install postgresql-devel
apt-get build-dep python-psycopg2
Just to sum up, I also faced exactly same problem. After reading a lot of stackoverflow posts and online blogs, the final solution which worked for me is this:
1) PostgreSQL(development or any stable version) should be installed before installing psycopg2.
2) The pg_config file (this file normally resides in the bin folder of the PostgreSQL installation folder) PATH had to be explicitly setup before installing psycopg2. In my case, the installation PATH for PostgreSQL is:
/opt/local/lib/postgresql91/
so in order to explicitly set the PATH of pg_config file, I entered following command in my terminal:
PATH=$PATH:/opt/local/lib/postgresql91/bin/
This command ensures that when you try to pip install psycopg2, it would find the PATH to pg_config automatically this time.
I have also posted a full error with trace and its solution on my blog which you may want to refer. Its for Mac OS X but the pg_config PATH problem is generic and applicable to Linux also.
You should add python requirements used in Postgres on Ubuntu. Run:
sudo apt-get install libpq-dev python-dev
sudo apt-get install libpq-dev works for me on Ubuntu 15.4
I had this issue because I didn't had a postgres install. If you have brew install run
brew install postgresql
This should fix the issue.
For those running OS X, this solution worked for me:
1) Install Postgres.app:
http://www.postgresql.org/download/macosx/
2) Then open the Terminal and run this command, replacing where it says {{version}} with the Postgres version number:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{{version}}/bin
e.g.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
On Linux Mint sudo apt-get install libpq-dev worked for me.
UPDATE /etc/yum.repos.d/CentOS-Base.repo, [base] and [updates] sections
ADD exclude=postgresql*
curl -O http://yum.postgresql.org/9.1/redhat/rhel-6-i386/pgdg-centos91-9.1-4.noarch.rpmr
rpm -ivh pgdg-centos91-9.1-4.noarch.rpm
yum install postgresql
yum install postgresql-devel
PATH=$PATH:/usr/pgsql-9.1/bin/
pip install psycopg2
Simply run the following:
sudo apt install libpq-dev
Fixed the issue for me
Try to add it to PATH:
PATH=$PATH:/usr/pgsql-9.1/bin/ ./pip install psycopg2
Ali's solution worked for me but I was having trouble finding the bin folder location. A quick way to find the path on Mac OS X is to open psql (there's a quick link in the top menu bar). This will open a separate terminal window and on the second line the path of your Postgres installation will appear like so:
My-MacBook-Pro:~ Me$ /Applications/Postgres93.app/Contents/MacOS/bin/psql ; exit;
Your pg_config file is in that bin folder. Therefore, before installing psycopg2 set the path of the pg_config file:
PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin/
or for newer version:
PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin
Then install psycopg2.
I'm going to leave this here for the next unfortunate soul who can't get around this problem despite all the provided solutions. Simply use sudo pip3 install psycopg2-binary
You need to upgrade your pip before installing psycopg2. Use this command
pip install --upgrade pip
On MacOS, the simplest solution will be to symlink the correct binary, that is under the Postgres package.
sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config
This is fairly harmless, and all the applications will be able to use it system wide, if required.
On Mac OS X and If you are using Postgres App (http://postgresapp.com/):
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
No need to specify version of Postgres in this command. It will be always pointed to latest.
and do
pip install psycopg2
P.S: If Changes doesn't reflect you may need to restart the Terminal/Command prompt
Source
Installing python-psycopg2 solved it for me on Arch Linux:
pacman -S python-psycopg2
On Windows,
You may want to install the Windows port of Psycopg, which is recommended in psycopg's documentation.
Just solved the problem in Cent OS 7 by:
export PATH=$PATH:/usr/pgsql-9.5/bin
make sure your PostgreSql version matches the right version above.
This was partly suggested before, adding it here for clarity.
From the documentation at https://www.psycopg.org/docs/install.html.
they suggest running: $ pip install psycopg2-binary
That solved the issue for me.
Here, for OS X completeness: if you install PostgreSQL from MacPorts, pg_config will be in /opt/local/lib/postgresql94/bin/pg_config.
When you installed MacPorts, it already added /opt/local/bin to your PATH.
So, this will fix the problem:
$ sudo ln -s /opt/local/lib/postgresql94/bin/pg_config /opt/local/bin/pg_config
Now pip install psycopg2 will be able to run pg_config without issues.
To those on macOS Catalina using the zsh shell who have also installed the postgres app:
Open your ~/.zshrc file, and add the following line:
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
Then close all your terminals, reopen them, and you'll have resolved your problem.
If you don't want to close your terminals, simply enter source ~/.zshrc in whatever terminal you'd like to keep working on.

Can't fix "zipimport.ZipImportError: can't decompress data; zlib not available" when I type in "python3.6 get-pip.py"

I was trying to install Django. Turns out that course's teacher said that we will be working with Python 3.6
I install Python 3.6. Now it's my default, it somewhat replaced the last version I had; which is Python 3.5.
Everything ok until that. But when I want to install Django doing
"pip3 install django", it tells me that the module is already satisfied and therefore installed.
I run "python3" command into my terminal. It runs Python 3.6. I try to import Django, and boom... "No module named 'django'".
Then I realized pip3 was actually installing my modules into Python 3.5 and not 3.6. So what I do is to install pip in Python 3.6.
I download get-pip.py and proceed to execute it with Python 3.6 typing in "python3.6 get-pip.py".
Here is when the damn "zipimport.ZipImportError: can't decompress data; zlib not available" goes in. I've tried a ton of things and no one of them fixed the %^$! problem. I'm really tired.
What I have already tried:
python3.6 -m pip install django, which output is "/usr/local/bin/python3.6: No module named pip"
apt install zlib, which output is "E: Unable to locate package zlib"
apt install zlib1g-dev, which says that it's already installed; the problem persists though.
I also came across this problem (while creating a simple installer for pyenv). Here's how I solved it for Mac and Linux:
Ubuntu 20.04, 18.04
You need the zlib development files, and probably zlib itself too:
sudo apt install -y zlib1g-dev zlibc
If you're missing zlib, it's likely that the next problem you'll run into is with openssl, so it's probably best to get that now as well:
sudo apt install -y libssl-dev
sudo apt install -y libssl1.1 || sudo apt install -y libssl1.0
macOS
I believe this comes with XCode CLI Tools (or at least I didn't have to custom install it Big Sur):
xcode-select --install
For me it worked in RHEL:
$ yum install zlib-devel
Suggested solutions (installing zlib1g-dev or zlib-devel) seem to resolve the issue in most cases. Here is one edge case I've encountered recently: whatever you are trying to run might use zlib via symlink which might be broken.
In my case I was trying to run a build of a 3rd-party software which already had python and all necessary libs being emebedded into it. It was packaged as a tar.gz archive. Unpacking the archive on a Windows machine and then copying the contents to another linux machine destroyed all the symlinks (if you do ls -l in a folder with symlinks you would see that all of them have size 0 and do not point to anything). Copying tar.gz to the linux machine directly and unpacking it there resolved the issue.
P.S. I know it's an edge case scenario but it took me and one more developer quite a while to figure it out so I think it's worth mentioning here, just in case someone gets as unlucky as I got.
Its solves my issue for centos 7.6 :-
yum install zlib-deve

Error installing psycopg2: pg_config executable not found [duplicate]

I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2:
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 in /tmp/pip-build/psycopg2
But the problem is pg_config is actually in my PATH; it runs without any problem:
$ which pg_config
/usr/pgsql-9.1/bin/pg_config
I tried adding the pg_config path to the setup.cfg file and building it using the source files I downloaded from their website (http://initd.org/psycopg/) and I get the following error message!
Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'
But it is actually THERE!!!
I am baffled by these errors. Can anyone help please?
By the way, I sudo all the commands. Also I am on RHEL 5.5.
pg_config is in postgresql-devel (libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)
On Mac OS X, I solved it using the homebrew package manager
brew install postgresql
Have you installed python-dev?
If you already have, try also installing libpq-dev
sudo apt-get install libpq-dev python-dev
From the article: How to install psycopg2 under virtualenv
Also on OSX. Installed Postgress.app from http://postgresapp.com/ but had the same issue.
I found pg_config in that app's contents and added the dir to $PATH.
It was at /Applications/Postgres.app/Contents/Versions/latest/bin. So this worked: export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH".
You can install pre-compiled binaries on any platform with pip or conda:
python -m pip install psycopg2-binary
or
conda install psycopg2
Please be advised that the psycopg2-binary pypi page recommends building from source in production:
The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources
To use the package built from sources, use python -m pip install psycopg2. That process will require several dependencies (documentation) (emphasis mine):
A C compiler.
The Python header files. They are usually installed in a package such as python-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.
The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them.
The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config --version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH:
$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
You only need pg_config to compile psycopg2, not for its regular usage.
Once everything is in place it’s just a matter of running the standard:
$ pip install psycopg2
or, from the directory containing the source code:
$ python setup.py build
$ python setup.py install
For ubuntu users, this is the solution:
sudo apt install libpq-dev
It worked for me.
On alpine, the library containing pg_config is postgresql-dev. To install, run:
apk add postgresql-dev
This is what worked for me on CentOS, first install:
sudo yum install postgresql postgresql-devel python-devel
On Ubuntu just use the equivilent apt-get packages.
sudo apt-get install postgresql postgresql-dev python-dev
And now include the path to your postgresql binary dir with you pip install, this should work for either Debain or RHEL based Linux:
sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2
Make sure to include the correct path. Thats all :)
You have to install libpq-dev/postgresql-libs, which is the header files and static library for compiling C programs to link with the libpq library in order to communicate with a PostgreSQL database backend.
On Arch this will run:
$ sudo pacman -S postgresql-libs
On Debian and Ubuntu:
$ sudo apt-get install libpq-dev
On Mac OS X:
$ brew install postgresql
On Red Hat/CentOS/Fedora:
$ sudo yum install postgresql-devel
apt-get build-dep python-psycopg2
Just to sum up, I also faced exactly same problem. After reading a lot of stackoverflow posts and online blogs, the final solution which worked for me is this:
1) PostgreSQL(development or any stable version) should be installed before installing psycopg2.
2) The pg_config file (this file normally resides in the bin folder of the PostgreSQL installation folder) PATH had to be explicitly setup before installing psycopg2. In my case, the installation PATH for PostgreSQL is:
/opt/local/lib/postgresql91/
so in order to explicitly set the PATH of pg_config file, I entered following command in my terminal:
PATH=$PATH:/opt/local/lib/postgresql91/bin/
This command ensures that when you try to pip install psycopg2, it would find the PATH to pg_config automatically this time.
I have also posted a full error with trace and its solution on my blog which you may want to refer. Its for Mac OS X but the pg_config PATH problem is generic and applicable to Linux also.
You should add python requirements used in Postgres on Ubuntu. Run:
sudo apt-get install libpq-dev python-dev
sudo apt-get install libpq-dev works for me on Ubuntu 15.4
I had this issue because I didn't had a postgres install. If you have brew install run
brew install postgresql
This should fix the issue.
For those running OS X, this solution worked for me:
1) Install Postgres.app:
http://www.postgresql.org/download/macosx/
2) Then open the Terminal and run this command, replacing where it says {{version}} with the Postgres version number:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{{version}}/bin
e.g.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
On Linux Mint sudo apt-get install libpq-dev worked for me.
UPDATE /etc/yum.repos.d/CentOS-Base.repo, [base] and [updates] sections
ADD exclude=postgresql*
curl -O http://yum.postgresql.org/9.1/redhat/rhel-6-i386/pgdg-centos91-9.1-4.noarch.rpmr
rpm -ivh pgdg-centos91-9.1-4.noarch.rpm
yum install postgresql
yum install postgresql-devel
PATH=$PATH:/usr/pgsql-9.1/bin/
pip install psycopg2
Simply run the following:
sudo apt install libpq-dev
Fixed the issue for me
Try to add it to PATH:
PATH=$PATH:/usr/pgsql-9.1/bin/ ./pip install psycopg2
Ali's solution worked for me but I was having trouble finding the bin folder location. A quick way to find the path on Mac OS X is to open psql (there's a quick link in the top menu bar). This will open a separate terminal window and on the second line the path of your Postgres installation will appear like so:
My-MacBook-Pro:~ Me$ /Applications/Postgres93.app/Contents/MacOS/bin/psql ; exit;
Your pg_config file is in that bin folder. Therefore, before installing psycopg2 set the path of the pg_config file:
PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin/
or for newer version:
PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin
Then install psycopg2.
I'm going to leave this here for the next unfortunate soul who can't get around this problem despite all the provided solutions. Simply use sudo pip3 install psycopg2-binary
You need to upgrade your pip before installing psycopg2. Use this command
pip install --upgrade pip
On MacOS, the simplest solution will be to symlink the correct binary, that is under the Postgres package.
sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config
This is fairly harmless, and all the applications will be able to use it system wide, if required.
On Mac OS X and If you are using Postgres App (http://postgresapp.com/):
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
No need to specify version of Postgres in this command. It will be always pointed to latest.
and do
pip install psycopg2
P.S: If Changes doesn't reflect you may need to restart the Terminal/Command prompt
Source
Installing python-psycopg2 solved it for me on Arch Linux:
pacman -S python-psycopg2
On Windows,
You may want to install the Windows port of Psycopg, which is recommended in psycopg's documentation.
Just solved the problem in Cent OS 7 by:
export PATH=$PATH:/usr/pgsql-9.5/bin
make sure your PostgreSql version matches the right version above.
This was partly suggested before, adding it here for clarity.
From the documentation at https://www.psycopg.org/docs/install.html.
they suggest running: $ pip install psycopg2-binary
That solved the issue for me.
Here, for OS X completeness: if you install PostgreSQL from MacPorts, pg_config will be in /opt/local/lib/postgresql94/bin/pg_config.
When you installed MacPorts, it already added /opt/local/bin to your PATH.
So, this will fix the problem:
$ sudo ln -s /opt/local/lib/postgresql94/bin/pg_config /opt/local/bin/pg_config
Now pip install psycopg2 will be able to run pg_config without issues.
To those on macOS Catalina using the zsh shell who have also installed the postgres app:
Open your ~/.zshrc file, and add the following line:
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
Then close all your terminals, reopen them, and you'll have resolved your problem.
If you don't want to close your terminals, simply enter source ~/.zshrc in whatever terminal you'd like to keep working on.

pg_config executable not found

I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2:
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 in /tmp/pip-build/psycopg2
But the problem is pg_config is actually in my PATH; it runs without any problem:
$ which pg_config
/usr/pgsql-9.1/bin/pg_config
I tried adding the pg_config path to the setup.cfg file and building it using the source files I downloaded from their website (http://initd.org/psycopg/) and I get the following error message!
Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'
But it is actually THERE!!!
I am baffled by these errors. Can anyone help please?
By the way, I sudo all the commands. Also I am on RHEL 5.5.
pg_config is in postgresql-devel (libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)
On Mac OS X, I solved it using the homebrew package manager
brew install postgresql
Have you installed python-dev?
If you already have, try also installing libpq-dev
sudo apt-get install libpq-dev python-dev
From the article: How to install psycopg2 under virtualenv
Also on OSX. Installed Postgress.app from http://postgresapp.com/ but had the same issue.
I found pg_config in that app's contents and added the dir to $PATH.
It was at /Applications/Postgres.app/Contents/Versions/latest/bin. So this worked: export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH".
You can install pre-compiled binaries on any platform with pip or conda:
python -m pip install psycopg2-binary
or
conda install psycopg2
Please be advised that the psycopg2-binary pypi page recommends building from source in production:
The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources
To use the package built from sources, use python -m pip install psycopg2. That process will require several dependencies (documentation) (emphasis mine):
A C compiler.
The Python header files. They are usually installed in a package such as python-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.
The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them.
The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config --version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH:
$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
You only need pg_config to compile psycopg2, not for its regular usage.
Once everything is in place it’s just a matter of running the standard:
$ pip install psycopg2
or, from the directory containing the source code:
$ python setup.py build
$ python setup.py install
For ubuntu users, this is the solution:
sudo apt install libpq-dev
It worked for me.
On alpine, the library containing pg_config is postgresql-dev. To install, run:
apk add postgresql-dev
This is what worked for me on CentOS, first install:
sudo yum install postgresql postgresql-devel python-devel
On Ubuntu just use the equivilent apt-get packages.
sudo apt-get install postgresql postgresql-dev python-dev
And now include the path to your postgresql binary dir with you pip install, this should work for either Debain or RHEL based Linux:
sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2
Make sure to include the correct path. Thats all :)
You have to install libpq-dev/postgresql-libs, which is the header files and static library for compiling C programs to link with the libpq library in order to communicate with a PostgreSQL database backend.
On Arch this will run:
$ sudo pacman -S postgresql-libs
On Debian and Ubuntu:
$ sudo apt-get install libpq-dev
On Mac OS X:
$ brew install postgresql
On Red Hat/CentOS/Fedora:
$ sudo yum install postgresql-devel
apt-get build-dep python-psycopg2
Just to sum up, I also faced exactly same problem. After reading a lot of stackoverflow posts and online blogs, the final solution which worked for me is this:
1) PostgreSQL(development or any stable version) should be installed before installing psycopg2.
2) The pg_config file (this file normally resides in the bin folder of the PostgreSQL installation folder) PATH had to be explicitly setup before installing psycopg2. In my case, the installation PATH for PostgreSQL is:
/opt/local/lib/postgresql91/
so in order to explicitly set the PATH of pg_config file, I entered following command in my terminal:
PATH=$PATH:/opt/local/lib/postgresql91/bin/
This command ensures that when you try to pip install psycopg2, it would find the PATH to pg_config automatically this time.
I have also posted a full error with trace and its solution on my blog which you may want to refer. Its for Mac OS X but the pg_config PATH problem is generic and applicable to Linux also.
You should add python requirements used in Postgres on Ubuntu. Run:
sudo apt-get install libpq-dev python-dev
sudo apt-get install libpq-dev works for me on Ubuntu 15.4
I had this issue because I didn't had a postgres install. If you have brew install run
brew install postgresql
This should fix the issue.
For those running OS X, this solution worked for me:
1) Install Postgres.app:
http://www.postgresql.org/download/macosx/
2) Then open the Terminal and run this command, replacing where it says {{version}} with the Postgres version number:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{{version}}/bin
e.g.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
On Linux Mint sudo apt-get install libpq-dev worked for me.
UPDATE /etc/yum.repos.d/CentOS-Base.repo, [base] and [updates] sections
ADD exclude=postgresql*
curl -O http://yum.postgresql.org/9.1/redhat/rhel-6-i386/pgdg-centos91-9.1-4.noarch.rpmr
rpm -ivh pgdg-centos91-9.1-4.noarch.rpm
yum install postgresql
yum install postgresql-devel
PATH=$PATH:/usr/pgsql-9.1/bin/
pip install psycopg2
Simply run the following:
sudo apt install libpq-dev
Fixed the issue for me
Try to add it to PATH:
PATH=$PATH:/usr/pgsql-9.1/bin/ ./pip install psycopg2
Ali's solution worked for me but I was having trouble finding the bin folder location. A quick way to find the path on Mac OS X is to open psql (there's a quick link in the top menu bar). This will open a separate terminal window and on the second line the path of your Postgres installation will appear like so:
My-MacBook-Pro:~ Me$ /Applications/Postgres93.app/Contents/MacOS/bin/psql ; exit;
Your pg_config file is in that bin folder. Therefore, before installing psycopg2 set the path of the pg_config file:
PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin/
or for newer version:
PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin
Then install psycopg2.
I'm going to leave this here for the next unfortunate soul who can't get around this problem despite all the provided solutions. Simply use sudo pip3 install psycopg2-binary
You need to upgrade your pip before installing psycopg2. Use this command
pip install --upgrade pip
On MacOS, the simplest solution will be to symlink the correct binary, that is under the Postgres package.
sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config
This is fairly harmless, and all the applications will be able to use it system wide, if required.
On Mac OS X and If you are using Postgres App (http://postgresapp.com/):
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
No need to specify version of Postgres in this command. It will be always pointed to latest.
and do
pip install psycopg2
P.S: If Changes doesn't reflect you may need to restart the Terminal/Command prompt
Source
Installing python-psycopg2 solved it for me on Arch Linux:
pacman -S python-psycopg2
On Windows,
You may want to install the Windows port of Psycopg, which is recommended in psycopg's documentation.
Just solved the problem in Cent OS 7 by:
export PATH=$PATH:/usr/pgsql-9.5/bin
make sure your PostgreSql version matches the right version above.
This was partly suggested before, adding it here for clarity.
From the documentation at https://www.psycopg.org/docs/install.html.
they suggest running: $ pip install psycopg2-binary
That solved the issue for me.
Here, for OS X completeness: if you install PostgreSQL from MacPorts, pg_config will be in /opt/local/lib/postgresql94/bin/pg_config.
When you installed MacPorts, it already added /opt/local/bin to your PATH.
So, this will fix the problem:
$ sudo ln -s /opt/local/lib/postgresql94/bin/pg_config /opt/local/bin/pg_config
Now pip install psycopg2 will be able to run pg_config without issues.
To those on macOS Catalina using the zsh shell who have also installed the postgres app:
Open your ~/.zshrc file, and add the following line:
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
Then close all your terminals, reopen them, and you'll have resolved your problem.
If you don't want to close your terminals, simply enter source ~/.zshrc in whatever terminal you'd like to keep working on.

Django unable to find MySQLdb python module

Installed Django from source (python setup.py install and such), installed MySQLdb from source (python setup.py build, python setup.py install). Using Python 2.4 which came installed on the OS (CentOS 5.5). Getting the following error message after launching the server:
Error loading MySQLdb module: No module named MySQLdb
The pythonpath the debug info provides includes
'/usr/lib/python2.4/site-packages'
and yet, if I ls that directory, I can plainly see
MySQL_python-1.2.3-py2.4-linux-i686.egg
Using the python interactive shell, I can type import MySQLdb and it produces no errors. This leads me to believe it's a Django pathing issue, but I haven't the slightest clue where to start looking as I'm new to both Django and python.
EDIT: And to be a bit more specific, everything is currently running as root. I haven't setup any users yet on the machine, so none exist other than root.
EDITx2: And to be even more specific, web server is Cherokee, and deploying using uWSGI. All installed from source.
Have you considered installing MySQLdb from python packages?
I would also recommend doing this with pip instead of easy_install.
First you can replace easy_install with pip:
easy_install pip
pip install pip --upgrade
And then install Django via PIP:
pip install MySQL-python
pip install Django
Typically easy_install is installed already (part of setuptools), while pip is much better. It offers uninstallation options too, and uses flat install directories instead of the EGG files magic. This might resolve some incompatibilities as well.
Did you try building the dependencies? This solved it for me on Ubuntu:
sudo apt-get build-dep python-mysqldb
pip install MySQLdb-python
What worked for me (Linux Mint):
sudo apt-get install libmysqlclient-dev (this was the key for me)
pip install mysql-python
pip install django
You can find out where Python is looking for it's libraries by invoking "python manage.py shell" from the directory base of your Django project. Then do:
import sys
import pprint
pprint.pprint(sys.path)
And you'll see where the python is pulling libraries from. Also try to do a "import mysql" to see if that's kicking out an error.
Finally, the pathing for the WSGI service is (likely) configured with the uWSGI setup in Cherokee - sorry, I don't know the details of that critter to make suggestions on how to determine where/how it's loading the library path.
I was having this same problem, but it was only an issue inside a virtualenv.
What I did to finally fix it was
workon [project_name]
pip uninstall django
pip install mysql-python
pip install django
So making sure you install mysql-python before django seems to work.
This is on a Ubuntu system and using virtualenv.
Try this if you are using
linux:- sudo apt-get install python-mysqldb
windows:- pip install python-mysqldb or
easy_install python-mysqldb
Hope this should work
This did the trick for me:
sudo apt-get install python-dev
pip install mysql-python
Solved my problem :)
in my case, Python was able to access mySQL, but Django (1.6) gave the error: "Error loading MySQLdb module: No module named MySQLdb"
I am on a Macbook OSX (Maverick), by the way.
When I tried to run
pip install mysql-python
I got an error during compilation : "clang: error: unknown argument: ‘-mno-fused-madd’ [-Wunused-command-line-argument-hard-error-in-future]."
The problem, it turns out, is an updated behavior of cc compiler with the new Xcode 5.1. When there is a parameter it doesn't recognize, considers it as a fatal error and quits. The solution to override this behavior can be found here:
http://bruteforce.gr/bypassing-clang-error-unknown-argument.html
This issue was the result of an incomplete/incorrect installation of the MySQL for Python adapter.
Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config.
Discussed in greater detail in this article:
http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

Categories