How to install psycopg2 for django on mac - python

I have postgresql installed (with postgresql app). When I try "pip install psycopg2", i get "unable to execute gcc-4.2: No such file or directory. How to fix?

Instead of
pip install psycopg2
try
pip install psycopg2-binary

You will need XCode to compile with gcc on macOS.
Install it with this command in the terminal:
xcode-select --install
Or you cold install it from the app store.

You should use virtualenv and Python Setup Tools/PIP to install all you packages for django.
This makes your job of package installs/upgrades very easy.
pip install virtualenv
This will install the virtualenv. Now create and activate a virtual environment using this virtualenv like this:
virtualenv <any_name_you_want_for_this_virtual_env>
e.g.
virtualenv api-venv
This will create a directory: api-venv in the current directory you are in.
Now activate this environment and then install all your packages using 'pip' command. This way, pip will install all packages inside this api-venv environment only.
source api-venv/bin/activate
Now the api-venv is activated. Let's install 'psycopg2'
pip install psycopg2
I would recommend to install django inside this venv so everything is in the venv.
pip install django
You can create as many such virtual environments as you like with different configuration.
Done!

You can try this way. First, you should install PostgreSQL through brew.
brew install postgresql
Then you should add pg_config to PATH. You can find pg_config with.
which pg_config
Finally, install psycopg2 with pip.
pip install psycopg2

Related

Python packages installing error in deep-learning environment

When I am installing the new package in my deep-learning environment it gives me this error:
Could not install packages due to an EnvironmentError: [WinError 32]
The process cannot access the file because it is being used by another
process: Consider using the --user option or check the permissions.
Please help to resolve this
You are trying to install the package to a system folder which you don't have permissions to write to.
You have three options(use only one of them):
1-setup a virtual env to install the package (recommended):
python3 -m venv env
source ./env/bin/activate
then:
Install your package using python -m pip install <name_package>
2-Install the package to the user folder:
Install your package using:
` python -m pip install --user <name_package>
3-use sudo to install to the system folder (not recommended):
`sudo python -m pip install <name_package>
terminal one as administrator if using Windows and use ubuntu before command use sudo.

Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

I am trying to install numpy on macOS X but after executing the command pip install numpy I am getting the error:
Environment error :[error 13]: permission denied : 'usr/local/bin/f2py
How do I fix it?
This worked for me.
pip3 install --user package-name # for Python3
pip install --user package-name # for Python2
The --user flag tells Python to install in the user home directory. By default it will go to system locations. credit
pip install --user package-name
Seems to work, but the package is install the the path of user. such as :
"c:\users\***\appdata\local\temp\pip-req-tracker-_akmzo\42a6c7d627641b148564ff35597ec30fd5543aa1cf6e41118b98d7a3"
I want to install the package in python folder such c:\Python27. I install the module into the expected folder by:
pip install package-name --no-cache-dir
I am also a Windows user. And I have installed Python 3.7 and when I try to install any package it throws the same error that you are getting.
Try this out. This worked for me.
python -m pip install numpy
And whenever you install new package just write
python -m pip install <package_name>
Hope this is helpful.
I too had to face the same problem. This worked for me. Right click and run as admin than run usual command to install. But first run update command to update the pip
python -m pip install --upgrade pip
Well, in my case the problem had a different cause, the Windows path Length Check this.
I was installing a library on a virtualenv which made the path get longer. As the library was installed, it created some files under site-packages. This made the path exceed Windows limit throwing this error.
Hope it helps someone =)
As a windows user, run an Admin powershell and launch :
python -m pip install --upgrade pip
I just ran the command with sudo:
sudo pip install numpy
Bear in mind that you will be asked for the user's password.
This was tested on macOS High Sierra (10.13)
I had the same problem for different package. I was installing pyinstaller in conda on Mac Mojave. I did
conda create --name ai37 python=3.7
conda activate ai37
I got the mentioned error when I tried to install pyinstaller using
pip install pyinstaller
I was able to install the pyinstaller with the following command
conda install -c conda-forge pyinstaller
If you are already using an virtual env on ubuntu and if you get this error,then navigate to your virtual env folder.If you see a lock symbol like:
Then you don't have the appropriate permission as a user to pip install inside this.Hence change the permission of the venv folder using this command:
sudo chown -R $USER path/to/venv
Now switch to the venv and install your package.
On Windows this has worked for me. From the command line, specify the path to the exe for Python:
& "C:/Program Files (x86)/Python37-32/python.exe" -m pip install --upgrade pip --user
It is always preferred to use a virtual environment ,Create your virtual environment using :
python -m venv <name_of_virtualenv>
go to your environment directory and activate your environment using below command on windows:
env_name\Scripts\activate.bat
then simply use
pip install package_name

How to install Django Rest Framework using Anaconda?

Main OS I am using are Ubuntu and Windows.
I couldn't find the package in conda.I am using python 3.5.
DRF is now on conda-forge. You can install it by using:
conda config --add channels conda-forge
conda install djangorestframework
More information about conda-forge:
https://conda-forge.github.io/
I have installed it on win7 and ubuntu 16.10.
To install this package with conda run:
conda install -c anaconda django=1.10.5
activate myenv # windows cmd
source activate myenv # linux, MacOS
# Anaconda prompt
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
# verify
conda list # Now you should see it on your list
django takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. To install this package with conda run https://anaconda.org/anaconda/django :
conda install -c anaconda django
I just installed django 1.11.3 on Python 3.6.2 Anaconda, Windows 10
This answer was posted inside the question:
The easiest is to go in to conda environment and use pip inside to install Django by doing:
> activate $your_conda_env
> pip install djangorestframework
just typing
pip install django
pip install djangoframework
or use a virtual environment to install those packages for that, first you need to install a virtual environment
pip install virtualenv
then create a new directory and make new virtual environment eg:
mkdir django
cd django
virtualenv django_project1
source django_project1/bin/activate
pip install django
pip install djangoframework
this will install django framework inside the virtual environment
will install django framework in your computer. And also update your pip packages using
pip update $PKG_NAME
eg:
pip update python

How to install psycopg2 with pg_config error?

I've tried to install psycopg2 (PostgreSQL Database adapater) from this site, but when I try to install after I cd into the package and write
python setup.py install
I get the following error:
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'.
I've also tried 'sudo pip install psycopg2' and I got the same message.
After reading through the docs, it asks to look at the setup.cfg file (which is below):
[build_ext]
define=
# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information
# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=
# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1
# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=
# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0
# Statically link against the postgresql client library.
#static_libpq=1
# Add here eventual extra libraries required to link the module.
#libraries=
However, I'm not sure if I'm suppose to edit this file, since the documentation states the following:
then take a look at the setup.cfg file.
Some of the options available in setup.cfg are also available as command line arguments of the build_ext sub-command. For instance you can specify an alternate pg_config version using:
$ python setup.py build_ext --pg-config /path/to/pg_config build
Use python setup.py build_ext --help to get a list of the options supported.
I've gotten the list of options supported but I'm not sure where to go from there
Debian/Ubuntu
Python 2
sudo apt install libpq-dev python-dev
Python 3
sudo apt install libpq-dev python3-dev
Additional
If none of the above solve your issue, try
sudo apt install build-essential
or
sudo apt install postgresql-server-dev-all
With pip
Install the psycopg2-binary PyPI package instead, it has Python wheels for Linux and Mac OS.
pip install psycopg2-binary
I was getting this issue because I hadn't yet installed PostgreSQL on my machine.
For Mac
brew install postgresql
For Mac without brew
python3 -m pip install psycopg2-binary
If you need to install without compiling:
pip install psycopg2-binary
https://www.psycopg.org/docs/install.html#binary-install-from-pypi
Note: The psycopg2-binary package is meant for beginners to start
playing with Python and PostgreSQL without the need to meet the build
requirements. If you are the maintainer of a publish package depending
on psycopg2 you shouldn’t use ‘psycopg2-binary’ as a module
dependency. For production use you are advised to use the source distribution.
If you are on Ubuntu or any other debian-based distro, try
sudo apt-get install python3-psycopg2
Otherwise, you need to find and install the Postgresql client packages for your distribution. psycopg2 installation from source
On Macbook with M1 i had to install postgresql
brew install postgresql
(If you don't have brew: https://brew.sh)
then run the install again
python3 -m pip install psycopg2-binary
Upgrading pip worked for me: pip install --upgrade pip
For OSX with Macports, you can install sudo port install py38-psycopg2 for Python 3.8. You can search for your version of Python with port search psycopg2. At the time of writing, the versions range from 2.7 to 3.9 all up to date with version 2.8.6 of psycopg2.
Note: This probably will not help in a venv.
Edit: So to find your pg_config for a venv, run the command port contents postgresql13 | grep pg_config where postgresql13 is the version of the package of postgresql installed from the above port command. You are looking for a path such as /opt/local/lib/postgresql13/bin/pg_config. Export that to your PATH variable with a command such as export PATH=/opt/local/lib/postgresql13/bin/:$PATH.
Refer to this answer for Homebrew.
On Fedora (tested and built from source on Fedora 35)
pg_config is present in libpq5-devel.
Now try these steps:
sudo dnf install libpq5-devel
python setup.py build
sudo python setup.py install
If you installed Postgres.app on macOS, add its bin directory to the PATH environment variable:
export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
For mac os
install postgresql with : brew install postgresql
then
install psycopg2 with : pip install psycopg2
worked for me
Incase you are using PyCharm/Visual Code Studio terminal, don't do it. Use the default system terminal, as it ensures correct format of brew install is done.
On M1 pro with Intel chip:
brew install postgresql
pip install psycopg2
For me just this worked on Mac:
pip install --upgrade pip
pip install psycopg2-binary
For Mac
I had this issue too. I had followed a guide that had me run
brew install postgresql#15
This worked, but I kept getting the error
Error: pg_config executable not found.
So I then tried to brew install postgres without the #15 after it, and it worked! So, if you have installed postgresql#15 or #any-number, try to brew install without a number.
brew install postgresql
Your error is becouse didn't install the prerequisites on your machine or environment.
You can install prerequisites from here.
In this case probably your missing prerequisite is postgresql you can try below instructors.
For Mac
brew install postgresql
For Linux
sudo apt-get install postgresql
For people building postgres and psycopg2 from source like me, another solution is here:
sudo su
export PATH=/usr/local/pgsql/bin:$PATH #or path to your pg_config
Now setup.py from psycopg2 could find pg_config correctly.
python3 setup.py install
or if you just want to use pip3, pip3 install psycopg2 should work too.

How do I can install pip inside virtual environment

I have created one virtual-environment. Inside that I have to install few python packages. For that I need pip package inside virtual environment. How can I install pip inside virtual-environment?
According to the pip documentation, you can install pip in a virtual environment by typing the following command when your virtual environment is activated:
python -m ensurepip --upgrade
For your information, ensurepip is an in-built Python module that serves the purpose of installing pip in your Python environment.
you can also try with upgrading pip command even after no pip installed at you specified virtual location
python -m pip install --upgrade pip
This will give you an error as below but also install latest pip version on virtual location
Can't uninstall 'pip'. No files were found to uninstall.
Successfully installed pip-19.2.3
kindly try above option and let me in case any issue.
Usually, you install pip OUTSIDE of your virtual environment.
But after activating the virtualenv you just run "pip install" inside the environment.
Meaning, you install it outside the virtualenv.
You run "pip install" inside the virtualenv.
You can follow this useful guide:
http://docs.python-guide.org/en/latest/dev/virtualenvs/
When you create a virtual environment for a particular python revision installed in your computer, all the libraries currently installed in your python revision would be copied inside the virtual environment.
Pip is usually default available in your python revision directory.
If not, install it first in your original python install directory.
Then copy the pip.exe to the virtual environment's Scripts directory.
After that execute the below command from your terminal (this is for Windows):
\your_venv_directory_path\Scripts\pip.exe install --upgrade pip
Now you can just type
pip install --upgrade pip
and it should recognise the path to the pip.exe file inside your venv
when creating the virtual-environment, be sure to include pip in the command. e.g:
conda create -n my_env pip python=3.6.8
I would suggest deleting the venv and recreating it using the above command

Categories