Import "brownie" could not be resolved in Pylance - python

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!

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

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:
Ignore it
Install brownie with pip in a virtual environment (not recommended)
If there is another suggestion, happy to hear it

for Windows:
Ctrl+Shift+P
Type python select
click Python: Select Interpreter
Enter interpreter path ~/.local/pipx/venvs/eth-brownie/Scripts/python
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:
View > Command Palette
Search for "Python: Select Interpreter"
Select "Enter interpreter path..." > "Find..."
Enter this path "C:\Users<< username >>.local\pipx\venvs\eth-brownie\Scripts" (of course swap << username >> with your username)
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 suggested methods seem correct, however, all of them are for Linux/OS, making the answer really confusing for the windows users.
I followed Patrick's tutorial and my path is: C:\Users\korisnik.local\pipx\venvs\eth-brownie\Scripts
^Korisnik Being the name of the account. It's honestly hard to find path if you don't know where to look at.

Related

in vscode, pygame wont import but it is installed and is working in the windows console ,and the python app thing

im very new to code in general so this could be a stupid beginner problem ive searched for things but i couldnt find something useful for me
under Problems is the message: Import "pygame" could not be resolved
You can use pip show pygame to view the package installed location.
And check whether the installation location is consistent with the version corresponding to the selected Python interpreter.
There are two ways to solve this problem:
"ctrl+shift+P" and change the python interpreter.
If you don't want to change the interpreter, you can use pip -t flodername pygame #flodername is your used python package location. Install the pygame package to the specified directory
First, try restarting your computer or vs code
Use this command in your vs code terminal if the above doesn't work:
pip uninstall pygame
Then reinstall pygame using:
pip install pygame
If this also does not work then follow these steps:
Go to the scripts folder of python.
Open command prompt or Powershell in the script folder.
Use this:
C:\python34\scripts> python -m pip install pygame
Done
As an Anaconda user, I found my packages were being installed to C:\Users\YOUR_USERNAME\anaconda3\Lib\site-packages, which Visual Studio Code was not picking up on.
With the Pylance extension installed, open Settings (File > Preferences > Settings, or Ctrl+,)
At the top, beside Workspace, choose User
Under Extensions > Pylance, scroll until you find Python › Analysis: Extra Paths and select Add item.
Type in the path to which packages are being stored (in this case, C:\Users\YOUR_USERNAME\anaconda3\Lib\site-packages)
Click OK and after a couple moments, you should see something like the below image (i.e, the path is included in the list of paths).
Finally, your Python file should have picked up on the changes, but if not, reload it or VS Code. If nothing else, add another path that packages are being saved to or change your Python Interpreter. Using another virtual environment's interpreter wouldn't be very helpful, after all.
I may be late, but I hope this helped.

Import "flask" could not be resolved from source Pylance (reportMissingModuleSource)

When I am writing
from flask import Flask
One Yellow line is coming up under flask and stating Import "flask" could not be resolved from source Pylance (reportMissingModuleSource) . Also, I am able to do the work with this package also successfully. But the thing is, I am not able to use autosuggesstions for Classes and methods very well.
Further:
I have checked that flask is installed successfully.
Also I visited this thread https://github.com/microsoft/pylance-release/issues/236
And I set up my settings.json in vscode as follows:
"python.analysis.extraPaths": [
"/media/sarimurrab/New Volume/COURSES/Flask/FlaskMigrateforDatabaseMigrations/2"
]
But Still, unable to resolve the error.
When I did not install the module "flask" in the Python environment currently used in VSCode:
Please use the command "pip --version" to check the source of the module installation tool "pip", the module is installed at this location:
Then, we can use the command "pip show flask" to check the installation location of the module "flask": (It checks whether the installation location of the module is consistent with the Python environment displayed in the lower left corner of VSCode.)
If the "reportMissingModuleSource" message is still displayed here, please reload VS Code.
(F1, Developer: Reload Window)
Are you using a Virtualenv? If so make sure that VSCode is using the virtualenv as your python interpreter, otherwise it will not be able to pick up the packages that you installed inside this virtualenv.
To do so, click on the Python interpreter in your bottom bar, you should get a list of possible python interpreters including your virtualenv.
I had a similar issue while trying to import flask on vscode.
I fixed it by using anaconda. Simply you install the flask module in your created environment example screenshot.
How to create a virtual env in anaconda:
1. On the left sidebar, click on environments.
2. Click create (at the bottom).
3. At the pop-up window, give your vir.env a name
and select the language version.
4. Once created, you can start installing
different modules in your environment.
I hope that helps!
For Linux Mint and for those who have installed flask, but VSCode doesn't find it:
check the Flask path: pip show flask (should be smth like
Location: /home/<username>/.local/lib/python3.8/site-packages
in VSCode click left bottom button and choose the python interpreter, in my case I changed it from python3.9 to python3.8 as we can see it in the flask path.
That's because you have not chosen your path correctly,
type:
pipenv --venv
then it will show you where your virtual env is installed. Check where the packages are installed in your env, and then type what comes to you from the shell\scripts or whatever\python, and the packages will work.
I solved my problem by using a "Global" version of Python. Maybe Pylance had not updated to work with the version of Python I was using.
interpreter language screenshot
I was facing same issue. I tried all solutions from stack-overflow but none worked. But after lot of searching and time waste I found my silly mistake. I had created folder named 'flask' and stored my project there. I'm beginner and going through lot of such silly mistakes. Hope it would help, if somebody commits same mistake.
I think this is a Vscode problem ,just restart the Vscode .
By the following steps, I solved this issue:
1)On the project directory create the .flaskenv file
In the .flaskenv file write the following two lines:
FLASK_ENV=development
FLASK_APP=main.py
Please pay attention that main.py is my main file after writing flask run in the Vscode terminal, additionally, you have to create an env folder or requirement file proviosly.
A few answers (Jill's, Marius's, and Roy's) mention the fact that is necessary to choose the correct Python interpreter to make Pylance function properly. I would like to add the fact that this is still necessary to do when using a Jupyter Notebook with the correct Python kernel already chosen.
It is counterintuitive to choose both Python interpreter and notebook's Python kernel to make things work. It is even more counterintuitive considering the fact that Python interpreter's button (on the left bottom of the screen, on status bar) does not necessarily appear when a Jupyter Notebook is open, but when a Python script is open. For instance, in this screenshot, we see the little line under Scikit-learn's import, indicating a problem with the import (even though the import is successful). However, the correct Python kernel, with Scikit-learn installed, is already chosen. Only opening a Python script we notice that the Python interpreter is the reason of this behavior, because a wrong one is chosen, without Scikit-learn. In some sense, one could think that the reason behind this was a problem with the Python kernel or the Conda environment (it is common to experience this kind of problem when experimenting with Jupyter Notebook and Jupyter Lab). I hope this answer may help those who are searching for solving this problem in the specific context of Jupyter Notebooks inside VS Code. They could ignore the other answers because they could think it is not the case for them.

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings

I was trying to download a GUI, but the terminal kept giving me this error:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
I'm trying to install it using this command:
python -m pip install --upgrade pip setuptools virtualenv
Check your Python version and be sure it is installed on your machine
Check the path environment variable
Go to -> "start" and type "Manage App Execution Aliases". Go to it and turn off "Python"
I was having the same issue and I fixed it by using the below method.
Copy two paths of Python
C:\Users\Maninder\AppData\Local\Programs\Python\Python39
C:\Users\Maninder\AppData\Local\Programs\Python\Python39\Scripts
These are the paths where your Python interpreter is installed. Now add this path into your environmental
variable. Put this path into System variable, not in user variable. I was using user variable, so I was facing the issue.
I have a solution for you. Make sure you check the path mark during installation. Then you need to go to Manage App Execution Aliases.
Simply go to your search bar and search for Manage App Execution Aliases. You will find the attached screen and you need to turn off App Installers as you see on the screen. Also, see the path,,, follow Maninder's answer.
Then you are good to go! :)
I had the same issue. In Windows CMD, only: py --version, works.
I tried adding the path on System variables, and it didn't work. If you are using PyCharm as I do, try to run all commands from the IDE's terminal. It usually is on the side bar where the Run and the Console is. If it is not, go to: menu View → Tool Windows → Terminal. It worked just fine for me.
You need to download Python from https://python.org. When in the installation, be sure to check the option that adds Python to PATH.
I haven't gotten this error before and have been using Python a long time, and then suddenly it showed up. I think that it is a result of a Windows update designed to steer you to their store.
In any case: to remedy the problem, go to Settings → app execution aliases → and turn "off" Python. (What they tell you to do, in other words). This should resolve the problem.
If you have installed Python successfully with add python path, ticked on, and have added
C:\Users\<user>\AppData\Local\Programs\Python\Python39
C:\Users\<user>\AppData\Local\Programs\Python\Python39\Scripts
to the path into System variables and have turned off the "aliases" and they all didn't work, you can simply use python instead of python3 in your cmd command.
Check the Aliases for App Execution in Windows. Search for Alias App in your Windows toolbar to find the UI for this. Try turning off anything Python related.
Try adding the following to your "Path" environment variable:
C:\Users\Default\AppData\Local\Programs\Python\Python37
C:\Users\Default\AppData\Local\Programs\Python\Python37\Scripts
Replace Python37 with your own version.
I solved this problem for Visual Studio Code with just writing "python" in the console:
python
After that, Microsoft Store opens automatically with the Python app:
And I just click Get.
And it all work!
All the previous answers are correct, but in my case, I was getting this, because I was not passing the version...
The fix is passing the version:
py.exe -3 your_program
If you're on Windows, you may want to use the Python installer, in Windows Marketplace.
I faced the same error while using Anaconda and trying to link the Python executable path in the command prompt.
It got rectified by going to Settings → App execution aliases → and turning "off" Python. Then again I had to set the path for Python in Anaconda and was successful in executing "python --version" command.
The same thing happened to me even after trying all the above-mentioned steps.
I just restart my system and it was working fine. Do it and if still doesn't fix the issue then make sure you have checked "add python ( any version ) to PATH" before installing Python.
If none of the previous answers are working, you can check if you have the Python executable in your program files.
Go to C:\Program Files and check if you have the Python application. If not, go to the python download website here and download the .exe file.
While installing you must select "Custom install" and select the location as C:/Program Files.
Install it and it should work now from anywhere. This worked for me!
To sincerely resolve this issue, do the following:
Uninstall the Python instance and reinstall it. Note: Make sure you check
"Add variable PATH".
On the command line, type:
python -m pip install --upgrade pip setuptools virtualenv
I got this issue when I used Visual Studio Code as the IDE, and Anaconda as my Python compiler. And you don't need to close the "app alias" in settings, but copy your python.exe to python3.exe in your Anaconda folder.
That happened to me. So, to fix it, you have to follow the following steps:
Uninstall the Python version you already installed.
Go ahead and open the installation file to reinstall it again.
Before hitting Install Now, make sure to tick the box in front of Add Python to path.
Go ahead and complete the installation procedure as usual.
Steps for installing Python
The problem is more subtle than it seems.
For example, if you are using Visual Studio Code on the bottom left, you should see Python X.X.X xx-bit (the X is the version).
If you click in there you will see where the IDE is getting the python.exe from.
Locate that folder into your file explorer and then just follow the answer that is saying to change the environments variables.
So copy the path where python.exe is and add it to the Path variable and do the same where the Script folder is (it is in the same directory where the python.exe is).
Then of course make sure your IDE is using the right Python.
None of the answers here worked for me. I did this and the error went away.
For Windows 11 which I was using, I reran the python-3.10.5-amd64.exe file from my downloads directory and then chose to modify the installation.
Then I followed these easy steps.
Make sure the PIP component is checked before proceeding to install.
Then check 'Add Python to environment variables' if it's not checked already.
Proceed to Install.
At this point, your error will be solved
If you already have the Python executable on your machine and you are getting this error in Windows 10, search for the Python executable and copy its path then copy the path in system variables. It worked for me.
to check in windows
py --version
or restart your pc first then put htis command again
I was also facing this issue after installing python, while running command
python --version in command prompt , error as:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
So, i too added
the path in environment variable as shown, and it worked:

E0401:Unable to import 'flask'

Am new in flask development and trying to import flask in my project but Pylint is giving this error in VSCode E0401:Unable to import 'flask', but I installed flask already in my project but I don't know how to solve this, how can I resolve it.
My virtual environment is installed , and the image which proves is below:
The error am getting code , the underlined from:
what am I missing?
Solution is to switch Interpreter
Simple solution :
Go to command palette
Type-- Python:Select Interpreter
Select virtual environment that you created
answered specifically for vscode can work for other also .
Go to Command Palette using Ctrl + Shift + P.
select python:select interpreter and then choose your appropriate virtualenv.
I guess VS Code doesn't use the correct virtualenv.
To select a virtualenv using the GUI, see Configuring Python environments.
If you use the terminal and have code in your path, launch VS code from your workspace and the virtualenv will be loaded automatically:
cd python-workspace
code -n python-workspace
(note: the -n is for new window)
If this does not work, ensure that pylint is installed in your virtualenv (i.e. your are not using the global pylint). If it still does not work, have a look at this troubleshooting guide.
Due to the fact that you are using a virtual environment, first of all it's required that Pylint is installed inside this virtual env.
Furthermore, you need to add the following entry to your workspace settings to avoid the [pylint] E0401 error:
"python.linting.pylintPath": "/path/to/your/virtualenv/bin/pylint"
This overrides the user settings (global settings) and instructs VSCode to use the Pylint version within your virtual env (instead of the global one). That was probably the point you was missing ;-)
I have the same problem when I code in VS code.
I open Command Palette by ⇧⌘P. And run the linter to solve this problem like this picture.
Here is the document from VS code.
The python extension picks up the venv automatically, and activates it for new terminals panes:
So selecting interpreter is not the issue. The only way I can get this to work is to install pylint into the user path after activating the venv:
./venv/bin/pip install -U pylint
How this works to solve the E0401 I got in VSCode, don't ask me, I can't explain it. Feels like cheating. I thought the point of using virtualenvs was so that we could avoid having to deal with ambiguities across system/site/user modules. 🥺
In my case, the vscode cannot run the lint becouse my flask install with venv environment, so I deactivate the vent and install flask again in the normal environment , the lint will be work~
I solved the problem using the command
sudo apt install python3-flask
You can just install pylint in your virtualenv using
pip install pylint
(inside your terminal) and restart your VS code.
Please check if you have created a .pylintrc file at the root of your project.
In my case, I have disabled pylint. and it works
Use Ctrl+Shift+P to open the command pallet in VSCode and type python select linter, then you can disable the linter there.

VS Code error when importing Django module

I'm working on a web app and I use Django as framework.
I'm using VS Code on a macOS.
I get an error when I try to import some Django module. This is a screenshot of my code in error.
The error message is the following:
[pylint] E0401:Unable to import 'django.conf.urls'
I too was facing this error while working with Python virtual environments. In my case, it was happening because I have installed Django on my virtual environment and my base environment didn't contain any module named Django.
Base(Global) environment
and when I use the same command inside my virtual environment (myDjangoEnv)
Fix:
Now what I understood is that pylint uses the python interpreter to analyze our code and mark the error while we write the code.
Since I have three python environments in my system, hence three different python interpreters are available to my VS Code.
So if VS code uses the Python interpreter from the base environment, it will not recognize the Django module (import Error). Therefore, you are required to change the Python interpreter to the one present in your virtual environment.
It sounds complicated but it is pretty simple:
Click on the bottom left of the screen to change python interpreter.
Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project.
Follow steps mentioned in the image. For details, or if that doesn't work, read further!!!
If you can't see your interpreter (installed in the virtual environment) listed in the drop-down list OR selecting interpreters listed don't rectify the error.
Then, you have to give the path of your interpreter (installed in venv) to vs code. Because you might have installed Django only in your venv. Happens when you don't use anaconda to create venv.
Steps to rectify-
To check the path, activate venv and type which python in terminal, this will give path. Copy the path.
Click interpreter on lower left, to pull drop-down, as shown in pic above.
Click enter the interpreter path.
Paste path copied.
This will assign the right interpreter and rectify the error.
Locate your project's virtual environment. In my case, I am working on a Django project and my virtual environment is located on the path below:
C:/Users/abc/Desktop/Virutal36/myLab/Scripts/python.exe
Copy the address of your virtual environment.
On VS Code, Select File > Preferences> Settings to open your User Settings (or use the Ctrl+, shortcut).
Create or modify an entry for python.pythonPath with the full path to your virtual environment and you will be good to go. In my case it is:
C:/Users/abc/Desktop/Virutal36/myLab01/Scripts/python.exe
https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter
Ctrl+Shift+P
Type Python:select interpreter
Now we will get:
Choose Enter interpreter path:
Select Find...
Then
Check correct django version is properly installed and active?
In the active environment, calling this code in python interpreter shouldn't have errors.
from django.conf.urls import url
Check the VS studio python environment
https://code.visualstudio.com/docs/python/environments
You need to select the right environment. So, go to view in tool bar, then select command pallet(ctrl+shift+p), then type "python:select interpreter", then select the right virtual environment where you start you project.
In my case I solved it using the Select Interpreter option from VS Code's Command Palette (Shift + Command + P).
I chose the Python interpreter option which corresponds with the folder in which my virtual environment was and it solved the issue immediately.
Hope it helps :)
Are you using a virtual environment (mkvirtualenv)? In that case you need to make sure you install django and pylint etc., within your virtual environment too, using the following commands.
workon [yourEnvName]
pip install pylint
pip install django
pip install djangorestframework
and so on...for all the modules you want to use.
enter image description here
Choosing a global environment helped me to recover this issue
First check the requirements that you install djangorestframework, second if you work on virtual envs maybe the pylint that you use not check in your virtual env so you can install in local the package.
For me, I opened the nested folder of my Django project. So, Django couldn't find the modules of venv.
Just mentioning, I thought it might be helpful to someone.

Categories