经过多年的C和C ++编写,我觉得在python和django方面有点笨拙。我似乎无法简单地工作。我承认错误已在一些帖子中讨论过。
我开始浏览一些django教程,所以我运行了'django-admin startproject mysite',它创建了一个'mysite'文件夹。
我已经安装了django_extensions所以现在我正在尝试使用它的'runscript'。我有一个更复杂的脚本,我想稍后运行但是现在,我试图调用一个简单的用户脚本,我通过这种机制在网上发现,即
mysimplescript.py:
def run(*script_args):
print script_args
它位于'scripts'文件夹中,所以我的结构如下,其中外部mysite / root目录只是我项目的容器:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
scripts
__init__.py
mysimplescript.py
当我按如下方式运行脚本时,我得到相对导入错误。
python manage.py runscript .scripts.mysimplescript.py --script-args
Testing 123
/usr/lib/python2.7/site-packages/Django-1.9.2-py2.7.egg/django/core
/management/base.py:265: RemovedInDjango110Warning:
OptionParser usage for Django management commands is deprecated, use
ArgumentParser instead
RemovedInDjango110Warning)
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/Django-1.9.2-py2.7.egg/django/core
/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/Django-1.9.2-py2.7.egg/django/core
/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/
django_extensions/management/email_notifications.py", line 63, in
run_from_argv
super(EmailNotificationCommand, self).run_from_argv(argv)
File "/usr/lib/python2.7/site-packages/Django-1.9.2-py2.7.egg/django/core
/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/
django_extensions/management/email_notifications.py", line 75, in execute
super(EmailNotificationCommand, self).execute(*args, **options)
File "/usr/lib/python2.7/site-packages/Django-1.9.2-py2.7.egg/django/core
/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/
django_extensions/management/utils.py", line 57, in inner
ret = func(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/
django_extensions/management/commands/runscript.py", line 163, in handle
modules = find_modules_for_script(script)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/
django_extensions/management/commands/runscript.py", line 146, in
find_modules_for_script
mod = my_import(nn)
File "/usr/lib/python2.7/site-packages/django_extensions-1.6.1-py2.7.egg/
django_extensions/management/commands/runscript.py", line 99, in my_import
importlib.import_module(mod)
File "/usr/lib64/python2.7/importlib/__init__.py", line 30, in import_module
raise TypeError("relative imports require the 'package' argument")
TypeError: relative imports require the 'package' argument
如果我包含调试,那么我可以看到以下@ runscript.py#99: 检查.scripts.mysimplescript.scripts.py
最初运行了一些django教程后,已经设置了以下内容:
./mysite/wsgi.py:os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"mysite.settings")
./manage.py: os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"mysite.settings")
./mysite/settings.py:ROOT_URLCONF = 'mysite.urls'
关于该主题的一些讨论,例如[1]:python packaging for relative imports和[2]:How to do relative imports in Python?建议在外部mysite /目录中添加main.py。
实质上,我仍然希望使用:
通过django-extensions调用脚本python manage.py runscript <some_script>
再说一点。如果我放弃'。'在文件夹之前,即
python manage.py runscript scripts.mysimplescript.py --script-args Testing
123
然后我收到以下错误:
No (valid) module for script 'scripts.mysimplescript.py' found
然后生成'mysimplescript.pyc'。
是的,有人能帮帮我吗?如何让python识别包?提前谢谢。答案 0 :(得分:1)
示例in the docs是将脚本保存为scripts/delete_all_questions.py
,然后使用
python manage.py runscript delete_all_questions
在您的情况下,您的脚本为scripts/mysimplescript.py
,因此您使用
python manage.py runscript mysimplescript --script-args Testing 123
请注意,您使用的是模块名称mysimplescript
,因此不应包含.py
。 django_extensions应用程序假定脚本位于scripts
目录中,因此不要包含该目录。
答案 1 :(得分:0)
这将有效..
python manage.py runscript mysimplescript --script-args Testing
123
答案 2 :(得分:0)
相对导入只能从python包运行。您使用相对导入的目录: 1.包含 init .py文件 2.可用于PYTHONPATH(声明为另一个目录的路径或子目录,其中 init .py并且在路径上)
换句话说,除非你调用整个包,否则相对路径在脚本中不起作用:
$ python -m manage.py runscript mystie.scripts.mysimplescript