最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python代码运行出错:SyntaxError invalid syntax

Django crifan 14303浏览 0评论

别人的python的Django代码:

#!/usr/bin/env python

import os

import sys

if __name__ == "__main__":

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.development.settings")

    try:

        from django.core.management import execute_from_command_line

    except ImportError as exc:

        raise ImportError(

            "Couldn’t import Django. Are you sure it’s installed and "

            "available on your PYTHONPATH environment variable? Did you "

            "forget to activate a virtual environment?"

        ) from exc

    execute_from_command_line(sys.argv)

运行

./manage.py migrate

出错:

其中:

之前是:

Python 2.7.10

现在已经切换为:

Python 3.6.4

了,还是出错:

再去换另外一个python 3的解析器:

结果:

问题依旧。

然后想到了:

估计是默认解析器的问题,换成Python3:

➜  NaturlingCmsServer git:(master) ✗ python3 ./manage.py migrate

Traceback (most recent call last):

  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/config.py", line 558, in configure

    handler = self.configure_handler(handlers[name])

  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/config.py", line 731, in configure_handler

    result = factory(**kwargs)

  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1030, in __init__

    StreamHandler.__init__(self, self._open())

  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1059, in _open

    return open(self.baseFilename, self.mode, encoding=self.encoding)

FileNotFoundError: [Errno 2] No such file or directory: ‘/Users/crifan/dev/dev_root/company/naturling/projects/NaturlingCms/server/NaturlingCmsServer/logs/development.log’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "./manage.py", line 15, in <module>

    execute_from_command_line(sys.argv)

  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line

    utility.execute()

  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute

    django.setup()

  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup

    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

  File "/usr/local/lib/python3.6/site-packages/django/utils/log.py", line 73, in configure_logging

    logging_config_func(logging_settings)

  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/config.py", line 795, in dictConfig

    dictConfigClass(config).configure()

  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/config.py", line 566, in configure

    ‘%r: %s’ % (name, e))

ValueError: Unable to configure handler ‘file’: [Errno 2] No such file or directory: ‘/Users/crifan/dev/dev_root/xxx/logs/development.log’

然后发现是:

(别人少了)项目根目录中,需要创建文件夹logs,然后就可以了:

但是又有其他错误了。

【总结】

此处,由于Mac中默认的解析器是Python2,

用别人的:其自己的Mac默认解析器据说是Python3

导致此处运行:

./manage.py migrate

出错,去改为:

python3 ./manage.py migrate

即可。

转载请注明:在路上 » 【已解决】Python代码运行出错:SyntaxError invalid syntax

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.192 seconds, using 22.10MB memory