SyntaxError:在Linux上部署lib/python3.8/site-packages/sql_server/



**

  1. 前言(使用操作系统测试HyperV虚拟服务器:在本地Windows PC上测试Ubuntu服务器20.04.1(

**

我在使用MS SQL Server作为数据库的Ubuntu Server 20.04.1 LTS上部署Django应用程序(使用Python 3.8.2(时遇到问题。在Linux上恢复到"ENGINE":"django.db.backends.sqlite3"没有问题,但我必须说实话,这很令人沮丧。

我已经不得不降级到Django==3.0才能让我的应用程序在我的本地windows pc上运行,但我所做的两次迁移证明我错过了一些东西,我希望有人能帮我一把。感谢您的帮助。

  1. 我的电脑

工作设置.py文件(当前在带有Sql Server 2019 Express 的windows上运行

DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mdm_db',
'USER': 'AAA',
'PASSWORD': 'AAA',
'HOST': 'localhost\SQLEXPRESS',
'PORT': '',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
# 'driver': 'ODBC Driver 13 for SQL Server ', -> port 1433?
},
},
}
installed components:
pip freeze
asgiref==3.2.10
Django==3.0
django-mssql-backend==2.8.1
pyodbc==4.0.30
pytz==2020.1
sql-server.pyodbc==1.0
sqlparse==0.3.1
  1. Ubuntu服务器

(也尝试过使用ODBC并安装最新版本https://learn.microsoft.com/it-it/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-服务器15(

DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'mdm_db',
'USER': 'mdmuser',
'PASSWORD': 'mdmuser',
'HOST': 'localhost\SQLEXPRESS',
'PORT': '',
'OPTIONS': {
#'driver': 'SQL Server Native Client 11.0',
'driver': 'ODBC Driver 17 for SQL Server ',
},
},
}

已安装的程序包

:~/app/MDM_SQLServer$ history | grep install
32  sudo apt-get install unixodbc-dev
34  sudo apt-get install python3-dev
36  sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
60  sudo ACCEPT_EULA=Y apt-get install msodbcsql17
  1. 问题:(venv(luca@webserver:~/app/MDM_SQLServer$python manage.py runserver

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/luca/app/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/luca/app/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/luca/app/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/home/luca/app/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/home/luca/app/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/luca/app/venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/luca/app/venv/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/luca/app/venv/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/luca/app/MDM_SQLServer/cpus/models.py", line 6, in <module>
from sites.models import Site
File "/home/luca/app/MDM_SQLServer/sites/models.py", line 4, in <module>
from wifis.models import Wifi
File "/home/luca/app/MDM_SQLServer/wifis/models.py", line 3, in <module>
class Wifi(models.Model):
File "/home/luca/app/venv/lib/python3.8/site-packages/django/db/models/base.py", line 121, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/luca/app/venv/lib/python3.8/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/home/luca/app/venv/lib/python3.8/site-packages/django/db/models/options.py", line 208, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/luca/app/venv/lib/python3.8/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/luca/app/venv/lib/python3.8/site-packages/django/db/utils.py", line 207, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/luca/app/venv/lib/python3.8/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/luca/app/venv/lib/python3.8/site-packages/sql_server/pyodbc/base.py", line 7
except ImportError, e:
^
SyntaxError: invalid syntax

注意提示没有返回,我需要用corntol+C输出。

Test2 New Env

luca@webserver:~/app/MDM_SQLServer$ source test2/bin/activate
(test2) luca@webserver:~/app/MDM_SQLServer$ pipinstall -r req
req.txt           requirements.txt
(test2) luca@webserver:~/app/MDM_SQLServer$ pipinstall -r req.txt
pipinstall: command not found
(test2) luca@webserver:~/app/MDM_SQLServer$ pip install -r req.txt
Collecting asgiref==3.2.10
Using cached asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting Django==3.0
Using cached Django-3.0-py3-none-any.whl (7.4 MB)
Collecting django-mssql-backend==2.8.1
Using cached django_mssql_backend-2.8.1-py3-none-any.whl (52 kB)
Collecting pyodbc==4.0.30
Using cached pyodbc-4.0.30.tar.gz (266 kB)
Collecting pytz==2020.1
Using cached pytz-2020.1-py2.py3-none-any.whl (510 kB)
Processing /home/luca/.cache/pip/wheels/a9/78/cf/dc91ed794c352243f2796a29991293fd578c91f6584b0682f7/sql_server.pyodbc-1.0-py3-none-any.whl
Collecting sqlparse==0.3.1
Using cached sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
Building wheels for collected packages: pyodbc
Building wheel for pyodbc (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /home/luca/app/MDM_SQLServer/test2/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-tth7v6fw/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-tth7v6fw/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-lr00jtg6
cwd: /tmp/pip-install-tth7v6fw/pyodbc/
Complete output (6 lines):
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: invalid command 'bdist_wheel'
----------------------------------------
ERROR: Failed building wheel for pyodbc
Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: asgiref, pytz, sqlparse, Django, pyodbc, django-mssql-backend, sql-server.pyodbc
Running setup.py install for pyodbc ... done
Successfully installed Django-3.0 asgiref-3.2.10 django-mssql-backend-2.8.1 pyodbc-4.0.30 pytz-2020.1 sql-server.pyodbc-1.0 sqlparse-0.3.1
(test2) luca@webserver:~/app/MDM_SQLServer$

提前感谢

错误消息中显示的except ImportError, e语法已弃用。pyodbc仅声称支持Python 2.7、3.4、3.5和3.6。

考虑到除了Python3.6之外的所有版本都已经到了生命的尽头,我怀疑pyodbc已经没有太多维护了。理想情况下,找一个更好的图书馆。如果必须使用这个,请尝试使用Python 3.6。

我通过pip-django pyodbc azure-2019==2.1.0.0安装解决了我的问题;正常的";django azure sql后端与sql Server 2019存在问题。我得到的错误是获取不支持的SQL Server v15:此处为解决方案。最后(TEST2:见下文(,我发现,我认为,这是与带有django的SQL Server数据库一起使用的最好和最新的lib。

以下是我在一个干净的虚拟环境中必须采取的必要步骤:

luca@webserver:~/app/MDM_SQLServer$ python3 -m venv venv2019
luca@webserver:~/app/MDM_SQLServer$ source venv2019/bin/activate
(venv2019) luca@webserver:~/app/MDM_SQLServer$ **pip install django**
Collecting django
Downloading Django-3.1.1-py3-none-any.whl (7.8 MB)
|████████████████████████████████| 7.8 MB 3.7 MB/s
Collecting asgiref~=3.2.10
Using cached asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
Collecting pytz
Using cached pytz-2020.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: asgiref, sqlparse, pytz, django
Successfully installed asgiref-3.2.10 django-3.1.1 pytz-2020.1 sqlparse-0.3.1
(venv2019) luca@webserver:~/app/MDM_SQLServer$ pip install django-pyodbc-azure-2019
Collecting django-pyodbc-azure-2019
Using cached django_pyodbc_azure_2019-2.1.0.0-py3-none-any.whl (37 kB)
Collecting pyodbc>=3.0
Using cached pyodbc-4.0.30.tar.gz (266 kB)
Collecting Django<2.2,>=2.1.0
Using cached Django-2.1.15-py3-none-any.whl (7.3 MB)
Requirement already satisfied: pytz in ./venv2019/lib/python3.8/site-packages (from Django<2.2,>=2.1.0->django-pyodbc-azure-2019) (2020.1)
Building wheels for collected packages: pyodbc
Building wheel for pyodbc (setup.py) ... error
**ERROR: Command errored out with exit status 1:
command: /home/luca/app/MDM_SQLServer/venv2019/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-hwv29w6j/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-hwv29w6j/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-ot4s3z6t
cwd: /tmp/pip-install-hwv29w6j/pyodbc/
Complete output (6 lines):
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: invalid command 'bdist_wheel'
----------------------------------------
ERROR: Failed building wheel for pyodbc
Running setup.py clean for pyodbc
Failed to build pyodbc**
Installing collected packages: pyodbc, Django, django-pyodbc-azure-2019
Running setup.py install for pyodbc ... done
Attempting uninstall: Django
Found existing installation: Django 3.1.1
Uninstalling Django-3.1.1:
Successfully uninstalled **Django-3.1.1**
Successfully installed **Django-2.1.15** django-pyodbc-azure-2019-2.1.0.0 pyodbc-4.0.30
(venv2019) luca@webserver:~/app/MDM_SQLServer$ pip freeze
asgiref==3.2.10
**Django==2.1.15**
**django-pyodbc-azure-2019==2.1.0.0**
pyodbc==4.0.30
pytz==2020.1
sqlparse==0.3.1
(venv2019) luca@webserver:~/app/MDM_SQLServer$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
September 19, 2020 - 22:50:34
Django version 2.1.15, using settings 'mdm.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

TEST2[lib django mssql后端2.8.1]新希望(不想使用django 2.1.15版本(

luca@webserver:~/app/MDM_SQLServer$ python3 -m venv venvSQL
luca@webserver:~/app/MDM_SQLServer$ source venvSQL/bin/activate
(venvSQL) luca@webserver:~/app/MDM_SQLServer$ pip install django
Collecting django
Using cached Django-3.1.1-py3-none-any.whl (7.8 MB)
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
Collecting asgiref~=3.2.10
Using cached asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting pytz
Using cached pytz-2020.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: sqlparse, asgiref, pytz, django
Successfully installed asgiref-3.2.10 django-3.1.1 pytz-2020.1 sqlparse-0.3.1
(venvSQL) luca@webserver:~/app/MDM_SQLServer$ pip install django-mssql-backend
Collecting django-mssql-backend
Using cached django_mssql_backend-2.8.1-py3-none-any.whl (52 kB)
Collecting pyodbc>=3.0
Using cached pyodbc-4.0.30.tar.gz (266 kB)
Building wheels for collected packages: pyodbc
Building wheel for pyodbc (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /home/luca/app/MDM_SQLServer/venvSQL/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ryv4vfy_/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ryv4vfy_/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-xzt70143
cwd: /tmp/pip-install-ryv4vfy_/pyodbc/
Complete output (6 lines):
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
**error: invalid command 'bdist_wheel'**
----------------------------------------
ERROR: Failed building wheel for pyodbc
Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: pyodbc, django-mssql-backend
Running setup.py install for pyodbc ... done
Successfully installed django-mssql-backend-2.8.1 pyodbc-4.0.30
(venvSQL) luca@webserver:~/app/MDM_SQLServer$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): auth.
Run 'python manage.py migrate' to apply them.
September 19, 2020 - 22:57:31
**Django version 3.1.1**, using settings 'mdm.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

相关内容

  • 没有找到相关文章

最新更新