简体   繁体   English

在 Pylance 中无法解析导入“布朗尼”

[英]Import "brownie" could not be resolved in Pylance

Error is: Import "brownie" could not be resolvedPylance错误是: Import "brownie" could not be resolvedPylance

I know there are other SO posts that refer to this, but it seems most of them are talking about booting up a new env and installing x package into that virtual env.我知道还有其他 SO 帖子提到了这一点,但似乎大多数人都在谈论启动一个新的环境并将 x package 安装到该虚拟环境中。

However with Brownie, I'm especially confused because the brownie docs say:但是对于布朗尼,我特别困惑,因为布朗尼文档说:

pipx installs Brownie into a virtual environment and makes it available directly from the commandline. pipx 将 Brownie 安装到虚拟环境中,并使其直接从命令行可用。 Once installed, you will never have to activate a virtual environment prior to using Brownie.安装后,您将永远不必在使用 Brownie 之前激活虚拟环境。

I don't want to mess with the virtual env that brownie uses.我不想弄乱布朗尼使用的虚拟环境。

Anyways, my code runs fine and the command line tells me that brownie is installed.It's just that this warning is really annoying me.无论如何,我的代码运行良好,命令行告诉我布朗尼已安装。只是这个警告让我很烦。 Can anyone tell me how to clear it up?谁能告诉我如何清除它? Thanks!谢谢!

It's happening because we install python with pipx instead of pip .发生这种情况是因为我们使用pipx而不是 pip 安装pip pylance looks in the location our pip files are generally stored, and doesn't see brownie since we installed with pipx (which installed to it's on isolated virtual environment). pylance在我们的pip文件通常存储的位置中查找,并且由于我们使用pipx安装(它安装在隔离的虚拟环境中),所以看不到 brownie。 So you have a few options:所以你有几个选择:

  1. Ignore it忽略它
  2. Install brownie with pip in a virtual environment (not recommended)在虚拟环境中安装带有pip的 brownie(不推荐)

If there is another suggestion, happy to hear it如果有其他建议,很高兴听到

  1. open command pallete cmd+shift+P (on mac)打开命令pallete cmd+shift+P(在mac上)
  2. type python select型号 python select
  3. click Python: Select Interpreter点击 Python:Select 解释器
    • Enter interpreter path ~/.local/pipx/venvs/eth-brownie/bin/python输入解释器路径 ~/.local/pipx/venvs/eth-brownie/bin/python
  4. this works for me.这对我有用。

for Windows:对于 Windows:

  1. Ctrl+Shift+P Ctrl+Shift+P
  2. Type python select型号 python select
  3. click Python: Select Interpreter点击 Python:Select 解释器
  4. Enter interpreter path ~/.local/pipx/venvs/eth-brownie/Scripts/python输入解释器路径 ~/.local/pipx/venvs/eth-brownie/Scripts/python
  5. You can also click "Find" to search your files for the path if manually inputting it isn't working如果手动输入不起作用,您也可以单击“查找”以搜索文件的路径

This is an addition to @Ali Sıtkı Aslantaş's answer这是@Ali Sıtkı Aslantaş 答案的补充

I tried below pip command & the warning in my python code got resolved.我在 pip 命令和我的 python 代码中的警告下尝试了解决。

pip install brownie

The answer is probably quiet simple, as for me, each time I have to use different packages, I need to select the right interpreter in order not to get that silly warning.答案可能很简单,对我来说,每次我必须使用不同的包时,我需要 select 正确的解释器,以免收到那个愚蠢的警告。

If you are on Windows (I assume you are using VS code) follow the following steps:如果您使用的是 Windows(我假设您使用的是 VS 代码),请按照以下步骤操作:

  1. View > Command Palette查看 > 命令面板
  2. Search for "Python: Select Interpreter"搜索“Python:Select 解释器”
  3. Select "Enter interpreter path..." > "Find..." Select “输入解释器路径...” > “查找...”
  4. Enter this path "C:\Users<< username >>.local\pipx\venvs\eth-brownie\Scripts" (of course swap << username >> with your username)输入此路径“C:\Users<< username >>.local\pipx\venvs\eth-brownie\Scripts”(当然将 << username >> 替换为您的用户名)
  5. Select as interpreter the "python.exe" file contained inside of the Scripts folder. Select 作为脚本文件夹中包含的“python.exe”文件的解释器。

Otherwise, if you are on an Apple device, the Steps to follow should be pretty much the same but at point 4) you need to enter a path that looks similar to the following: ~/.local/pipx/venvs/eth-brownie/bin/python.否则,如果您使用的是 Apple 设备,则遵循的步骤应该几乎相同,但在第 4 点)您需要输入类似于以下内容的路径:~/.local/pipx/venvs/eth-brownie /斌/蟒蛇。

So far, this method allowed me to solve any "Import "" could not be resolved in Pylance".到目前为止,这种方法使我能够解决任何“无法在 Pylance 中解决的导入”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM