3

I'm a newbie in Django/Python. I just setup Django successfully with vagrant on my computer. But I want to change the default database from sqlite to postgresql.

To do so, I just changed the database section on the setting.py file to :

# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.postgresql',
      'NAME': os.path.join(BASE_DIR, 'db.postgresql'),
   }
}

Now whenever I run the command python manage.py runserver 0.0.0.0:8080 (which used to work well), I get the following error :

 Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fb91ed95ea0>
Traceback (most recent call last):
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 327, in execute
    autoreload.check_errors(django.setup)()
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/ubuntu/.virtualenvs/meshine_api/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/models/base.py", line 114, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/models/base.py", line 315, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/models/options.py", line 205, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/home/ubuntu/.virtualenvs/meshine_api/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 32, in <module>
    PSYCOPG2_VERSION = psycopg2_version()
  File "/home/ubuntu/.virtualenvs/meshine_api/local/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 28, in psycopg2_version
    version = psycopg2.__version__.split(' ', 1)[0]
AttributeError: module 'psycopg2' has no attribute '__version__'

I don't know why!

Here is my vagrant file :

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/xenial64"

  config.vm.network "forwarded_port", host_ip: "127.0.0.1", guest: 8080, host: 8080

  config.vm.provision "shell", inline: <<-SHELL
    # Update and upgrade the server packages.
    sudo apt-get update
    sudo apt-get -y upgrade
    # Set Ubuntu Language
    sudo locale-gen en_GB.UTF-8
    # Install Python, SQLite and pip
    sudo apt-get install -y python3-dev sqlite python-pip postgresql postgresql-contrib
    # Upgrade pip to the latest version.
    sudo pip install --upgrade pip
    # Install and configure python virtualenvwrapper.
    sudo pip install virtualenvwrapper
    if ! grep -q VIRTUALENV_ALREADY_ADDED /home/ubuntu/.bashrc; then
        echo "# VIRTUALENV_ALREADY_ADDED" >> /home/ubuntu/.bashrc
        echo "WORKON_HOME=~/.virtualenvs" >> /home/ubuntu/.bashrc
        echo "PROJECT_HOME=/vagrant" >> /home/ubuntu/.bashrc
        echo "source /usr/local/bin/virtualenvwrapper.sh" >> /home/ubuntu/.bashrc
    fi
  SHELL

end

Please where I'm wrong?

kabrice
  • 1,475
  • 6
  • 27
  • 51
  • How did you install psycopg2 and what version number? – Jason Jan 06 '18 at 03:21
  • Simply with : `pip install psycopg2` and I think the version is `2.7.3.2` – kabrice Jan 06 '18 at 03:42
  • are you able to import it in the shell? eg `import psycopg2` and `print(psycopg2.__version__` – Jason Jan 06 '18 at 13:50
  • No when typing : import `psycopg2` and `print(psycopg2.__version__)` in the python shell, I got the error : `AttributeError: module 'psycopg2' has no attribute '__version__'`. Why ? – kabrice Jan 06 '18 at 14:07
  • Sounds like a bad install. I just installed it in a new virtualenv and it worked fine. I can only suggest uninstalling and reinstalling – Jason Jan 06 '18 at 14:20
  • 3
    Wow thanks! Just uninstalled and reinstalled it. And it works great. Thx again – kabrice Jan 06 '18 at 14:43

0 Answers0