Python Visual Studio Code pylint:无法导入“protorpc”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43574995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:12:00  来源:igfitidea点击:

Visual Studio Code pylint: Unable to import 'protorpc'

pythonvisual-studiopython-2.7google-app-engineprotorpc

提问by Hyman

I'm using pylintin Visual Studio Codeto 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, pylintcannot find the protorpclibrary?

我在Visual Studio Code 中使用pylintPython中开发 Google App Engine (GAE) Cloud Endpoint API。我无法解决 lint 错误。我不知道是什么导致了错误,但猜测是pylint找不到库?protorpc

enter image description here

在此处输入图片说明

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

故障排除 Linting 中的推荐修复是配置工作区设置以指向完全限定的 python 可执行文件。我已经这样做了,但 lint 错误仍然存​​在。

enter image description here

在此处输入图片说明

protorpcitself is installed to:

protorpc本身安装到:

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

...and this contains the remote.pymodule that cannot be imported:

...这包含remote.py无法导入的模块:

__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):

我已将此路径添加到$PYTHONPATH(以及厨房水槽):

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.

该应用程序在本地运行,也在部署时运行,所以这似乎只是一个 lint 错误,但令人沮丧的是我无法解决它。

Using third-party librariesstates:

使用第三方库说明:

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

标准环境中的 Python 运行时包括 Python 标准库、App Engine 库和一些捆绑的第三方包。

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 APIonly requires google-endpoints be installed to the app's lib directory:

因此,我假设“App Engine 库”包含protorpc,但我不确定。此外,将 Cloud Endpoints Frameworks 库添加到示例 API只需要将google-endpoints 安装到应用程序的 lib 目录中:

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.

我的观点是,我认为我没有安装任何东西,而且我认为我的(网络)应用程序的 lib 目录中没有丢失任何东西。

采纳答案by Morad

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

打开 Visual Studio Code ( settings.json)的设置文件并将库路径添加到"python.autoComplete.extraPaths"列表中。

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

回答by Ivan_ug

Changing the library path worked for me. Hitting Ctrl + Shift + Pand typing python interpreterand 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

更改库路径对我有用。点击Ctrl + Shift + P并输入python interpreter并选择可用的显示之一。一个是熟悉的(正如前面提到的一个运行良好的 virtualenv)并且它起作用了。记下您正在使用的 python 版本,2.7 或 3.x 并相应地选择

回答by SurajKj

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

我遇到了同样的问题(VS Code)。通过以下方法解决

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

1) 从命令面板中选择解释器命令 (Ctrl+Shift+P)

2) Search for "Select Interpreter"

2) 搜索“选择口译员”

3) Select the installed python directory

3)选择安装的python目录

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

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

回答by wizawu

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

对于您的情况,请将以下代码添加到 vscode 的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

对于其他在 pip 包方面遇到麻烦的人,您可以使用

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

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

你应该python3.7用你的python版本替换上面的。

回答by PapaKai

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.

我还没有尝试过所有的可能性,但至少我的印象是这可能是与 python 版本相关的问题。不知道为什么,我只是相信自己的直觉。

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

因此,我只是将 pythonPath 更改为 python3(默认值:python):

"python.pythonPath": "python3"

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

我重新安装了依赖项(包括 pylint !!!)

pip3 install <package> --user

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

...在重新启动 vs 代码后,一切看起来都很好。

HTH Kai

HTH凯

回答by jrc

I was still getting these errors even after confirming that the correct pythonand pylintwere being used from my virtual env.

即使在确认正确python并且pylint正在从我的虚拟环境中使用后,我仍然收到这些错误。

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.

最终我发现在 Visual Studio Code 中我是 A) 打开我的项目目录,这是 B) 我的 Python 虚拟环境所在的位置,但我是 C) 从更深的两个级别运行我的主要 Python 程序。这三件事需要同步才能正常工作。

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/activatethen 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.pythonPathin Settings.json.

  1. 在 Visual Studio Code 中,打开包含主要 Python 程序的目录。(这可能是也可能不是项目目录的顶层。)

  2. 选择终端菜单 > 新建终端,直接在同一个目录下创建一个虚拟环境。

    python3 -m venv env
    
  3. 在虚拟环境中安装pylint。如果您在侧栏中选择任何 Python 文件,Visual Studio Code 将提供为您执行此操作。或者,source env/bin/activate那么pip install pylint

  4. 在编辑器窗口的蓝色底栏中,选择 Python 解释器env/bin/python。或者,转到“设置”并设置“Python:Python 路径”。这python.pythonPath在 Settings.json 中设置。

回答by Harry

First I will check the python3path where it lives

首先我会检查python3它所在的路径

enter image description here

在此处输入图片说明

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

然后在 VS Code 设置中添加该路径,例如:

"python.pythonPath": "/usr/local/bin/python3"

回答by Qing Yuan

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

Visual Studio 默认设置应与解释器路径相同。

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

更改 VS 代码默认设置:windows:文件 > 首选项 > 设置

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

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

找到正确的解释器: windows: Ctrl+ Shift+ P-> 选择解释器:

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

该解释器的路径应该与您正在处理的版本相同。

回答by Hyman

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

我通过将protorpc库添加到$PYTHONPATH环境变量中解决了这个问题。具体来说,我指向了安装在我的App Engine 目录中的库:

export PYTHONPATH=$PYTHONPATH:/Users/Hymanwootton/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.

将其添加到 后~/.bash_profile,重新启动我的机器和 Visual Studio Code,导入错误消失了。

For completeness, I did notmodify any Visual Studio Code settings relating to Python. Full ~/.bash_profilefile:

为了完整起见,我并没有修改有关Python的任何Visual Studio代码设置。完整~/.bash_profile文件:

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

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

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

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

# The next line enables shell command completion for gcloud.
if [ -f '/Users/Hymanwootton/google-cloud-sdk/completion.bash.inc' ]; then source '/Users/Hymanwootton/google-cloud-sdk/completion.bash.inc'; fi

回答by Foad

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:

我在我的 vscode 上遇到了同样的错误,我安装了一个库并且代码在从终端运行时工作,但由于某种原因,vscode pylint 无法选择已安装的包,返回臭名昭著的错误:

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

无法导入“someLibrary.someModule”pylint(导入错误)

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 whichcommand, and if you are on Windows, use the wherecommand to find the installed Python interpreters. For example, on *nix machines:

由于多个 Python 安装,可能会出现该问题。基本上,您已经在其中安装了一个库/包,并且 vscode pylint 已安装并从另一个安装运行。例如,在 macOS 和许多 Linux 发行版上,默认情况下安装了 Python2,当您安装 Python3 时,这可能会导致混淆。同样在 Windows 上,Chocolatey 包管理器可能会造成一些混乱,你最终会安装多个 Python。要确定您是否使用 *nix 机器(即 macOS、GNU/Linux、BSD...),请使用该which命令,如果您使用的是 Windows,请使用该where命令查找已安装的 Python 解释器。例如,在 *nix 机器上:

which python3

and on Windows

在 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

pip install someLibrary

then on vscode press ??Pif you are on a mac and CtrlShiftPon other operating systems. Then type-select the >python: Select Interpreteroption 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.

??P如果您使用的是 mac 和CtrlShiftP其他操作系统,请在 vscode 上按下。然后键入>python: Select Interpreter选项并选择您知道已安装库的选项。此时 vscode 可能会要求您再次安装 pyling,您只需继续安装即可。