Yes, found a solution,
Copypython -m pip install psycopg2-binary
does the trick!
Answer from arilwan on Stack OverflowHi, I'm currently programming a school project in python and need to connect my program to a database. I'm using postgresql and have tried downloading psycopg2 using every possible pip command the internet had to offer but I keep getting a module not found error when I use 'import psycopg2'. Does anyone have any suggestions? I'm pretty new to this level of coding and wayyyyyy out of my depth. (it's running on windows if that helps)
python - ModuleNotFoundError: No module named 'psycopg2' macOS - Stack Overflow
python 3.x - ModuleNotFoundError: No module named psycopg2 after installing package on Mac - Stack Overflow
Error loading psycopg2 module: No module named 'psycopg2'
python - ImportError: No module named psycopg2 - Stack Overflow
Videos
Step 1: Install the dependencies
sudo apt-get install build-dep python-psycopg2
Step 2: Run this command in your virtualenv
pip install psycopg2-binary
Ref: Fernando Munoz
Use psycopg2-binary instead of psycopg2.
pip install psycopg2-binary
Or you will get the warning below:
UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: http://initd.org/psycopg/docs/install.html#binary-install-from-pypi.
Reference: Psycopg 2.7.4 released | Psycopg
MacOs M1, Python3 and postgresSQL.
I did
pip3 install psycopg2-binary
then
import psycopg2
but I get this error
File "/Users/lce21/Documents/GitHub/hazen-web-app/hazen-web-app/lib/python3.8/site- packages/psycopg2/__init__.py", line 51, in <module> from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/lce21/Documents/GitHub/hazen-web-app/hazen-web- app/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so, 0x0002): Library not loaded: /usr/local/opt/postgresql/lib/libpq.5.dylib Referenced from: /Users/lce21/Documents/GitHub/hazen-web-app/hazen-web- app/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so Reason: tried: '/usr/local/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such. file), '/usr/local/Cellar/postgresql@14/14.6_1/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file)
I read I need to change file locations but I am a real newbie and I don't understand how to do that
The first thing to do is to install the dependencies.
sudo apt-get build-dep python-psycopg2 sudo apt install python3-psycopg2 # Python 3
After that go inside your virtualenv and use:
pip install psycopg2-binary
These two commands should solve the problem.
pip install psycopg2-binary
The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: http://initd.org/psycopg/docs/install.html#binary-install-from-pypi.
Here I am trying to connect my PostgreSQL db. I can see them install whenever I run pip freeze. Despite trying many fixes I get the following error when I type in py manage.py makemigrations. I have attempted many solutions online but none seem to work. Please help me out geeks stuck here for 2 days!!
ERROR :
Traceback (most recent call last):
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 25, in <module>
import psycopg as Database
ModuleNotFoundError: No module named 'psycopg'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 27, in <module>
import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\GymManagement\gymManageSys\manage.py", line 22, in <module>
main()
File "F:\GymManagement\gymManageSys\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
utility.execute()
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\management\__init__.py", line 416, in execute
django.setup()
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\apps\registry.py", line 116, in populate
app_config.import_models()
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\apps\config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\contrib\auth\base_user.py", line 59, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\models\base.py", line 143, in __new__
new_class.add_to_class("_meta", Options(meta, app_label))
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\models\base.py", line 371, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\models\options.py", line 243, in contribute_to_class
self.db_table, connection.ops.max_name_length()
^^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\utils\connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\utils.py", line 193, in create_connection
backend = load_backend(db["ENGINE"])
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\utils.py", line 113, in load_backend
return import_module("%s.base" % backend_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Neel\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\postgresql\base.py", line 29, in <module>
raise ImproperlyConfigured("Error loading psycopg2 or psycopg module")
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module