Django SQL server connection not working in Pycharm

I'm using v2021.1.2 Pro edition on Ubuntu 20.4.2 running Python 3.8 and Django 2.3.2. 

I've create a Django project that has nothing in it but a revised DATABASES setting to connect to a SQL server instance and a very simple one table model:

DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'MyDB',
'HOST': 'TheServerName',

and

from django.db import models

# Create your models here.

class HPOTerm(models.model):
TermCode = models.CharField(50)
Term = models.CharField(500)

When I run the project in Pycharm I get an error at line 122 in Utils.py:

if backend_name not in ['django.db.backends.%s' % b for b in builtin_backends]:
backend_reprs = map(repr, sorted(builtin_backends))
raise ImproperlyConfigured(
"%r isn't an available database backend or couldn't be "
"imported. Check the above exception. To use one of the "
"built-in backends, use 'django.db.backends.XXX', where XXX "
"is one of:\n"
" %s" % (backend_name, ", ".join(backend_reprs))
) from e_user

Obviously sql_server.pyodbc is not in django.db.backends. 

But if I run the project ye olde worlde way i.e. "python3 manage.py runserver", the project at least accepts the DATABASES setting and moves on to another error about the model.

So there is an issue with this project and Pycharm. I've tried adding the django-mssql-backend package as a dependency and that introduced an earlier error.

Any ideas?

4 comments
Comment actions Permalink

Hi, I'd say the difference is probably in interpreters. Could you please provide `which python3` output from the terminal, and screenshot of your project interpreter settings in PyCharm?

0
Comment actions Permalink

The terminal only gives me this output:

python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

Well, until I actually try to open a web page, at least.

The Pycharm interpretors are:

Any help appreciated.

 

 

 

0
Comment actions Permalink

In PyCharm your interpreter is `venv/bin/python`. It's not necessarily the same as `python3`. Each interpreter may also have different Django versions installed.

You can check the path to python3 by using `which python3` command, then configure a new project interpreter with this path. Then, you should get the same behavior as in terminal.

0
Comment actions Permalink

Thanks very much.  Pycharm was displaying the correct interpreter option all along, I just needed to select it. 

0

Please sign in to leave a comment.