84

I'm using pylint in Visual Studio Code to develop a Google App Engine (GAE) Cloud Endpoint API in Python. I'm unable to resolve a lint error. I don't know what's causing the error, but at a guess, pylint cannot find the protorpc library?

enter image description here

The recommended fix in Troubleshooting Linting is to configure workspace settings to point to fully qualified python executable. I have done this, but the lint error remains.

enter image description here

protorpc itself is installed to:

~/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0/protorpc

...and this contains the remote.py module that cannot be imported:

__init__.py             generate_python.py      protojson.py            transport.py
definition.py           google_imports.py       protourlencode.py       util.py
descriptor.py           message_types.py        registry.py             webapp
generate.py             messages.py             remote.py               wsgi
generate_proto.py       protobuf.py             static

I've added this path to $PYTHONPATH (along with the kitchen sink):

export GOOGLE_CLOUD_SDK=~/google-cloud-sdk
export APPENGINE_PATH=$GOOGLE_CLOUD_SDK/platform/google_appengine

export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib/googlecloudsdk
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib/googlecloudsdk/api_lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/platform/google_appengine/lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/platform/google_appengine/lib/protorpc-1.0/protorpc

The application runs locally and also when deployed, so this appears to be just a lint error, but it's frustrating that I can't solve it.

Using third-party libraries states:

The Python runtime in the standard environment includes the Python standard library, the App Engine libraries, and a few bundled third-party packages.

Because of this, I assumed 'the App Engine libraries' includes protorpc, but I'm unsure. Moreover, Adding the Cloud Endpoints Frameworks library to the sample API only requires google-endpoints be installed to the app's lib directory:

pip install -t lib google-endpoints --extra-index-url=https://gapi-pypi.appspot.com/admin/nurpc-dev --ignore-installed

My point is, I don't think I've not installed something, and I don't think I'm missing anything in my (web) app's lib directory.

Peter Macej
  • 4,831
  • 22
  • 49
Jack
  • 10,313
  • 15
  • 75
  • 118
  • If Morad's post was the answer it is recommended to mark it as the solution to better help the community. If you have solved the issue, you can also post your own answer and mark it as the solution. Note, when using third-party libs in App Engine, you must [vendor them](https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#installing_a_third-party_library) in to properly install and use them. – Jordan Jun 22 '17 at 15:51

20 Answers20

158

Changing the library path worked for me. Hitting Ctrl + Shift + P and typing python interpreter and choosing one of the available shown. One was familiar (as pointed to a virtualenv that was working fine earlier) and it worked. Take note of the version of python you are working with, either 2.7 or 3.x and choose accordingly

Ivan_ug
  • 2,467
  • 2
  • 17
  • 18
  • 3
    Don't know why but this worked for me. What a relief! ^.^ – ankush981 Jul 25 '18 at 09:56
  • 2
    I had the same issue. It appear the VSCode linter extension used the python interpreter installed by Visual Studio - instead of the one I'd installed myself. – thomthom Feb 04 '19 at 14:08
  • 2
    Choose the Python intepreter used in your virtual environment. If you are using `pipenv`, run `pipenv --venv` to find the folder of your environment. Then set the interpreter path to `{folder you found}/bin/python`. You may have to restart VS Code – jpenna Aug 03 '19 at 00:34
  • Cmd + shift + P for macOS – johan Aug 10 '19 at 14:47
52

I was facing same issue (VS Code).Resolved by below method

1) Select Interpreter command from the Command Palette (Ctrl+Shift+P)

2) Search for "Select Interpreter"

3) Select the installed python directory

Ref:- https://code.visualstudio.com/docs/python/environments#_select-an-environment

SurajKj
  • 706
  • 7
  • 13
38

Open the settings file of your Visual Studio Code (settings.json) and add the library path to the "python.autoComplete.extraPaths" list.

"python.autoComplete.extraPaths": [
    "~/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2",
    "~/google-cloud-sdk/platform/google_appengine",
    "~/google-cloud-sdk/lib",
    "~/google-cloud-sdk/platform/google_appengine/lib/endpoints-1.0",
    "~/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0"
],
Morad
  • 2,761
  • 21
  • 29
18

For your case, add the following code to vscode's settings.json.

"python.linting.pylintArgs": [
    "--init-hook='import sys; sys.path.append(\"~/google-cloud-sdk/platform/google_appengine/lib\")'"
]

For the other who got troubles with pip packages, you can go with

"python.linting.pylintArgs": [
    "--init-hook='import sys; sys.path.append(\"/usr/local/lib/python3.7/dist-packages\")'"
]

You should replace python3.7 above with your python version.

wizawu
  • 1,881
  • 21
  • 33
  • TKS. I have many path to include. This one solve my question. – Tony Chou Jul 04 '19 at 01:45
  • 1
    I experienced the mentioned linting error in ROS development despite using the VSCode ROS extension and thus having `settings.json` configured with `"python.autoComplete.extraPaths": ["/opt/ros/melodic/lib/python2.7/dist-packages"]`. Unfortunately your suggestion did not work, but adding `[MASTER] init-hook='import sys; sys.path.append("/opt/ros/melodic/lib/python2.7/dist-packages")'` to `~/.pylintrc` finally did the trick. – F1iX Feb 28 '20 at 14:28
  • 1
    I tried it *all* and this is the only fix that worked for me. – Joshua Schlichting Jul 24 '20 at 13:16
13

Spent hours trying to fix the error for importing local modules. Code execution was fine but pylint showed:

    Unable to import '<module>'

Finally figured:

  1. First of all, select the correct python path. (In the case of a virtual environment, it will be venv/bin/python). You can do this by hitting

  2. Make sure that your pylint path is the same as the python path you chose in step 1. (You can open VS Code from within the activated venv from terminal so it automatically performs these two steps)

  3. The most important step: Add an empty __init__.py file in the folder that contains your module file. Although python3 does not require this file for importing modules, I think pylint still requires it for linting.

Restart VS Code, the errors should be gone!

S_M
  • 131
  • 1
  • 2
  • This solution worked for me. I had issues with pylint even though the correct interpreter jad been set. I had installed pylint with pipx. I fixed this by making sure the pylint path is in the same environment as the project. – Ian Andwati May 02 '22 at 20:33
5

I've not played around with all possibilities, but at least I had the impression that this could be a python version related issue. No idea why, I just trusted my gut.

Thus I just changed the pythonPath to python3 (default: python):

"python.pythonPath": "python3"

I reinstalled the dependencies (including pylint!!!) with

pip3 install <package> --user

... and after restarting vs code, everything looked fine.

HTH Kai

PapaKai
  • 434
  • 1
  • 6
  • 7
  • 1
    The `python.pythonPath` setting is no longer used by the Python extension. See https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath – BradT Jan 06 '23 at 20:54
5

I was still getting these errors even after confirming that the correct python and pylint were being used from my virtual env.

Eventually I figured out that in Visual Studio Code I was A) opening my project directory, which is B) where my Python virtual environment was, but I was C) running my main Python program from two levels deeper. Those three things need to be in sync for everything to work.

Here's what I would recommend:

  1. In Visual Studio Code, open the directory containing your main Python program. (This may or may not be the top level of the project directory.)

  2. Select Terminal menu > New Terminal, and create an virtual environment directly inside the same directory.

    python3 -m venv env
    
  3. Install pylint in the virtual environment. If you select any Python file in the sidebar, Visual Studio Code will offer to do this for you. Alternatively, source env/bin/activate then pip install pylint.

  4. In the blue bottom bar of the editor window, choose the Python interpreter env/bin/python. Alternatively, go to Settings and set "Python: Python Path." This sets python.pythonPath in Settings.json.

jrc
  • 20,354
  • 10
  • 69
  • 64
  • Just wanna upvote this comment. Step number 4 actually works for my case. Turns out I need to select the correct `python` interpreter from my virtualenv directory in the blue menu bar at the bottom. – addicted Apr 10 '20 at 08:58
5

The simplest solution is to create a .env file in your project root with this content:

PYTHONPATH=.

You don't need __init__.py files. It works even if your code is in src dir, and unit tests in tests subdirs. This helped pylint and pytest to find all the modules.

For more info, see https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file

wisbucky
  • 33,218
  • 10
  • 150
  • 101
3

The visual studio default setting should be the same as the interpreter path.

Change VS code default setting: windows: File > Preferences > Settings

{
    "python.pythonPath": "C:\\Users\\Anaconda3\\pythonw.exe",
    "workbench.startupEditor": "newUntitledFile"
}

Find the right interpreter: windows: Ctrl+Shift+P->select interpreter:

the path of that interpreter should be same as the version you are working on.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Qing Yuan
  • 67
  • 2
  • The `python.pythonPath` setting is no longer used by the Python extension. See https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath – BradT Jan 06 '23 at 20:53
3

First I will check the python3 path where it lives

enter image description here

And then in the VS Code settings just add that path, for example:

"python.pythonPath": "/usr/local/bin/python3"
Harry
  • 951
  • 2
  • 12
  • 18
  • The `python.pythonPath` setting is no longer used by the Python extension. See https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath – BradT Jan 06 '23 at 21:14
1

I resolved this by adding the protorpc library to the $PYTHONPATH environment variable. Specifically, I pointed to the library installed in my App Engine directory:

export PYTHONPATH=$PYTHONPATH:/Users/jackwootton/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0

After adding this to ~/.bash_profile, restarting my machine and Visual Studio Code, the import errors went away.

For completeness, I did not modify any Visual Studio Code settings relating to Python. Full ~/.bash_profile file:

export PATH=/Users/jackwootton/protoc3/bin:$PATH

export PYTHONPATH=/Users/jackwootton/google-cloud-sdk/platform/google_appengine

export PYTHONPATH=$PYTHONPATH:/Users/jackwootton/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0

# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/jackwootton/google-cloud-sdk/path.bash.inc' ]; then source '/Users/jackwootton/google-cloud-sdk/path.bash.inc'; fi

# The next line enables shell command completion for gcloud.
if [ -f '/Users/jackwootton/google-cloud-sdk/completion.bash.inc' ]; then source '/Users/jackwootton/google-cloud-sdk/completion.bash.inc'; fi
Jack
  • 10,313
  • 15
  • 75
  • 118
  • 1
    This solution is very flaky as it requires modifying a global variable for this particular project. I'm trying to set PYTHONPATH only in the context of the vscode workspace, but without luck – Cruncher Feb 03 '20 at 22:37
1

I find the solutions stated above very useful. Especially the Python's Virtual Environment explanation by jrc.

In my case, I was using Docker and was editing 'local' files (not direcly inside the docker). So I installed Remote Development extension by Microsoft.

ext install ms-vscode-remote.vscode-remote-extensionpack

More details can be found at https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack

I should say, it was not easy to play around at first. What worked for me was...
1. starting docker
2. In vscode, Remote-container: Attach to running container
3. Adding folder /code/<path-to-code-folder> from root of the machine to vscode

and then installing python extension + pylint

Ahmed Hafiz
  • 67
  • 1
  • 9
1

Another solution could be generating a pylintrc in project location using command: (this command will by default print the content to terminal and not actually create a file, you need to dump output to .pylintrc)

pylint --generate-rcfile

and then update key init-hook= using:

import sys; sys.path.append("your_project_location")
Hamza Mogni
  • 672
  • 1
  • 7
  • 21
  • The only thing that finally worked for me. Though the first command printed the config to the terminal, so I would add one has to dump it to `.pylintrc`: `pylint --generate-rcfile > .pylintrc` – Vladimir Fomenko Apr 27 '21 at 16:12
1

In my case, I tried flake8, bandit, didn't work, eventually I uninstalled the extension called python (pylance) and everything worked perfectly.

Georges D
  • 351
  • 3
  • 11
0

I got the same error on my vscode where I had a library installed and the code working when running from the terminal, but for some reason, the vscode pylint was not able to pick the installed package returning the infamous error:

Unable to import 'someLibrary.someModule' pylint(import-error)

The problem might arise due to the multiple Python installations. Basically you have installed a library/package on one, and vscode pylint is installed and running from another installation. For example, on macOS and many Linux distros, there are by default Python2 installed and when you install Python3 this might cause confusion. Also on windows the Chocolatey package manager might cause some mess and you end up with multiple Python installations. To figure it out if you are on a *nix machine (i.e., macOS, GNU/Linux, BSD...), use the which command, and if you are on Windows, use the where command to find the installed Python interpreters. For example, on *nix machines:

which python3

and on Windows

where python

then you may want to uninstall the ones you don't want. and the one you want to use check if the package causing above issue is installed by

python -c "import someLibrary"

if you get an error then you should install it by for example pip:

pip install someLibrary

then on vscode press P if you are on a mac and CtrlShiftP on other operating systems. Then type-select the >python: Select Interpreter option and select the one you know have the library installed. At this moment vscode might asks you to install pyling again, which you just go on with.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
0

I resolve this error by below step :

1 : first of all write this code in terminal :

...$ which python3
/usr/bin/python3

2 : Then :

"python.pythonPath": "/users/bin/python",

done.

Laxifan
  • 11
  • 1
  • 4
  • 1
    The `python.pythonPath` setting is no longer used by the Python extension. See https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath – BradT Jan 06 '23 at 20:52
0

I had same problem for pyodbc , I had two version of python on my Ubuntu (python3.8 and python3.9), problem was: package installed on python3.8 location but my interpreter was for python3.9. i installed python3.8 interpreter in command palette and it fixed.

NEBEZ
  • 702
  • 8
  • 19
0

Other solutions not working for me (multiple workspaces)

Create ~/.vscode/workspace.env with following content

PYTHONPATH=$PYTHONPATH:/Users/jackwootton/protoc3/bin

Go to Workspace Settings: ⌘ / CtrlPWorkspace Settings.

Add next config line there:

"python.envFile": "/Users/jackwootton/.vscode/workspace.env",

Reload Window.


This solution better than @Jack, because it doesn't modify a global variable for all system, just for your project workspace.

Sole Sensei
  • 369
  • 3
  • 8
0

In my case, the packages got installed in the global Python installation path and not in the venv, even though I had the virtual environment activated at the time of installation.

I could see this when I switched to the base environment (by a click on the bottom left status field of the chosen interpreter) and saw that the package could be imported.

I could only solve this by removing the venv and installing it again.

questionto42
  • 7,175
  • 4
  • 57
  • 90
0

pylint is run in the vscode terminal, and its output transferred to the editor. Therefore the terminal environment settings need to match those in the command line outside vscode. Specifically, in settings.json:

"terminal.integrated.env.linux": {
        "PYTHONPATH": "/path/to/library/files/you/want/pylint/to/FINALLY/EXPLETIVE/FIND"
    },
Dave Baird
  • 143
  • 7