Django - cannot import name 'config' from 'decouple'
Asked Answered
S

9

42

I'm trying to run this project locally but when i try manage.py makemigrations i keep getting the following error:

ImportError: cannot import name 'config' from 'decouple'

Here are my steps:

  1. Clone the repository from github
  2. Create a virtual environment
  3. Install the dependencies

I made some research but i found nothing about what could be generating that error. Can anyone help me out on this? Thanks in advance!I'm running Django 3.

Selfassured answered 25/6, 2020 at 16:41 Comment(1)
I'm having this problem too, but I think the solution with the two Decouple modules could be legit. Otherwise, I would say try using something else. Currently I use Decouple for the .env environment variables in Django and so I hope it works, I've tried other situations like the environ module (which doesn't work). Remember to put .env in your .gitignore though, otherwise you could expose your secret key. If a better answer/solution pops up I'd love to find it. EDIT: Yes, this does work on Windows at least (with Django 3.3) and so thank you Stack OverflowCiracirca
M
170

You might have decouple installed in additional to python-decouple (two different packages).

If that is the case simply uninstall decouple

pip uninstall decouple

And ensure you have python-decouple installed

pip install python-decouple
Merl answered 21/7, 2020 at 1:31 Comment(4)
Thank you! Packages developers should seriously solve thisUnrobe
This is why you should always install dependencies from requirements.txt @RamiAlloushRefection
might also want to try with pip3 install python-decoupleFrizette
conda install -c conda-forge python-decouple works too :)Explore
T
7

The error happens because your python interpreter is looking for python-decouple package instead of decouple

Uninstall the existing package using this command pip uninstall decouple

And then install
pip install python-decouple

Trajectory answered 15/3, 2022 at 12:38 Comment(1)
The procedure worked. I did unistall the decouple package then installed the python-decoupleSweetening
W
2

It shows the error because you have not installed the python-decouple module. Just run:

pip uninstall decouple

Then try

pip install python-decouple
Wigfall answered 26/7, 2023 at 15:18 Comment(1)
Welcome to Stack Overflow! You should always wrap commands in a code block to make them easier to read. Also, there are other reasons this could be happening, so please take the time to research and suggest other possible solutions as well.Everybody
S
1

It shows the error because you are not installed the module python-decouple Just run pip install -r requirements.txt If it won't work then try it in env

pip install python-decouple

Sherrill answered 25/6, 2020 at 17:1 Comment(1)
Weird thing is that i installed Decouple but i still keep getting the errorSelfassured
G
1

I struggled with this for a good hour, the fix was to close the current bash/terminal window and opening a new one.

Gabler answered 7/5, 2021 at 4:28 Comment(0)
F
0

I was using pipenv to create a virtual environment and run the Django project

pipenv install -r requiremnts.txt
pipenv shell
python manage.py runserver

Interestingly above method kept throwing error that it cannot import config from decouple

then I tried:

python -m venv myEnv
myEnv/Scripts/activate
pip install -r requirements.txt
python manage.py runserver

and this ran the project successfully. I didnt understand why this would happen as I am doing pretty much same just using pip instead of pipenv.

Footworn answered 29/7, 2020 at 14:23 Comment(0)
W
0

I encountered the same issue, and in my case, it was caused by a conflict between "decouple" and "python-decouple" packages (I had installed both of them by mistake). As the latter creates a file called "decouple.py" in the installation virtual environment, finding the package of the same name, this will confuse the Django app of which of the 2 decouple reference it will consider. To fix this I had to uninstall the "decouple" package with pipenv uninstall decouple and install the "python-decouple" package with pipenv install python-decouple.

...

class AutoConfig(object):
    """
    Autodetects the config file and type.

    Parameters
    ----------
    search_path : str, optional
        Initial search path. If empty, the default search path is the
        caller's path.

    """
    ...

# A pré-instantiated AutoConfig to improve decouple's usability
# now just import config and start using with no configuration.
config = AutoConfig()

# Helpers
...

Wham answered 27/3, 2021 at 13:0 Comment(0)
D
0

After trying almost everything, installing the python-decouple for the whole system, as opposed to in an environment, worked for me. So left the virtual environment, installed python-decouple then activated back my environment again. It works for me.

Let me know if this helps.

Dicentra answered 1/8, 2021 at 10:47 Comment(0)
C
0

In my case, the issue was with the selection of interpreter. In Visual Studio Code, CMD+Shift+P and type: 'Select Interpreter', and then select the correct interpreter for the virtual environment.

Canso answered 13/11, 2023 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.