0

Why can't I import a file that I have in the same folder as the code I want to import? I am working in Visual Studio Code.

Here is my code:

import requests
import json
import webbrowser
import credentials
from pprint import pprint

r = requests.get("https://api.thecatapi.com/v1/favourites",
                 headers=credentials.headers)

try:
    content = r.json
except json.decoder.JSONDecodeError:
    print("Wrong format")
else:
    pprint(content)

This is the problem the tab pops up.

Import "credentials" could not be resolved

And this is the content of the imported file.

headers = {"x_api_key": "xxxxxxxxxxxxxxxxxx"}
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
SoSaymon
  • 11
  • 3
  • Can you post the *full* and *exact* error message? It usually has more information, like which line is raising the error. Because that doesn't look like the standard import errors from Python. And what is "*tab*" in "*the tab pops up*"? – Gino Mempin Feb 20 '21 at 01:51
  • 2
    Try `import .credentials`. In Python 3, imports are absolute by default, and you need to add '.' to them to make them relative (in this case, to the current dir). - I expect that you're using some sort of syntax checker, maybe because VSC has set that up. That error is coming from the checker, I. believe, rather than the Python interpreter. – CryptoFool Feb 20 '21 at 02:04
  • Does this answer your question? [How do I import from a file in the current directory in Python 3?](https://stackoverflow.com/questions/31269437/how-do-i-import-from-a-file-in-the-current-directory-in-python-3) – CryptoFool Feb 20 '21 at 02:06
  • 1
    Do you have a `__init__.py` file? – drum Feb 20 '21 at 02:11
  • All the above answers have to sense. When two files are in the same folder they can be imported without being any problem. The problem here is the name of the file. – Bhavyadeep Yadav Feb 20 '21 at 13:24
  • Mate I think you have written the wrong file name. Try changing the name and then importing. I used VS Code itself and this is the error which I can think of. – Bhavyadeep Yadav Feb 20 '21 at 13:26

1 Answers1

0

I'm guessing that this

This is the problem the tab pops up.

Import "credentials" could not be resolved

isn't an error coming from the Python interpreter, because it does not look like the standard "ModuleNotFoundError: No module named 'XXX'", which is what you get when you run your Python code and your imports are wrong or misconfigured.

Since you mentioned Visual Studio Code and a "tab", then I'm guessing that's coming from an integrated linter and it's displaying that error in the Problems tab:

enter image description here

If credentials is indeed in the same directory as that file, and you are running your code under the same directory, then

import credentials

should be fine. The problem is only with the linter, it does not know where to look for credentials. Depending on which linter you have enabled (see the Linting Python in Visual Studio Code section of the docs), you'll have to explicitly tell your linter where to find credentials.

For example, assuming you have a folder app that contains credentials and this script:

app
├── ...(other files)...
├── credentials.py
└── script.py

Add/update a .vscode/settings.json file on your workspace (see the General linting settings section of the VS Code docs). I'm using Pylance so I need to add a python.analysis.extraPaths list entry like this:

"python.analysis.extraPaths":[
    "/absolute/path/to/app"
]

to basically tell my linter that "when checking imports, also check this app folder". That clears the "could not be resolved" from the Problems tab.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135