gpt4 book ai didi

Python/Django 类型错误 : relative imports require the 'package' argument

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:32 25 4
gpt4 key购买 nike

在用 C 和 C++ 写了多年之后,当谈到 python 和 django 时,我感觉有点笨。我似乎无法做一些简单的事情。我承认这个错误已经在很多帖子中讨论过。

我开始学习一些 django 教程,因此我运行了“django-admin startproject mysite”,它创建了一个“mysite”文件夹。

我已经安装了 django_extensions,所以现在我尝试使用它的“runscript”。我有一个更复杂的脚本,我想稍后运行,但现在,我正在尝试调用一个简单的用户脚本,该脚本是我通过此机制在网上找到的,即

mysimplescript.py:

def run(*script_args):
print script_args

它位于“scripts”文件夹中,因此我的结构如下,其中外部 mysite/根目录只是我的项目的容器:

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识别这个包?提前致谢。

最佳答案

示例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 目录中,因此也不要包含该目录。

关于Python/Django 类型错误 : relative imports require the 'package' argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35226883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com