ImportError: No module named 'settings'

Question:

I have two versions of python installed 2.7 and 3.4 and created a virtualenv and assigned python34 to that new environment. When I activate that virtualenv and run python manage.py runserver then I get the following output.

(casenv) C:pyprojectsfocussite>python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:virtualenvscasenvlibsite-packagesdjangocoremanagement__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:virtualenvscasenvlibsite-packagesdjangocoremanagement__init__.py", line 341, in execute
    django.setup()
  File "C:virtualenvscasenvlibsite-packagesdjango__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:virtualenvscasenvlibsite-packagesdjangoappsregistry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:virtualenvscasenvlibsite-packagesdjangoappsconfig.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "C:virtualenvscasenvlibimportlib__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:pyprojectsfocussitegeneralmodels.py", line 8, in <module>
    from focus2.util import HashedPk
  File "C:pyprojectsfocussitefocus2util.py", line 3, in <module>
    from settings import Hasher
ImportError: No module named 'settings'

File – manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "focus2.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

The first 2 lines from utils.py —

from django.utils.html import strip_tags, escape
from settings import Hasher

The project directory structure is

├───focus
│   ├───data_dumps
│   ├───notes
│   ├───setup
│   └───site(main project folder)
|       └───static
|       └───general
|       └───pro
|           └───models
|               +--__init__.py
|               +--plans.py
│       └───focus2
│           └───templates
|           +--__init__.py
│           +--settings.py
│           +--util.py            
│           +--wsgi.py
│       +--manage.py

I have the same code and dir structure which runs well with virtualenv python2.7. The difference between python27 env and python34 env is that I have installed mysql-python module in 27 and mysqlclient in 34.

Pip freeze command from python34 env.

(casenv) C:virtualenvscasenv>pip freeze
certifi==2017.7.27.1
chardet==3.0.4
defusedxml==0.5.0
Django==1.10.1
django-ajax-selects==1.5.0
django-allauth==0.32.0
django-crispy-forms==1.6.1
easy-thumbnails==2.3
hashids==1.1.0
idna==2.5
lxml==3.7.3
mysqlclient==1.3.10
oauthlib==2.0.2
Pillow==3.3.1
PyMySQL==0.7.11
python3-openid==3.1.0
requests==2.18.3
requests-oauthlib==0.8.0
urllib3==1.22

Pip freeze command from python27 virtualenv. I don’t get any error while running this venv.

(testvenv27) C:virtualenvstestvenv27>pip freeze
Django==1.10.1
django-ajax-selects==1.5.0
django-allauth==0.27.0
django-crispy-forms==1.6.0
easy-thumbnails==2.3
hashids==1.1.0
lxml==3.4.2
MySQL-python==1.2.5
oauthlib==2.0.0
Pillow==3.3.1
python-openid==2.2.5
requests==2.9.1
requests-oauthlib==0.6.1

The sys.path command output–

>>> import sys
>>> print(sys.path)
['', 'C:\WINDOWS\SYSTEM32\python34.zip', 'C:\virtualenvs\casenv\DLLs', 'C:\virtualenvs\casenv\lib', 'C:\virtualenvs\casenv\Scripts', 'c:\python34\Lib', 'c:\python34\DLLs', 'C:\virtualenvs\casenv', 'C:\virtualenvs\casenv\lib\site-packages']

Any help is highly appreciated.

Asked By: Prithviraj Mitra

||

Answers:

try:

from .settings import Hasher
Answered By: badiya

Go to your directory and select setting in the directory and open it with pycharm. When it open right click the tab and and click open in Terminal then run the code. It works

Answered By: fortune
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.