Skip to content

Instantly share code, notes, and snippets.

@kaelzhang
Last active November 24, 2020 12:54
Show Gist options
  • Save kaelzhang/6abf92f06a42fdc902be241cb54b6086 to your computer and use it in GitHub Desktop.
Save kaelzhang/6abf92f06a42fdc902be241cb54b6086 to your computer and use it in GitHub Desktop.
vscode (pythonVSCode): how to fix the issue that unable to import 'somemodule'

There is a common problem we often encounter when using vscode.

[pylint] E0401:Unable to import 'tensorflow'
  • First make sure that both the python.pythonPath and python.linting.pylintPath are configured correctly in the WORKSPACE SETTINGS, especilly for the cases when you use anaconda or virtualenv
{
	// We can run `type python` to get the python path after activating the virtual env
	// Notice that we should set the pythonPath to the bin location of python, but NOT the path of site-packages.
	"python.pythonPath": "/Users/kael/anaconda2/envs/tensor/bin/python",
	"python.linting.pylintPath": "/Users/kael/anaconda2/bin/pylint"
}
  • Then, for most situtations, you need to restart the project by opening the .code-workspace file.
  • Choose the python environment for vscode. Check this article first: https://code.visualstudio.com/docs/python/environments#_choosing-an-environment. Or you can select the python tab of the status bar at the bottom of vscode, and select "/Users/kael/anaconda2/envs/tensor/bin/python" (according to the example above, you might have to choose your own env.)
@kaelzhang
Copy link
Author

kaelzhang commented Nov 24, 2020

@dannystaple

This is a very old gist though.

For now, selecting the python tab of the status bar at bottom left of vscode, and choosing the specific python env version provided anaconda3 always works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment