【问题标题】:Python - importing fastai results in SyntaxErrorPython - 导入 fastai 导致 SyntaxError
【发布时间】:2019-04-14 22:35:25
【问题描述】:

我正在尝试使用 fastai 设置 ML 模型,并且必须执行以下导入:

import fastai.models
import fastai.nlp
import fastai.dataset

但是,fastai 导入给了我以下错误。

Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastai.nlp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/fastai/nlp.py", line 172
    if os.path.isdir(path): paths=glob(f'{path}/*.*')
                                                   ^
SyntaxError: invalid syntax

显然,glob(f'{path}/*.*') 中的字符 f 导致了错误。我通过删除f 修复了该错误,但fastai 库中似乎有很多这些错误。

我目前的想法是我使用了不正确的 python 版本。谁能给我一些指示?

【问题讨论】:

    标签: python machine-learning pytorch fast-ai


    【解决方案1】:

    以下形状的字符串:

    f'{path}/*.*'
    

    被称为 f-strings 并且在 Python3.6 中被引入。 这就是为什么你会得到SyntaxError - 对于低于 Python3.6 的版本,SyntaxError 将被提升,因为这种语法在较低版本中不存在。

    所以很明显 fast-ai 是为 Python3.6 或更高版本编写的。

    当您查看installation issues (you have to scroll down a bit)时,
    您可以在 Is My System Supported? 下看到第一点:

    Python:您需要有 python 3.6 或更高版本

    所以恐怕更新你的python是解决问题的最简单方法!


    如果您想了解更多关于 f-strings 的信息,可以在这里查看:https://www.python.org/dev/peps/pep-0498/

    【讨论】:

    • 感谢您的回复。我也在 python 3.6.6 上安装了 fastai。它允许我做import fastai,但不允许我做import fastai.dataset。它说包dataset 不存在。但是,我确实需要模块 dataset
    • @Fan 我不确定,但我相信它已更改为 fastai.datasets - 你可以试试吗?那么import fastai.datasets
    猜你喜欢
    • 2011-08-07
    • 2021-07-16
    • 2018-07-03
    • 2011-02-05
    • 2021-08-18
    • 2019-03-18
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多