Visual Studio Code pylint: Unable to import 'protorpc'
Asked Answered
C

21

86

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.

Chrysostom answered 23/4, 2017 at 18:38 Comment(1)
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.Firenew
A
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"
],
Acton answered 4/5, 2017 at 19:34 Comment(3)
do you need to replace ~ with your programfiles path or is it fine like this?Hassock
@JoelHarkes ~ expands to your $HOME directory. Take a look at tilde expansion.Flask
~ not working with Pylance. Use absolute.Backstairs
P
163

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

Pforzheim answered 24/6, 2018 at 13:45 Comment(4)
Don't know why but this worked for me. What a relief! ^.^Scleroderma
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.Franco
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 CodeStandin
Cmd + shift + P for macOSHillell
A
53

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

Affray answered 14/8, 2018 at 5:31 Comment(0)
A
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"
],
Acton answered 4/5, 2017 at 19:34 Comment(3)
do you need to replace ~ with your programfiles path or is it fine like this?Hassock
@JoelHarkes ~ expands to your $HOME directory. Take a look at tilde expansion.Flask
~ not working with Pylance. Use absolute.Backstairs
O
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.

Opportunism answered 30/4, 2019 at 6:58 Comment(3)
TKS. I have many path to include. This one solve my question.Bravo
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.Boehmenism
I tried it all and this is the only fix that worked for me.Conservationist
M
14

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!

Marolda answered 11/7, 2020 at 11:26 Comment(1)
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.Trichinopoly
B
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

Beatup answered 10/7, 2018 at 8:6 Comment(1)
The python.pythonPath setting is no longer used by the Python extension. See github.com/microsoft/vscode-python/wiki/…Farthest
A
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.

Avifauna answered 21/3, 2019 at 15:29 Comment(1)
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.Biathlon
W
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

Woozy answered 12/9, 2021 at 22:17 Comment(0)
C
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.

Contemporaneous answered 15/7, 2018 at 0:26 Comment(1)
The python.pythonPath setting is no longer used by the Python extension. See github.com/microsoft/vscode-python/wiki/…Farthest
M
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"
Mossy answered 21/7, 2018 at 9:50 Comment(1)
The python.pythonPath setting is no longer used by the Python extension. See github.com/microsoft/vscode-python/wiki/…Farthest
C
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
Chrysostom answered 26/6, 2017 at 16:33 Comment(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 luckNorwegian
G
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

Gormley answered 2/5, 2020 at 11:21 Comment(0)
D
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")
Demission answered 1/3, 2021 at 21:30 Comment(1)
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 > .pylintrcOvercrop
A
1

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

Anticosti answered 11/7, 2021 at 19:29 Comment(0)
O
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.

Oquinn answered 21/2, 2020 at 23:11 Comment(0)
C
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.

Crayfish answered 14/11, 2020 at 15:22 Comment(1)
The python.pythonPath setting is no longer used by the Python extension. See github.com/microsoft/vscode-python/wiki/…Farthest
B
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.

Breughel answered 3/12, 2020 at 8:40 Comment(0)
B
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.

Backstairs answered 19/3, 2021 at 7:18 Comment(0)
N
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.

Notation answered 2/12, 2021 at 17:23 Comment(0)
C
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"
    },
Chiquia answered 10/2, 2023 at 10:33 Comment(0)
P
0

I had configured the correct interpreter, but I just needed to do Ctrl + Shift + P > Developer: Reload Window.

Periphrastic answered 4/6 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.