I keep getting the error "ModuleNotFoundError: No module named 'environ' in my settings.py file". I have installed the dependency in my python shell
Asked Answered
S

4

15

I keep getting an import error on environ in my settings.py file, I have it installed via poetry in my .venv file as well. Could this be an error outside the settings file possibly?

`
import environ

env = environ.Env(
    DEBUG=(bool, False),
    ENVIORNMENT=(str, 'PRODUCTION'),
)

environ.Env.read_env()

ENVIRONMENT= env.str('ENVIRONMENT')


SECRET_KEY = env.str('SECRET_KEY')

DEBUG = env.bool('DEBUG')

ALLOWED_HOSTS = tuple(env.list('ALLOWED_HOSTS'))

`
Savior answered 20/8, 2020 at 19:25 Comment(1)
if you have a virtual environment you're working with, make sure when you run your script it is run in that environmentDremadremann
S
19

Make sure that you are using the desired python interpreter, that your virtualenv is setup correctly, and that the desired django-environ is installed within that virtualenv via

(inside venv) pip install django-environ
Seasickness answered 20/8, 2020 at 19:35 Comment(0)
G
2

The problem could occur due to the following reasons:

  1. You are using. Virtual environment, but you installed module outside the virtual environment.
  2. You haven't added 'environ', in your your settings.py file in INSTALLED_APPS.(based on its reference exceptionally not required for this package!)
Gorcock answered 20/8, 2020 at 19:34 Comment(3)
I'm having the same issue. I installed everything inside of my virtual environment and am attempting to run $ python manage.py runserver with my virtual environment open and I'm getting "raise ImproperlyConfigured(error_msg) django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable"Severen
Sorry for the late response. The issue seems like in your settings file, for secret_key value, you are getting value from environment variable. but environment variable is not set. SOLUTION : 1. either set up secret key variable value in environment variable on your computer. 2. set up variable value in setting.py file ...like secret_key = "any-random-text"Gorcock
You don't have to add django-environ to INSTALLED_APPS for it to work: django-environ.readthedocs.io/en/latest/…Unerring
K
2

Make sure you have done the following three actions:

  1. Install the package through this command:

    (inside venv) pip install django-environ

  2. Select the right python interpreter(the environment in which you have installed the package)

  3. Create an ".env" file in project root directory. And based on its reference doc here, it should be consisting of something like below:

DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://user:[email protected]:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret
Karlykarlyn answered 20/12, 2021 at 19:55 Comment(0)
D
0

In my case, I had one environ.py file that was clashing with the library import, changing the file name fixed the issue.

For Django there is another option that I use,

from os import getenv
from dotenv import load_dotenv

load_dotenv()

create a .env file along with settings.py then you have to load those variables. For example :

CONFIGURATION = getenv("CONFIGURATION")

Install the module using

pip install python-dotenv
Domination answered 23/5, 2023 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.