【问题标题】:import json not working with shell and manage.py Django导入 json 不适用于 shell 和 manage.py Django
【发布时间】:2021-06-16 11:47:00
【问题描述】:

我正在为我的 Django 项目编写一些 python 脚本。我正在使用 manage.py 和 shell 运行它。

如果我使用 manage.py 运行 import json 并出售它,则它不起作用。

但是,如果我尝试不使用 shell 和 manage.py,它会起作用。我不知道为什么会这样。

使用 manage.py 和 shell:(不工作)

python3 manage.py shell < custom_scripts/imports.py
Error: NameError: name 'json' is not defined

没有 manage.py 和 shell:(有效)

python3 custom_scripts/imports.py

脚本内的代码:

    import json
    
    print(json.loads("{}"))

有什么想法吗?

【问题讨论】:

    标签: python python-3.x django django-manage.py


    【解决方案1】:

    不推荐从shell 执行此类操作,如果此操作将多次运行,您可以创建自定义命令来为您运行。

    请参阅answer by @danodonovan 了解类似问题。

    【讨论】:

    • 这只是我需要运行的一次性脚本。
    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2018-07-27
    • 1970-01-01
    相关资源
    最近更新 更多