ImportError on psycopg2 when upgrading to v2.3.0 #1578

Closed
opened 2025-12-29 16:33:08 +01:00 by adam · 9 comments
Owner

Originally created by @anhnvme on GitHub (Feb 27, 2018).

Issue type

[ ] Feature request
[x] Bug report
[ ] Documentation

Environment

  • Python version: 3.5.5
  • NetBox version: 2.3.0

Description

After pull new netbox source version 2.3.0. And running upgrade.sh i meet this error

`Applying database migrations (python3 netbox/manage.py migrate)...
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 21, in
import psycopg2 as Database
ImportError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "netbox/manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/usr/lib/python3.4/site-packages/django/core/management/init.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/lib/python3.4/site-packages/django/core/management/init.py", line 338, in execute
django.setup()
File "/usr/lib/python3.4/site-packages/django/init.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.4/importlib/init.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 2254, in _gcd_import
File "", line 2237, in _find_and_load
File "", line 2226, in _find_and_load_unlocked
File "", line 1200, in _load_unlocked
File "", line 1129, in _exec
File "", line 1471, in exec_module
File "", line 321, in _call_with_frames_removed
File "/usr/lib/python3.4/site-packages/django/contrib/auth/models.py", line 4, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/usr/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 52, in
class AbstractBaseUser(models.Model):
File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 124, in new
new_class.add_to_class('_meta', Options(meta, app_label))
File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/usr/lib/python3.4/site-packages/django/db/models/options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/usr/lib/python3.4/site-packages/django/db/init.py", line 33, in getattr
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 211, in getitem
backend = load_backend(db['ENGINE'])
File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib64/python3.4/importlib/init.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 25, in
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
Collecting static files (python3 netbox/manage.py collectstatic --no-input)...
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 21, in
import psycopg2 as Database
ImportError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "netbox/manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/usr/lib/python3.4/site-packages/django/core/management/init.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/lib/python3.4/site-packages/django/core/management/init.py", line 338, in execute
django.setup()
File "/usr/lib/python3.4/site-packages/django/init.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.4/importlib/init.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 2254, in _gcd_import
File "", line 2237, in _find_and_load
File "", line 2226, in _find_and_load_unlocked
File "", line 1200, in _load_unlocked
File "", line 1129, in _exec
File "", line 1471, in exec_module
File "", line 321, in _call_with_frames_removed
File "/usr/lib/python3.4/site-packages/django/contrib/auth/models.py", line 4, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/usr/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 52, in
class AbstractBaseUser(models.Model):
File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 124, in new
new_class.add_to_class('_meta', Options(meta, app_label))
File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/usr/lib/python3.4/site-packages/django/db/models/options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/usr/lib/python3.4/site-packages/django/db/init.py", line 33, in getattr
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 211, in getitem
backend = load_backend(db['ENGINE'])
File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib64/python3.4/importlib/init.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 25, in
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'`

psycopg2 already installed
Requirement already up-to-date: psycopg2-binary>=2.7.4 in /usr/lib64/python3.4/site-packages (from -r requirements.txt (line 17))

Originally created by @anhnvme on GitHub (Feb 27, 2018). ### Issue type [ ] Feature request <!-- An enhancement of existing functionality --> [x] Bug report <!-- Unexpected or erroneous behavior --> [ ] Documentation <!-- A modification to the documentation --> ### Environment * Python version: 3.5.5 * NetBox version: 2.3.0 ### Description After pull new netbox source version 2.3.0. And running upgrade.sh i meet this error `Applying database migrations (python3 netbox/manage.py migrate)... Traceback (most recent call last): File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 21, in <module> import psycopg2 as Database ImportError: No module named 'psycopg2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/lib/python3.4/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line utility.execute() File "/usr/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute django.setup() File "/usr/lib/python3.4/site-packages/django/__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate app_config.import_models() File "/usr/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "/usr/lib64/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 2254, in _gcd_import File "<frozen importlib._bootstrap>", line 2237, in _find_and_load File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked File "<frozen importlib._bootstrap>", line 1129, in _exec File "<frozen importlib._bootstrap>", line 1471, in exec_module File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed File "/usr/lib/python3.4/site-packages/django/contrib/auth/models.py", line 4, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/usr/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 52, in <module> class AbstractBaseUser(models.Model): File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 124, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 325, in add_to_class value.contribute_to_class(cls, name) File "/usr/lib/python3.4/site-packages/django/db/models/options.py", line 214, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/usr/lib/python3.4/site-packages/django/db/__init__.py", line 33, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 211, in __getitem__ backend = load_backend(db['ENGINE']) File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 115, in load_backend return import_module('%s.base' % backend_name) File "/usr/lib64/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 25, in <module> raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2' Collecting static files (python3 netbox/manage.py collectstatic --no-input)... Traceback (most recent call last): File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 21, in <module> import psycopg2 as Database ImportError: No module named 'psycopg2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/lib/python3.4/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line utility.execute() File "/usr/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute django.setup() File "/usr/lib/python3.4/site-packages/django/__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate app_config.import_models() File "/usr/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "/usr/lib64/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 2254, in _gcd_import File "<frozen importlib._bootstrap>", line 2237, in _find_and_load File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked File "<frozen importlib._bootstrap>", line 1129, in _exec File "<frozen importlib._bootstrap>", line 1471, in exec_module File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed File "/usr/lib/python3.4/site-packages/django/contrib/auth/models.py", line 4, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/usr/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 52, in <module> class AbstractBaseUser(models.Model): File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 124, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "/usr/lib/python3.4/site-packages/django/db/models/base.py", line 325, in add_to_class value.contribute_to_class(cls, name) File "/usr/lib/python3.4/site-packages/django/db/models/options.py", line 214, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/usr/lib/python3.4/site-packages/django/db/__init__.py", line 33, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 211, in __getitem__ backend = load_backend(db['ENGINE']) File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 115, in load_backend return import_module('%s.base' % backend_name) File "/usr/lib64/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 25, in <module> raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'` psycopg2 already installed `Requirement already up-to-date: psycopg2-binary>=2.7.4 in /usr/lib64/python3.4/site-packages (from -r requirements.txt (line 17))`
adam closed this issue 2025-12-29 16:33:08 +01:00
Author
Owner

@jeremystretch commented on GitHub (Feb 27, 2018):

It looks like you might have a conflict between the old psycopg2 package and the new psycopg2-binary package. Try running this:

pip3 uninstall psycopg2
pip3 uninstall psycopg2-binary
pip3 install psycopg2-binary
@jeremystretch commented on GitHub (Feb 27, 2018): It looks like you might have a conflict between the old `psycopg2` package and the new `psycopg2-binary` package. Try running this: ``` pip3 uninstall psycopg2 pip3 uninstall psycopg2-binary pip3 install psycopg2-binary ```
Author
Owner

@anhnvme commented on GitHub (Feb 27, 2018):

Hello,

I Just remove psycopg2 and
psycopg2-binary already installed, but while upgrade still meet that error

@anhnvme commented on GitHub (Feb 27, 2018): Hello, I Just remove psycopg2 and psycopg2-binary already installed, but while upgrade still meet that error
Author
Owner

@Armadill0 commented on GitHub (Feb 27, 2018):

@jeremystretch That worked for me, thanks.

@Armadill0 commented on GitHub (Feb 27, 2018): @jeremystretch That worked for me, thanks.
Author
Owner

@GitRost commented on GitHub (Feb 27, 2018):

Yes, uninstall and pip install psycopg2-binary worked.

@GitRost commented on GitHub (Feb 27, 2018): Yes, uninstall and pip install psycopg2-binary worked.
Author
Owner

@jeremystretch commented on GitHub (Feb 27, 2018):

@anhnvme You need to run all three commands.

@jeremystretch commented on GitHub (Feb 27, 2018): @anhnvme You need to run all three commands.
Author
Owner

@jeremystretch commented on GitHub (Feb 27, 2018):

Seems like we have a solution for this, but please comment here if you think this needs to be re-opened.

@jeremystretch commented on GitHub (Feb 27, 2018): Seems like we have a solution for this, but please comment here if you think this needs to be re-opened.
Author
Owner

@mabazyar commented on GitHub (Mar 7, 2018):

Environment

OS: CentOS Linux release 7.3.1611 (Core)
Python version: Python 3.4.5

Goal: Upgrading Netbox

Netbox [former] : 2.2.10
Netbox [current]: 2.3.1

Description

I got it worked by performing the following in order:

pip3 uninstall psycopg2-binary
pip3 uninstall psycopg2
pip3 install psycopg2-binary
@mabazyar commented on GitHub (Mar 7, 2018): ### Environment OS: CentOS Linux release 7.3.1611 (Core) Python version: Python 3.4.5 ### Goal: Upgrading Netbox Netbox [former] : 2.2.10 Netbox [current]: 2.3.1 ### Description I got it worked by performing the following in order: ``` pip3 uninstall psycopg2-binary pip3 uninstall psycopg2 pip3 install psycopg2-binary ```
Author
Owner

@anhnvme commented on GitHub (Mar 23, 2018):

@mabazyar it's worked, thanks

@anhnvme commented on GitHub (Mar 23, 2018): @mabazyar it's worked, thanks
Author
Owner

@ammar1y commented on GitHub (Apr 13, 2019):

@jeremystretch Thank you for the solution. The problem drove me crazy. Can you please explain the cause of the problem and how psycopg2-binary differs from psycopg2?

@ammar1y commented on GitHub (Apr 13, 2019): @jeremystretch Thank you for the solution. The problem drove me crazy. Can you please explain the cause of the problem and how `psycopg2-binary` differs from `psycopg2`?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1578