Fix Python Unresolved Import in VSCode

Python is a computer programming language that is easy to learn and use. It is one of the most popular programming languages out there. In this digital age, where everyone is looking for ways to automate their business, Python is on the rise.

One of the many Python error that confuses beginners is Unresolved Import, which happens when the system cannot detect the Python module. In this article, we will show you a few ways to fix Unresolved Import in VSCode and avoid encountering it in the future.

Unresolved Import in VSCode

"Unresolved Import" is an error message produced by VSCode, not Python itself. The message simply means that VSCode cannot detect the correct path for a Python module. The cause of "Unresolved Import" could be one of the following reason:

  • VSCode is using the wrong Python path. This is often the case if you’re running your code against a virtual environment. Each environment contains its own binary path that contains different set of package binaries, so a package in one specific virtual environment cannot be found by another and vice versa.
  • There might be a missing .env file at the root directory of your project that contains additional information about which directory to import modules from. Maybe you were opening VScode in the project’s root directory, however, your module lies in a nested sub directory (usually the src directory). By default, VSCode won’t look for modules in any other directory except it was added to PYTHONPATH environment variable, which can be set at runtime by placing an additional .env file at the root directory. However, this file is often ignored when people push their projects to public Github repository, as it may contain sensitive information.

Unresolved Import

Set the correct Python path in VSCode

In order to fix Unresolved Import in VSCode, you have to set python.pythonPath key in the settings to the correct value. You can quickly open the settings.json editor by accessing File > Preferences or press Ctrl + , key combination. Alternatively, open Command Palette and find Open Settings (JSON) to open settings.json. In your workspace settings, you can set your Python path like the following.

    "python.pythonPath": "/path/to/your/virtualenvironment/bin/python",

Remember to replace /path/to/your/virtualenvironment/bin/python with the correct value. If you’re in a virtual terminal window, you can run which python to quickly get the path of the current Python interpreter. Once you’re done, reload VSCode, and the error message will go away.

In recent versions of VSCode, there is an alternative way to quickly set the pythonPath variable using the command interface.


Press Ctrl + Shift + P keyboard combination, then select Python: Select Interpreter, choose the proper one with the packages you need installed, and the problem will be fixed.


Using .env file

You can also create a .env file in your project root folder to quickly add the proper path to PYTHONPATH environment variable at runtime. Given the example project structure below:

  • my_project
    • .vscode
    • … other folders
    • my_code

What you need to do to fix "Unresolved Import" is following these steps

  1. Create an .env file in the workspace folder (here my_project)
  2. In this newly-created, empty .env file, add the line PYTHONPATH=/path/to/module (replace /path/to/module with the actual module you’re trying to import). In this case, we have to replace /path/to/module with my_code to make Python import module from my_code
  3. Then, to make sure that VSCode recognizes the .env file, place an additional "python.envFile": "${my_project}/.env" line to the settings.json, similar to how we did in the previous section of this article.
  4. Restart VSCode and verify that the "Unresolved Import" now disappeared.

We hope that the information above is useful to you. If you’re interested in more advanced editing features of VSCode, check out our post on how to enable/disable word wrap in VSCode, How to use LaTeX in VSCode or how to automatically indent your code in Visual Studio Code.

Leave a Comment