0

I am having an issue while importing 'registration' app into any of my python scripts (PyCharm showing an error - "Unresolved reference 'registration'"). Django-registration-redux has been installed and loads fine e.g. from urls.py. I also have an issue with my own apps, which I never had an issue with. In the following urls.py user_profile is also not recognized. How do you go a folder level up rather than down, I know that if you are going into folders you include a dot for every folder, but how do you go back? This is probably not relevant for this case but in general. This is my project layout:

|--'project'
|   |--'Lib'
|   |   |--'site-packages'
|   |   |   |--'registration'
|   |--'src'
|   |   |--'proj'
|   |   |   |-- settings.py
|   |   |   |-- urls.py
|   |   |--'user_profile'
|   |   |   |-- forms.py

urls.py

from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
import user_profile.views as views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home),
    url(r'^accounts/', include('registration.backends.default.urls')),
]

forms.py

from registration.forms import RegistrationFormUniqueEmail
from django import forms

class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
    field = forms.CharField()

settings.py

    import os

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

        INS

TALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',  # manually added
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # third party apps
        'crispy_forms',
        'debug_toolbar',
        # my apps
        'user_profile',
        'registration',
    ]
Curtis
  • 1,157
  • 4
  • 17
  • 30

3 Answers3

3

In PyCharm, go to File -> Settings. In the tabs on the left, choose Project: <your_project_name> -> Project Interpreter and choose the path to your virtualenv. That should get PyCharm to recognize the packages installed in your virtualenv.

As for the problem with your own apps, there is a nice thread about the pros and cons of relative and absolute imports.

Community
  • 1
  • 1
Aylen
  • 3,524
  • 26
  • 36
2

You don't "go back", because you're not "in" anywhere to go back from.

Python uses the PYTHONPATH environment variable to determine where to import things from. A virtualenv will automatically put its root and the site-packages directory into that variable. If your virtualenv is the "project" directory, you probably need to do from src.user_profile import views.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • It looks I'm getting there - `from project.src.user_profile import views`. So `user_profile` is resolved. But I can't use the same approach to reference to `registration`. Python doesn't seem to like `site-packages`, maybe it's just a dash sign? How can I access these apps without extra reference, can I change `settings.py` so that I can simply say `from user_profile import views` and `import registration`. Sorry for a long comment! – Curtis May 09 '16 at 12:34
  • As I said, site-packages is on your python path already, so you just need to do `from registration import...`. – Daniel Roseman May 09 '16 at 12:53
0

You can use something called 'relative imports'. Say you need to access your settings from your forms.py, you can do this

import from ..settings import YOUR_SETTING

Hope this helps!

Ward
  • 2,802
  • 1
  • 23
  • 38