Python/Django TypeError: relative imports require the 'package' argument -
after years of writing in c , c++, feel bit of dunce when comes python , django. can't seem simple work. concede error has been discussed in number of posts.
i started going through django tutorials have run 'django-admin startproject mysite' has created 'mysite' folder.
i've installed django_extensions i'm trying use 'runscript'. have more complex script want run later now, i'm trying invoke simple user script found online via mechanism i.e.
mysimplescript.py: def run(*script_args): print script_args
it located in 'scripts' folder structure follows, outer mysite/ root directory container project:
mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py scripts __init__.py mysimplescript.py
when run script follows, relative imports error.
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 django management commands 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 'package' argument") typeerror: relative imports require 'package' argument
if include debug, can see following @ runscript.py #99: check .scripts.mysimplescript.scripts.py
having run django tutorials, following have been set:
./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'
some of discussions on topic e.g. [1]:python packaging relative imports , [2]:how relative imports in python? suggest adding main.py in outer mysite/ directory.
in essence, though, still want invoke script via django-extensions using:
python manage.py runscript <some_script>
just further point. if drop '.' preceding folder i.e.
python manage.py runscript scripts.mysimplescript.py --script-args testing 123
then following error:
no (valid) module script 'scripts.mysimplescript.py' found
yet 'mysimplescript.pyc' generated.
can me please? how can python recognize package? in advance.
the example in docs save script scripts/delete_all_questions.py
, , run
python manage.py runscript delete_all_questions
in case, script scripts/mysimplescript.py
, run with
python manage.py runscript mysimplescript --script-args testing 123
note use module name mysimplescript
, shouldn't include .py
. django_extensions app assumes scripts in scripts
directory, don't include either.
Comments
Post a Comment