3

I have installed django1.9 with python 2.7. But now I want to use it with python3.4. Hence I have modified symbolic link of python to python 3.4 like below.

sudo ln -s /usr/bin/python3.4 /usr/bin/python

Because same django works with python 2.7 and 3.4 as well so it should work. But now if I run ./mange.py runserver I am getting below error. But with Python 2.7 the same code works properly.

from Helpers import views
ImportError: No module named 'Helpers'

Please let me know whats wrong there? Below are the project structure.

myproject
   ├── myproject
   │   ├── settings.py
   │   ├── __init__.py
   │   ├── urls.py   
   │   ├── wsgi.py
   │   └─── Helpers
   │         ├── views.py
   │         └── __init__.py
   └── manage.py

Urls.py is like below.

from django.conf.urls import url
from Helpers import views
urlpatterns = [
    url(r'^$', views.index, name='index')
]

setting.py contains below relevant information.

    INSTALLED_APPS = [ 
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'myproject',
    ]

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    # Look for modules here as well.
    sys.path.insert(0, os.path.join(BASE_DIR, "Helpers"))

Any idea?

Gaurav Pant
  • 4,029
  • 6
  • 31
  • 54

1 Answers1

2

Python 3 has changed the import policy. Take a look at this question.

Instead of adding Helpers directory to sys.path, add it's parent:

sys.path.insert(0, os.path.join(BASE_DIR, 'myproject'))

Or like @albar mentioned - use relative import:

from .Helpers import views
Community
  • 1
  • 1
Bogdan Klichuk
  • 111
  • 1
  • 5