How to install psycopg2 on windows 7 - postgresql

I have tried pip install psycopg2 and following errors i got:
Error:
(ScrapeProj) C:\Users\e2sn7cy\Documents\GitHub\scraper>pip install psycopg2==2.5.4
Collecting psycopg2==2.5.4
Using cached psycopg2-2.5.4.tar.gz
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 in c:\users\e2sn7cy\appdata\local\temp\pi
p-build-joij3x\psycopg2
Then I tried using easy_install by downloading the .exe file from Stickpole and I got following errors :
(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj\Scripts>easy_install psycopg2-2.6.1.win-amd64-py2.7-pg9
.4.4-release.exe
Processing psycopg2-2.6.1.win-amd64-py2.7-pg9.4.4-release.exe
psycopg2.tests.test_module: module references __file__
psycopg2.tests.test_types_basic: module references __file__
creating 'c:\users\e2sn7cy\appdata\local\temp\easy_install-sa7asq\psycopg2-2.6.1-py2.7-win32.egg' and
adding 'c:\users\e2sn7cy\appdata\local\temp\easy_install-sa7asq\psycopg2-2.6.1-py2.7-win32.egg.tmp'
to it
creating c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Extracting psycopg2-2.6.1-py2.7-win32.egg to c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages
Adding psycopg2 2.6.1 to easy-install.pth file
Installed c:\users\e2sn7cy\envs\scrapeproj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Processing dependencies for psycopg2==2.6.1
Finished processing dependencies for psycopg2==2.6.1
But when I again tried to install through pip I am getting a new message that its already there :
(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj>pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in c:\users\e2sn7cy\envs\scrapepro
j\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg
Then I thought ok I should try to import and then I got this error:
(ScrapeProj) C:\Users\e2sn7cy\Envs\ScrapeProj>python
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\e2sn7cy\Envs\ScrapeProj\lib\site-packages\psycopg2-2.6.1-py2.7-win32.egg\psycopg2\__
init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: DLL load failed: %1 is not a valid Win32 application.
Now I am not able to understand what to do. Thanks
Edit 1:
I have also added my PostgreSQL path to system path variables
path:C:\Program Files\PostgreSQL\9.4
path:C:\Program Files\PostgreSQL\9.4\bin

The correct answer was given by Craig Ringer, I am just adding this because I came to know that we should first check which version of 64 bit or 32bit Python installed on our system. If you have 32 bit version of Python and if you will try to install 64bit, then you will get the above error on importing.
For checking the version:
import platform
platform.architecture()
and you will get an output like this:
('32bit','WindowsPE')

Download the pre-packaged binary release of psycopg2 for Windows rather than compiling it yourself. It's a pain to compile on Windows. This is explained in the install documentation for psycopg2.
You might need to remove your half-installed attempts.

Choose the appropriate version from this page:
http://www.stickpeople.com/projects/python/win-psycopg/
Right click and select copy link address
Back at home, use easy_install <<Paste URL Here>>
Freeze your new requirements: pip freeze > requirements.txt

Now that psycopg is in PyPI (as of March 2017 it appears), just fire up a command prompt with administrator privileges and install like so:
python -m pip install -U pip
python -m pip install psycopg2
If you don't have pip installed, follow the instructions here to do that first:
https://stackoverflow.com/a/12476379/2540707

pip install psycopg2-binary
This worked for me

Step 1: Compilers Installation and configuration
https://wiki.python.org/moin/WindowsCompilers
Before do anything, install or upgrade the Setuptools Python package. It contain compatibility improvements and add automatic use of compilers:
pip install --upgrade setuptools
Step 2: Download and install PostgreSQL
Before you continue to install python packages inside you virtualenvs download postgres itself. It contains files that are needed when compiling the psycopg2 python package. Just use the PostgreSQL installer(version 10 for example).
Important: add the postgres C:\Program Files\PostgreSQL\10\bin folder to your path. It contains the .dll needed for psycopg2.
Step 3: Install psycopg2
pip install psycopg2
Tested on Windows 7 + python 3.7

The best/easiest way of installing packages (including this problematic psycopg2 ) for python on windows platform is to:
1) install anaconda and ...
2) use Anaconda Navigator GUI to install/update packages.
By this one avoids many inconsistencies and conflicts that occurs among different packages versions and installations.

Related

How can i use PIL in python?

I have successfully installed Pillow:
chris#MBPvonChristoph sources % python3 -m pip install --upgrade Pillow
Collecting Pillow
Using cached Pillow-9.0.1-1-cp310-cp310-macosx_11_0_arm64.whl (2.7 MB)
Installing collected packages: Pillow
Successfully installed Pillow-9.0.1
but when i try to use it in pycharm got:
Traceback (most recent call last):
File "/Users/chris/PycharmProjects/pythonProject2/main.py", line 1, in
from PIL import Image
ModuleNotFoundError: No module named 'PIL'
or using in Blender i got:
ModuleNotFoundError: No module named 'PIL'
I am not a python lib installing pro...so obviously i made something wrong. But how do i fix that?
Maybe i have to say i am working on a M1 Macbook
looks like you may need to repoint your pycharm to your installed python interpreter.
go to command line and find out python interpreter path. On windows you can where python in your command line an it will give you where your python and packages are installed.. You could also activate python directly in command line and find paths from there. For example, open command line then;
python
press enter = activates python
within then you can do:
import sys
for x in sys.path: x
In pycharm make sure you point to path discovered from step 1 and select that to be your python interpreter within pycharm --- check out examples here https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#add-existing-interpreter
Should work. Not sure about all the steps you took, but if you installed python with pycharm on top of your regular installation of python i would recommend :
finding all the paths from step 1
deleting python using system
checking if folders found from paths step still exist
if they do, delete those as well
start over just with one python installation
repoint to that in pycharm
first
pip uninstall PIL
after uninstall
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
or
brew install Pillow

Installing Cryptography on an Apple Silicon M1 Mac

Help! I'm trying to install cryptography on my m1. I know I can run terminal in rosetta mode, but I'm wondering if there is a way not to do that.
Output:
ERROR: Command errored out with exit status 1:
command: /opt/homebrew/opt/python#3.9/bin/python3.9 /opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/tmpl4sga84k
cwd: /private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-install-jko4b562/cryptography_7b1bbc9ece2f481a8e8e9ea03b1a0030
Complete output (55 lines):
=============================DEBUG ASSISTANCE=============================
If you are seeing a compilation error please try the following steps to
successfully install cryptography:
1) Upgrade to the latest pip and try again. This will fix errors for most
users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
2) Read https://cryptography.io/en/latest/installation.html for specific
instructions for your platform.
3) Check our frequently asked questions for more information:
https://cryptography.io/en/latest/faq.html
=============================DEBUG ASSISTANCE=============================
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module>
main()
File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 133, in prepare_metadata_for_build_wheel
return hook(metadata_directory, config_settings)
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 161, in prepare_metadata_for_build_wheel
self.run_setup()
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 145, in run_setup
exec(compile(code, __file__, 'exec'), locals())
File "setup.py", line 44, in <module>
setup(
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/opt/homebrew/Cellar/python#3.9/3.9.1_7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/core.py", line 108, in setup
_setup_distribution = dist = klass(attrs)
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 432, in __init__
_Distribution.__init__(self, {
File "/opt/homebrew/Cellar/python#3.9/3.9.1_7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 292, in __init__
self.finalize_options()
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 708, in finalize_options
ep(self)
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 715, in _finalize_setup_keywords
ep.load()(self, ep.name, value)
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/setuptools_ext.py", line 219, in cffi_modules
add_cffi_module(dist, cffi_module)
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/setuptools_ext.py", line 49, in add_cffi_module
execfile(build_file_name, mod_vars)
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/setuptools_ext.py", line 25, in execfile
exec(code, glob, glob)
File "src/_cffi_src/build_openssl.py", line 77, in <module>
ffi = build_ffi_for_binding(
File "src/_cffi_src/utils.py", line 54, in build_ffi_for_binding
ffi = build_ffi(
File "src/_cffi_src/utils.py", line 74, in build_ffi
ffi = FFI()
File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/api.py", line 48, in __init__
import _cffi_backend as backend
ImportError: dlopen(/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/_cffi_backend.cpython-39-darwin.so, 2): Symbol not found: _ffi_prep_closure
Referenced from: /private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/_cffi_backend.cpython-39-darwin.so
Expected in: flat namespace
in /private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/_cffi_backend.cpython-39-darwin.so
I've tried to build and run like their instructions say in that code block to the same error. I've looked around and nobody has seemingly found the fix yet, but those things are two months old usually. What am I missing?
This issue is due to a mismatch between the libffi header version and the version of libffi the dynamic linker finds. In general it appears users encountering this problem have homebrew libffi installed and have a Python built against that in some fashion.
When this happens cffi (a cryptography dependency) compiles, but fails at runtime raising this error. This should be fixable by passing the right path as a linker argument. To reinstall cffi you should pip uninstall cffi followed by
LDFLAGS=-L$(brew --prefix libffi)/lib CFLAGS=-I$(brew --prefix libffi)/include pip install cffi --no-binary :all:
This is an ugly workaround, but will get you past this hurdle for now.
Update: I've uploaded arm64 wheels for macOS so the below compilation is no longer required if your pip is up-to-date. However, if, for some reason you wish to compile it yourself:
LDFLAGS="-L$(brew --prefix openssl#1.1)/lib" CFLAGS="-I$(brew --prefix openssl#1.1)/include" pip install cryptography
I'm using Macbook Pro M1 2020 model and faced the same issue. The issue was only with my cffi and pip versions maybe. Because these 4 steps helped me -
Uninstalling old cffi pip uninstall cffi
Upgrading pip python -m pip install --upgrade pip
Reinstalling cffi pip install cffi
Intalling cryptography pip install cryptography
Probably, you'll have a problem with more packages and each has it's own solution for Apple Silicon, it's exhausting.
I came to final solution: using x86_x64 Homebrew which installs x86 packages, including Python. Thus, all your requirements are installing as on the x86_x64 macs and there are no more problems with the compilation errors and so on.
Instructions:
Run iTerm2 (or default Terminal app) under Rosetta 2 (right click on the app icon -> Get info -> Open using rosetta).
Install homebrew as usual /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" or you can get this link from https://brew.sh/ for security reasons (never copy curl commands from stackoverflow without double-checking).
Add an alias in your ~/.zshrc (if you're using ZSH) or ~/.bash_profile (if you're bash user): alias brew='arch -x86_64 /usr/local/bin/brew'.
Turn off Open using rosetta in iTerm2 Get info.
Now, every time you'll print brew in terminal apps you'll run x86_x64 Homebrew. And when you install any package from brew, it'll work under Rosetta 2 automatically.
A little late to the party, but the solutions above didn't work for me. Paul got me on the right track, but my problem was that pyenv used the mac libffi for its build and cffi used the homebrew version. I read this somewhere, can't claim this unique insight.
My solution was to ensure that my python (3.8.13) was built by pyenv using the homebrew libffi by ensuring correct headers libraries and package config:
export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix openssl#1.1)/lib -L$(brew --prefix libffi)/lib"
export CPPFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix openssl#1.1)/include -I$(brew --prefix libffi)/include"
export PKG_CONFIG_PATH="$(brew --prefix openssl#1.1)/lib/pkgconfig:$(brew --prefix libffi)/lib/pkgconfig"
rebuilding python...
pyenv uninstall 3.8.13
pyenv install 3.8.13
killing the pip cache
pip cache purge
and, finally, reinstalling my dependencies using pipenv
pipenv --rm
pipenv sync --dev
After these steps, I was free from the dreaded
ImportError: dlopen(/private/var/folders/k7/z3mq67_532bdr_rcm2grml240000gn/T/pip-build-env-apk5b25z/overlay/lib/python3.8/site-packages/_cffi_backend.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '_ffi_prep_closure'
This answer here worked like a charm! #paveldroo
As an extension to the answer above, I went ahead and saved the alias in step 3 as alias ibrew='arch -x86_64 /usr/local/bin/brew' at ~/.zshrc
This means when I install anything with brew command, it gets installed for M1 architecture, and when I install with ibrew command it gets installed for -x86_64 architecture.
As a consequence, I installed two instances of python3 at my system one at /opt/homebrew/bin/python3 using brew and the other at /usr/local/bin/python3 using ibrew
The two versions adds some flexibility on creating the project virtual environments as needed. For example you could create virtual environments using:
/usr/local/bin/python3 -m venv venv for -x86_64 architecture
/opt/homebrew/bin/python3 -m venv venv for M1 architecture
I wasn't able to previously install cffi, until I discovered an unrelated issue. I was at this for about two days, until I found this command:
python3 -m ensurepip --upgrade
Magically, everything started working for me. It came from an issue between Python and Pip coming from different sources.
Answer stolen from this question: using pip3: module "importlib._bootstrap" has no attribute "SourceFileLoader"
Edit: This may be a courtesy of the above poster, so could be unrelated. If so, thank you anonymous human!
I have uninstalled older version of cffi and cryptography,
pip uninstall cffi
pip uninstall cryptography
and updated the requirements.txt file from exact versions to updated versions
# requirements.txt
cffi>=1.15.1
cryptography>=38.0.1
(version number can be different).
This resolved my issue

python's readline module not available for windows?

Granted I've been off PYTHON for two + years. I'm trying to get back into the swing, and I remember having command and variable completion available on windows a few years back. We were stuck in 2.6 as we had dependencies.
Today, I'm trying to import readline. I get this message from pip.
C:\Users\Joe>pip3 install readline
Collecting readline
Using cached https://files.pythonhosted.org/packages/f4/01/2cf081af8d880b44939a5f1b446551a7f8d59eae414277fd0c303757ff1b/readline-6.2.4.1.tar.gz
Complete output from command python setup.py egg_info:error: this module is not meant to work on Windows
my version is:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import readline
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'readline'
I have recently faced the same issue on Windows 10. readline is not available for Windows, however there is pyreadline3 (a continuation of the abandoned pyreadline package) which is for windows and you may try it.
Try to install pyreadline3 and use it instead of readline:
pip: pip install pyreadline3 or python -m pip install pyreadline
mamba: mamba install -c conda-forge pyreadline3
conda: conda install -c conda-forge pyreadline3
Then, in your python file do this:
from pyreadline3 import Readline
readline = Readline()
That way, you can use readline in Windows like in linux systems.
Go to your CMD terminal and type
pip install pyreadline
with in few seconds it will be installed and you will be able to see message i.e. "Successfully installed pyreadline-"
then go back to your PYSPARK terminal and type again import readline
it will import now without any error
Thanks
I tried pyreadline after finding this question and had problems and eventually found that it is no longer supported.
What worked for me was installing pyreadline3 with:
pip install pyreadline3

Problems Installing virtualenvwrapper on mountain lion

I recently lost my HDD and am currently in the process of rebuilding my laptop (running Mountain Lion) dev system. This afternoon I went to install virtualenvwrapper and ran into some weirdness that prevents me from installing virtualenvwrapper.
I've done the following:
Installed setup tools.
Used easy_install-2.7 to install pip.
Used pip-2.7 to install virtual env.
When I go to install virtualenvwrapper using pip-2.7, I get the following (see below). The -2.5,-2.6,-2.7 suffixes seem to be a new thing for mountain lion? I've tried the same process without the suffixes, but that didn't work either.
I'm posting because I suspect something is majorly awry here, and would like to get to the bottom of it before it becomes a more insidious problem down the road.
Is it better for me to download and install a fresh version of 2.7, and set up my paths to use it?
Downloads sudo pip-2.7 install virtualenvwrapper
Requirement already satisfied (use --upgrade to upgrade): virtualenvwrapper in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): virtualenv-clone in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): stevedore in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Downloading/unpacking distribute (from stevedore->virtualenvwrapper)
Downloading distribute-0.6.28.tar.gz (627kB): 627kB downloaded
Running setup.py egg_info for package distribute
Installing collected packages: distribute
Running setup.py install for distribute
Before install bootstrap.
Scanning installed packages
Setuptools installation detected at /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg
Egg installation
Patching...
Renaming /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg into /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg.OLD.1346705360.93
Patched done.
Relaunching...
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'install' is not defined
Complete output from command /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c "import setuptools;__file__='/tmp/pip-build/distribute/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-RMDd1X-record/install-record.txt --single-version-externally-managed:
Before install bootstrap.
Scanning installed packages
Setuptools installation detected at /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg
Egg installation
Patching...
Renaming /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg into /Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg.OLD.1346705360.93
Patched done.
Relaunching...
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'install' is not defined
What p4tux said
virtualenv is now using distribute not setuptools
export VIRTUALENV_DISTRIBUTE=true
before you create the environment with
virtualenv ENV
or do
$ python virtualenv.py --distribute ENV
See virtualenv documentation
virtualenv is now using distribute not setuptools
export VIRTUALENV_DISTRIBUTE=true
This link:
https://gist.github.com/3179227
(using pythonbrew) seems to have sorted the issue out for me temporarily. I'd like to not have another component in the mix (pythonbrew), so hopefully someone else will weight in with how to get virtualenv to play nicely with ML.

Installing psycopg2 into virtualenv when PostgreSQL is not installed on development system

Is it possible to install psycopg2 into a virtualenv when PostgreSQL isn't installed on my development system—MacBook Pro with OS X 10.6?
When I run pip install psycopg2 from within my virtualenv, I received the error shown below.
I'm trying to connect to a legacy database on a server using Django, and I'd prefer not to install PostgreSQL on my development system if possible.
Why not install PostgreSQL?
I received an error when installing PostgreSQL using homebrew. I have Xcode4—and only Xcode4—installed on my MacBook Pro and am thinking it's related to missing gcc 4.0. However, this is a problem for another StackOverflow question.
Update 8:37 AM on April 12, 2011: I'd still like to know if this is possible without installing PostgreSQL on my MacBook Pro. However, I ran brew update and forced a reinstallation of ossp-uuid with brew install --force ossp-uuid and now brew install postgresql works. With PostgreSQL successfully installed, I was able to pip install psycopg2 from within my virtualenv.
Error from pip install psycopg2
$ pip install psycopg2
Downloading/unpacking psycopg2
Running setup.py egg_info for package 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'.
Complete output from command python setup.py egg_info:
running 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
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 /Users/matthew/.pip/pip.log
Preliminary Research
Below are the articles I read as preliminary research:
Installing psycopg2 to use Django with PostgreSQL on OS X
Installing psycopg2 on OS X
Using psycopg2 with virtualenv on Ubuntu JauntyLucid
Postgres, psycopg2, virtualenv install hints
apt-get install libpq-dev
helped me on debian squeeze
From comments:
apt-get install python-dev-is-python3
psycopg depends on pg_config command, and if you don't have it, you can't install psycopg.
If system installation is a problem to you, why don't you try compiling PostgreSQL and including generated bin files in your $PATH? Like:
export PATH=/path/to/compiled/postgresql/bin:"$PATH"
pip install psycopg2
brew install postgresql
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
pip3 install psycopg2
apt-get install libpq-dev
helped me in debian squeeze too . After that do pip install psycopg2. I faced problem of pg_config not found problem when i was setting up my environment on heroku , now its working fine .
You need to install the python-dev package in order to make use of python extensions such as psycopg2. I don't know how to install packages in mac but I run the following commands to install a python package on my Ubuntu machine.
sudo apt-get install python-dev
Or
sudo apt-get install python3-dev
if you are using Python3.x.
Once the installation is finished run the following command within your virtual environment.
pip install psycopg2
You don't need the complete PostgreSQL installed: you only need the client side libraries.
I solved it in MAC OSX using :
$ wget https://ftp.postgresql.org/pub/source/v9.5.3/postgresql-9.5.3.tar.bz2
$ tar xfv postgresql-9.5.3.tar.bz2
$ cd postgresql-9.5.3
$ ./configure
$ make
$ cd src/bin/pg_config
$ export PATH=`pwd`:"$PATH"
$ pip install psycopg2
Use pip install psycopg2-binary, it worked for me when pip install psycopg2 wasn't working.
Using the method you described in your April 12th update, I was able to install PostgreSQL(+1). Note that I originally was running Python 2.7.1 (32bit) and homebrew threw several errors and warnings regarding using a 32bit version of Python. I have since switched the 64/32 bit version of 2.7.1 and it works like a champ.
Regarding the pyscopg2, I was able to install it into my virtual environment from source by editing setup.cfg. I set pg_config to correct path inside homebrew's Cellar (pg_config=/usr/local/Cellar/postgresql/9.0.4/bin/pg_config). After saving the changes, I ran python setup.py install with zero issues. It's worth noting that I did not set the Mac's default python setting to 32 bit. I used the new 64 bit from start to finish.
After looking over some of the documentation, I think if I added homebrew's postgresql path to the system path I could have used pip to install it.
Reference:
http://favosdream.blogspot.com/2009/09/make-psycopg2-and-readline-work-in-snow.html
Update 6-8-2011:
While porting a project written on OS X to Windows 7, I found out that I had to install PostgreSQL on Windows as well. This ended up creating another user on my start up screen and other things that I just didn't like. While doing some digging I found Windows drivers for PostgreSQL here. I have since uninstalled the full PostgreSQL and installed the ODBC drivers which, thus far, work great.
To address the original question, after doing a bit more digging I think I found the equivalent ODBC for OS X here. I have not had a chance to try them out, but the concept works very well on Windows 7. I will update this when I get a chance to try them out. Until then, I hope this helps.
All from the above doesn't work for me (OS Catalina 10.15.1)
There was a conflict with classical postgres and homebrew version.
Please delete homebrew version by command
$ brew uninstall postgresql
and then install it from the official website:
$ sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
It is actual for a simple psycopg2 install or django-heroku package.
if you have recently updated python or changed default python (let's say from 3.6 to 3.8). The following code
sudo apt-get install python-dev OR sudo apt-get install python3-dev
will be installing/working for the previous python version.
so if you want this command to work for the recently updated/changed python version try mentioning that specific version like python3.8 in command like
sudo apt-get install python3.8-dev
try above with following
pip install wheel
export PATH=/path/to/compiled/postgresql/bin:"$PATH"
sudo apt-get install libpq-dev
sudo apt-get install python3.x-dev **Change x with your version, eg python3.8**
pip install psycopg2-binary
pip install psycopg2
As I never needed to install postgresql database on this server I installed the following libraries on Ubuntu 14_04 version before running pip install psycopg2 on the same server
apt-get install libpq-dev python-dev
and then executed pip install psycopg2 within virtual env.
Output
Collecting psycopg2
Using cached psycopg2-2.6.1.tar.gz
Building wheels for collected packages: psycopg2
Running setup.py bdist_wheel for psycopg2
Stored in directory: /root/.cache/pip/wheels/e2/9a/5e/7b620848bbc7cfb9084aafea077be11618c2b5067bd532f329
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.6.1
I know you are asking for development environment but if you are deploying on server say, Heroku.
Just add below line in the requirements.txt of your project.
django-heroku==0.3.1
As this package itself will install the required packages like psycopg2 on server deployment.
Try to specify the version and it'll works, do it inside the venv
pip install psycopg2-binary==2.8.6
based on my experience, apt install libpq-dev then try to install psycopg2 inside venv will fix many issues, however for more information you can refer to this link:
https://www.psycopg.org/docs/install.html