简体   繁体   中英

Import "brownie" could not be resolved in Pylance

Error is: 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.

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. Once installed, you will never have to activate a virtual environment prior to using 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 . 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). So you have a few options:

  1. Ignore it
  2. Install brownie with pip in a virtual environment (not recommended)

If there is another suggestion, happy to hear it

  1. open command pallete cmd+shift+P (on mac)
  2. type python select
  3. click Python: Select Interpreter
    • Enter interpreter path ~/.local/pipx/venvs/eth-brownie/bin/python
  4. this works for me.

for Windows:

  1. Ctrl+Shift+P
  2. Type python select
  3. click Python: Select Interpreter
  4. Enter interpreter path ~/.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

I tried below pip command & the warning in my python code got resolved.

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.

If you are on Windows (I assume you are using VS code) follow the following steps:

  1. View > Command Palette
  2. Search for "Python: Select Interpreter"
  3. Select "Enter interpreter path..." > "Find..."
  4. Enter this path "C:\Users<< username >>.local\pipx\venvs\eth-brownie\Scripts" (of course swap << username >> with your username)
  5. Select as interpreter the "python.exe" file contained inside of the Scripts folder.

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.

So far, this method allowed me to solve any "Import "" could not be resolved in Pylance".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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