12

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?

trs-k3tchup
  • 239
  • 1
  • 2
  • 13
  • is it vscode issue? the check https://www.codegrepper.com/code-examples/python/vscode+python+import+could+not+be+resolved – Mahamudul Hasan Jan 13 '22 at 02:27
  • I think that the problem is that `load_dotenv` can't be imported from `dotenv` because it doesn't exist on the package `__init__.py` file. I replicated your steps and got the following error: `ImportError: cannot import name 'load_dotenv' from 'dotenv` (I used vim). – Jorge Alvarado Jan 13 '22 at 02:29
  • 1
    Oh I think that I have found the problem: you installed `python-env` instead of `python-dotenv`. Do a `pip install python-dotenv`. Execute your code again and it should work. – Jorge Alvarado Jan 13 '22 at 02:33
  • This may help: https://stackoverflow.com/questions/68486207/import-could-not-be-resolved-could-not-be-resolved-from-source-pylance-in-vs-cod – ingernet Jan 13 '22 at 02:34
  • 1
    i think it is a vscode issue. i'm looking into it now – trs-k3tchup Jan 13 '22 at 02:37
  • i did install the correct library, it was a post typo sorry – trs-k3tchup Jan 13 '22 at 02:39

2 Answers2

11

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.

Jorge Alvarado
  • 372
  • 2
  • 12
3

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)