Visual Studio Code pylint: Unable to import 'protorpc'

165,345

Solution 1

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"
],

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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!

Share:
165,345

Related videos on Youtube

Jack
Author by

Jack

Updated on July 12, 2022

Comments

  • Jack
    Jack almost 2 years

    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.

    • Jordan
      Jordan almost 7 years
      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 in to properly install and use them.
  • Joel Harkes
    Joel Harkes almost 7 years
    do you need to replace ~ with your programfiles path or is it fine like this?
  • ankush981
    ankush981 almost 6 years
    Don't know why but this worked for me. What a relief! ^.^
  • Casimir
    Casimir over 5 years
    @JoelHarkes ~ expands to your $HOME directory. Take a look at tilde expansion.
  • thomthom
    thomthom over 5 years
    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.
  • Tony Chou
    Tony Chou almost 5 years
    TKS. I have many path to include. This one solve my question.
  • jpenna
    jpenna almost 5 years
    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
  • johan
    johan over 4 years
    Cmd + shift + P for macOS
  • Cruncher
    Cruncher over 4 years
    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
  • F1iX
    F1iX about 4 years
    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-package‌​s")' to ~/.pylintrc finally did the trick.
  • addicted
    addicted about 4 years
    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.
  • Joshua Schlichting
    Joshua Schlichting almost 4 years
    I tried it all and this is the only fix that worked for me.
  • Sole Sensei
    Sole Sensei about 3 years
    ~ not working with Pylance. Use absolute.
  • Vladimir Fomenko
    Vladimir Fomenko about 3 years
    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
  • Ian
    Ian about 2 years
    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.