Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved import 'util'

Extensive searching on Google, Reddit, and this site was fruitless, so hopefully someone here can help me out. The code

import util

gives the error unresolved import 'util'. util.py is a module in the same folder. For reference, here is my hierarchy:

\Projects
    |-\adventure
        |-\src
            |-__init__.py
            |-main.py
            |-util.py

and this is my VSCode's info:

Version: 1.32.2 (user setup)
Commit: e64cb27b1a0cbbc3f643c9fc6c7d93d6c6509951
Date: 2019-03-13T02:00:46.035Z
Electron: 3.1.6
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Windows_NT x64 10.0.17134

Any help would be greatly appreciated.

like image 305
DCoded Avatar asked Dec 20 '25 21:12

DCoded


1 Answers

What I believe is happening is that VSCode is running the main.py from a current working directory that is not /Projects or any of its sub directories. So when you try to import util or from adventure import util, it cannot find the file. A quick and easy fix would be to append the path to /src to the sys.path:

import sys
sys.path.append('/Projects/adventure/src')
import util

or whatever the absolute path to /src may be. A bit cleaner might be to append your /Projects directory, and then import from adventure, so that you don't have to append extra directories if you want to include modules from other packages in your Projects folder.

import sys
sys.path.append('/Projects') # whatever the absolute path to /Projects is
from adventure import util

Another possibility involves updating the VSCode configuration file to specify a modified PYTHONPATH environment variable, to specify the path to import the project from. You can either include the following in the launch.json under your configuration:

"env": {
    "PYTHONPATH": "/path/to/src/:${PYTHONPATH}"
}

or create an .env file to specify the same:

PYTHONPATH=/path/to/src/:${PYTHONPATH}

These were obtained from the visualstudio docs. Note that you may have to change : to ; and / to \\ on windows.

like image 106
Dillon Davis Avatar answered Dec 23 '25 09:12

Dillon Davis