trs-k3tchup
trs-k3tchup

Reputation: 299

import "dotenv" could not be resolved

I am making a program in python that I plan to host on github. I have a .env file containing an api token. I tried to import it into my code like so:

first i installed the python-dotenv library by typing pip install python-dotenv in the command prompt. python-dotenv shows when i type pip list.

then in my code:

import os
from dotenv import load_dotenv

load_dotenv()

example = os.getenv('TOKEN')

from dotenv import load_dotenv gives the error Import "dotenv" could not be resolved Pylancereport (MissingImports) and my code will not run. Is there anything i'm doing wrong? How can i fix it?

Upvotes: 18

Views: 59916

Answers (5)

Kene-dili-chukwu
Kene-dili-chukwu

Reputation: 11

Here is how I solved this problem:

  • Open the command palette.
  • Type and select Python: Select Interpreter.
  • Choose the interpreter located in your virtual environment (it should show a path like myproject/myVenv/bin/python or myproject\myVenv\Scripts\python.exe).

Upvotes: 1

hamid
hamid

Reputation: 609

I faced the same issue and after little bit of researching I found that the issue was in using the system python interpreter instead of python interpreter inside the virtual environment which I have created. Since I was using VSCode editor. Therefore, following steps resolved the issue.

Step1: Hit Ctrl+P and type >Python: Select Interpreter click the 'Python: Select Interpreter' which appear in the search bar. enter image description here

Step2:Click the Enter interpreter path enter image description here

Step3: Now browse your system and select the python interpreter inside your virtual enviroment folder venvFoderName/Scripts/Python

Upvotes: 10

Codingkido
Codingkido

Reputation: 73

If you look at the dotenv library folder, load_dotenv() is in the main.py file.

To properly import, you should do

from dotenv.main import load_dotenv
load_dotenv()

will work.

Upvotes: 1

Sergei Ivanitsa
Sergei Ivanitsa

Reputation: 85

The only thing that helped me was the complete removal of the virtual environment, creating a new one and installing requirements.txt (of course with python-dotenv inside)

Upvotes: 6

Jorge Alvarado
Jorge Alvarado

Reputation: 520

It seems you have installed python-env, when you really wanted to install python-dotenv. The former doesn't have the function you are trying to use on it's __init__.py file, that's why Pylancereport can't resolve it.

Solution: Do a pip install python-dotenv. Execute your code again and it should work.

Upvotes: 22

Related Questions