我遇到了一个非常奇怪的问题。
编辑:添加完整详细信息:
virtualenv -p python3 testenv
cd testenv
source bin/activate
django-admin startproject blah
cd blah
mkdir modules
vi modules/stash.py
pip install django
pip install stashy
python manage.py shell < modules/stash.py我有一个同步数据以填充数据库的脚本:
$ cat modules/stash.py
import stashy
class SyncProject:
def __init__(self, endpo, user, passw):
import stashy
self.stash = stashy.connect(endpo, user, passw)
access = SyncProject("http://localhost", "test", "test" )我用以下命令运行我的脚本:
$ python manage.py shell < modules/stash.py奇怪的是,如果我不放入第二个import stashy,它就不能工作:
NameError: name 'stashy' is not defined从我很久以前的Python代码来看,这似乎是非常出乎意料的,迫使我在每个方法中都添加了import……不确定我导入依赖项的方式或运行脚本的方式是否有问题...
编辑:更多详细信息,错误消息:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/[PATH]/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/[PATH]/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/[PATH]/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/[PATH]/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/[PATH]/python3.6/site-packages/django/core/management/commands/shell.py", line 92, in handle
exec(sys.stdin.read())
File "<string>", line 6, in <module>
File "<string>", line 4, in __init__
NameError: name 'stashy' is not definedhttps://stackoverflow.com/questions/47748824
复制相似问题