Forums

Cannot import django.urls.path during pa_autoconfigure_django.py

I'm new to Django and have been going through the Mozilla Tutorial using PyCharm on a Windows machine. While doing the Djangogirls tutorial and couple of weeks ago, I learned about PythonAnywhere and had the Djangogirls working on PythonAnywhere. I got rid of that web app and now I've trying to deploy Mozilla the same way - it's working on my Windows machine and I want to migrate it to PythonAnywhere. I'm following a step using pa_autoconfigure_django.py and the command failing while running migrate database:

< Running migrate database >
   \
    ~<:>>>>>>>>>
Traceback (most recent call last):
  File "/home/pfuntner/pfuntner.pythonanywhere.com/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/home/pfuntner/pfuntner.pythonanywhere.com/locallibrary/urls.py", line 17, in <module>
    from django.urls import path
ImportError: cannot import name 'path'
Traceback (most recent call last):
  File "/home/pfuntner/.local/bin/pa_autoconfigure_django.py", line 56, in <module>
    main(arguments['<git-repo-url>'], arguments['--domain'], arguments['--python'], nuke=arguments.get('--nuke'))
  File "/home/pfuntner/.local/bin/pa_autoconfigure_django.py", line 43, in main
    project.run_migrate()
  File "/home/pfuntner/.local/lib/python3.6/site-packages/pythonanywhere/django_project.py", line 96, in run_migrate
    'migrate',
  File "/usr/lib/python3.6/subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '[PosixPath('/home/pfuntner/.virtualenvs/pfuntner.pythonanywhere.com/bin/python'), PosixPath('/home/pfuntner/pfuntner.pythonanywhere.com/manage.py'), 'migrate']' returned
 non-zero exit status 1.
10:25 ~ $

How do I fix this?

I was using Django 2.0.2 with PyCharm but it looks like 1.11.10 was used on PythonAnywhere. I tried dropping PyCharm down to 1.11.10 got the same error while trying to start the site. So I think it's a Django version issue.

Fixed with a requirements.txt file!! Niiiiiiiiiiiiiiiiiiiiiiiice!

:-)

please how did you fix it with .txt file

pfuntner added a requirements file to specify the packages that were needed by their app so the script could install them when it created the virtualenv.

I'm having the exact same problem trying to upload new Django Project. ..."returned non-zero exit status 1."

Would appreciate a hearing how to set up the requirements.text if this is the fix. Thanks

can you give the full error stack trace to help debug?

For me, I tried to use the newest version of Django (2.0.2?) in the local version, which caused this exact error. I had to basically through through the entire tutorial again with a new folder and install the old Django they specify (1.10.1? Something like this). Rerunning this command in Pythonanywhere's bash worked for me after that!

Right, this definitely looks like it must be a Django version compatibility issue. We'll need to update the autoconfigure script so that it can support both Django 1.x and 2.x.

Thanks all. I'm using Django 2.0.2 Here is the Full Error Stack Trace.

< Running migrate database >
   \
    ~<:>>>>>>>>>
Traceback (most recent call last):
  File "/home/resilientstudio/resilientstudio.pythonanywhere.com/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", l
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/utils/functional.py",
 line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/site-packages/django/urls/resolvers.py", l
ine 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/lib/python3.6/importlib/__init__.py", line 126, in impor
t_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/home/resilientstudio/resilientstudio.pythonanywhere.com/base/urls.py", line 18, in <module>
    from django.urls import path
ImportError: cannot import name 'path'
Traceback (most recent call last):
  File "/home/resilientstudio/.local/bin/pa_autoconfigure_django.py", line 56, in <module>
    main(arguments['<git-repo-url>'], arguments['--domain'], arguments['--python'], nuke=arguments.get('--nuke'))
  File "/home/resilientstudio/.local/bin/pa_autoconfigure_django.py", line 43, in main
    project.run_migrate()
  File "/home/resilientstudio/.local/lib/python3.6/site-packages/pythonanywhere/django_project.py", line 96, in run_migrate
    'migrate',
  File "/usr/lib/python3.6/subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '[PosixPath('/home/resilientstudio/.virtualenvs/resilientstudio.pythonanywhere.com/bin/python')
, PosixPath('/home/resilientstudio/resilientstudio.pythonanywhere.com/manage.py'), 'migrate']' returned non-zero exit status 1.

The script doesn't work for Django 2.x right now :-(

I'm also having this issue

Which Django version are you using?

@giles django 2

OK -- you won't be able to use the pa_autoconfigure script for Django 2.x until we've upgraded it to handle the new Django version.

Any idea as to when you will be upgrading the script file?

No ETA yet -- we're working full-time on getting PythonAnywhere ready for the GDPR, which (as it could involve multi-million euro fines if we get it wrong) has to be our top priority right now :-(

Just confirmed this is still an issue if you're using the djangogirls tutorial and accidentally installed django 2 instead of django 1 which they're using in the tutorial (https://tutorial.djangogirls.org/en/deploy/)

Another workaround is using the django.conf.urls module instead of the django.path module (which doesn't exist in django 1.11 installed by the pa_autoconfigure_django script).

I changed my urls.py file from

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

to

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

To get the site to deploy.

Note: this will require further deviation from the tutorial as you continue. This is a good workaround if you started the tutorial using django 2 and are trying to avoid restarting with an earlier version.

yes- because the django girls tutorial is still on 1.x, our pa-autoconfigure script also only works for 1.x, not django 2.x

As of today I ran into the same problem - started with the Djangogirls tutorial and ran the autoconfigure script with the above mentioned issues. I do not reeeally want to install an earlier version of Django, so is there a description/documentation of how to link my git repo (I used Bitbucket instead but shouldn't matter) manually to my account? So that I go through all necessary steps without using the automated script? Can anyone please provide a link? Do I have to uninstall/delete the project completely again from your servers before I do so? Thanks a lot! [Sorry, Google helped: I'll try these instructions ] :-)

You're getting the error, because the Django Girls code is not for Django 2. It's not a problem with the script. The git repo that Django Girls provides will not run in Django 2.

If you want to, you can start with a git repo that is a minimal Django 1 app, use the autoconfigure script, upgrade your Django in the virtualenv and then continue with development using Django 2, but that will not necessarily follow the Django Girls tutorial.

Ah ok, thanks for your quick reply. I thought it was an error with the automated script and Django 2. So if I had read more carefully, that they use pip install django~=1.11.0 I could have known .. Sorry for that and thanks again for your advice

Hello, i also still have the same issue. Is Django 2x upgraded?

What tutorial are you trying to follow, and which python version are you using?