Python Django shell脚本定义错误

Python Django shell脚本定义错误,python,django,python-3.x,shell,Python,Django,Python 3.x,Shell,我有一个包含以下代码的文件test.py: def getTrue(): return True def getSome(): return getTrue() somevar = getSome() print(somevar) 当我使用 python manage.py shell < test.py 添加后 import unicodedata 到文件顶部,然后尝试使用unicodedata中的某些函数。我收到以下错误: NameError: name 'u

我有一个包含以下代码的文件
test.py

def getTrue():
    return True

def getSome():
    return getTrue()

somevar = getSome()
print(somevar)
当我使用

python manage.py shell < test.py
添加后

import unicodedata
到文件顶部,然后尝试使用unicodedata中的某些函数。我收到以下错误:

NameError: name 'unicodedata' is not defined
我看不出我的问题被标记为可能重复的问题的答案是如何回答这一部分的

当我正常运行文件时

python3 /path/to/file/test.py 
没有问题,
True
按预期打印


知道发生了什么吗?

将脚本管道化到Django shell就像复制并粘贴到shell中一样。shell的“智能”自动缩进逻辑经常会混淆,并过度识别粘贴的代码


尝试安装最新的iPython,此问题已得到解决。Django将使用iPython作为shell(如果已安装)。

执行此操作时,脚本会在
test.py
中的代码上内部调用
exec()
。在Python3中,这将失败-有关原因的详细解释,请参阅
python3 /path/to/file/test.py