E0401:Unable to import 'flask' - python

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.

Related

Transitioning from CS50 IDE to VS Code (Import module does not work)

I recently finished with PSET9 and wanted to transition from CS50 IDE to VS Code for the Final Project. Problem is I don't know what exactly I need to set up to make it all work. I guessed that I just need to import the CS50 library, which I then did following me checking out their GitHub. I did as instructed pip3 install cs50. However, even though various files were installed, my application.py is still saying that it can't find any modules, see screenshot. Why is that? What do I need to do?
Also what might I still be missing after having resolved this? Do I still need to do something to fully have the CS50 IDE essentially running on the VS Code (server, databases, etc)?
VS Code screenshot
I think you need to create virtual environment for that and then activate it.
Create virtual environment in command line Windows
go to you root app folder.
python3 -m venv env (type this command in terminal)
for activate env type this in windows (env\scripts\activate)
for apple and linux type this in terminal) source env/bin/activate
then use pip install
First thing to do is just make sure you've got those items installed correctly by using these commands in your terminal:
pip show cs50
flask --version
werkzeug --version
If they're installed, but you haven't quit and restarted VS Code, try that.
If you're using a virtual environment, make sure you reactivate it before trying to run your application.
I hope one of these helps solve your problem!

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:

Module not found error in VS code despite the fact that I installed it

I'm trying to debug some python code using VS code. I'm getting the following error about a module that I am sure is installed.
Exception has occurred: ModuleNotFoundError
No module named 'SimpleITK'
File "C:\Users\Mido\Desktop\ProstateX-project\src\01-preprocessing\03_resample_nifti.py", line 8, in <module>
import SimpleITK as sitk
I installed the module using
sudo pip install SimpleITK
I know that it was installed because I was getting a similar error when I ran the code through the command line, and it was fixed by doing the above. I don't understand why VS code does not recognize that
After installing a new module via pip reloading vscode may work if vscode doesn't recognize it. To do this, make sure that the module is installed inside the virtual environment by creating and activating a virtualenv:
python3 -m venv env
source env/bin/activate
Make sure to use the correct way of installing a module with pip:
python3 -m pip install {new_module}
Replace the string "{new_module}" with your module name. After that, make sure to reload vscode by clicking Ctrl+Shift+P, and selecting Reload window.
Now vscode will know the new module and autocompletion works.
sudo pip install is most likely installing globally into a Python interpreter that is different than the one that you have selected in VS Code. Please select the Python interpreter you want to use and then install explicitly using that interpreter (if you're not using a virtual environment then use something like /path/to/python -m pip install SimpleITK, although I strongly recommend using a virtual environment and to not install packages globally).
In Mac, correctly selecting the Python Interpreter worked for me:
From within VS Code, select a Python 3 interpreter by opening the Command Palette (⇧⌘P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
No interpreter selected
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see Configuring Python environments.
Source :VS Code Select Interpreter
This error: your vscode use other python version. This solution change vscode use current python.
In terminal find current python version:
py --version
In vscode Press Ctrl+Shift+P then type:
Python: Select Interpreter
Select current python version
I ran into this problem with VSCode and resolved it by setting my Python interpreter within VSCode to the same as the one in my system path (type "echo %PATH%" on Windows and look for Python) via the process here: https://code.visualstudio.com/docs/python/python-tutorial#_select-a-python-interpreter
There are a lot of proposed answers that suggest changing the launch.json or the settings.json file. However, neither of these solutions worked for me.
My situation:
Is Python environment selected? yes
Does the Terminal recognize Python environment? yes
Can I run the Python code from the activated Terminal? yes
Does the code run w/o error when I use "Start Debugging"? yes
Does the code run when I click "Run Code"? no
The only solution that worked for me is to:
Open Windows Terminal (or cmd)
Activate environment: conda activate <environment_name>
Open Visual Studio Code from Terminal: code
Then, "Run Code" (#5) works without any issues.
Source:
"module not found error" in VS Code using Conda - l3d00m's answer
Faced similar issue and here is how I fixed it. Remember that there are multiple ways to run your code in VS code. And for each way you may end up with different interpreters and environments. For example:
1. Creating virtual env and installing libraries
In my case I opted into creating virtual environment and doing so outside of VS Code using command prompt:
python -m venv .plotting_test
Following that I activated it:
.plotting_test\Scripts\activate.bat
Following that I installed additional libraries:
python -m pip install matplotlib
Following that I made sure to see it was all installed ok:
python -m pip list
And I also checked where for current directory:
cd
2. Point VS Code & VS Code Code Runner to virtual environment
Opened vs code, closed previous workspaces, opened new folder, created test.py as I was starting new. Pressed ctrl + shift + p. Selected ```Python: Select Interpreter``:
Followed by + Enter interpreted path
Navigated to directory from last step from section 1. Found my virtual environment folder created in step one and pointed VS code to that version's python.exe in Scripts:
Verified I am pointed to such:
Saved as workspace so that I can create default workspace settings for this project:
In workspace settings files defined paths to my virtual environment created n step 1 for workspace interpreter & CODE RUNNER(!):
"settings": {
"python.defaultInterpreterPath": "C:/Users/yyguy/.plotting_test/Scripts/python.exe",
"code-runner.executorMap": {"python": "call C:/Users/yyguy/.plotting_test/Scripts/activate.bat && python -u"}
}
}
Reloaded window just to make sure (ctrl + shift + p) = "Developer: Reload Window"
Now run code and run python file should be execute under your specified envs:
Try running pip list in VS Code to check if the module is installed, next check if your python version is correct/supports that version of SimpleITK. It may be a problem with the python interpreter that you are using for VS Code (ie. the module may be installed on a different python instance than the one your VS Code is using)
Is Python environment selected?
Does the Terminal recognize the Python environment?
Can I run the Python code from the activated Terminal?
Does the code run w/o error when I use "Start Debugging"?
if the answer to the above is "yes."
Then,
Try running the Code using the option "Run python file in terminal" (in code runner extension). And assign a new shortcut for that for future use...
How to fix module not found error in Visual Studio code?
To Solve VSCode ModuleNotFoundError: No module named X Error Make sure you are running from the package folder (not from package/module ) if you want import module. calculations to work. You can also set the PYTHONPATH environment variable to the path to the package folder.
Once you have created a virtual environment, and installed your required packages in that environment, close VS code. For Windows platform, open command prompt and navigate to the folder where your virtual env folder is created. And then launch VS code from there using the command code .
For ex: My virtual env name is .imgenv, and its inside C:\py_stuff\projects
So, I navigate to C:\py_stuff\projects and then type code .
Now, your VS code should recognize the packages !
I just ran into the same issue. I found that if I selected all text before shift enter the script would compile as a file instead of as a single line.
I had the same problem. I bet you have a shebang statement at the top of your file.
If you do.
Visual Studios settings
Under "Code-runner->Code-runner: Respect Shebang" section or just do a search for "Code-runner: Respect Shebang"
Uncheck weather to respect Shebang to run code.
Now it will run under the virtual environment and find the modules that you installed using pip! :)
I struggled with this for a very long time, and had tried almost every other answer. I wasn't using pip, so that wasn't the issue. But still VS Code wasn't finding the modules that were installed in the Selected Interpreter.
Ultimately it came down to old conflicts that existed because I switched to miniconda, and VS Code was still looking for anaconda3.
I completely wiped VS Code and its associated files (cache, preference files, etc.) from my machine (some instructions), and installed a clean version.
This now syncs as expected with miniconda.
If you have different python versions installed, be sure you install module with right one.
python -m pip install <module>
or
python3 -m pip install <module>
Run your environment from a directory not in the users directory. I solved my problem running my environment from C:\Code\ProjectA\
I discovered my problem by running:
IMPORT os
Mycwd = os.getcwd()
PRINT(Mycwd)
.venv/Lib/SitePackages is the default directory where Vscode looks for Modules.
This directory is automatically created on creating .venv via the command Pallete.
External modules installed via pip are placed in this directory by default.
Place self created modules inside this folder manually.
For mac users
In the terminal check which python you are using by command which python. It will give you the path of the python interpreter path. After that type cmd shift P and type Python: Select interpreter.
After that select + Enter interpreter path and paste the path which you got after running the command which python.

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.

Visual Studio Code pylint: Unable to import 'protorpc'

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?
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.
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.
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
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
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"
],
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.
Spent hours trying to fix the error for importing local modules. Code execution was fine but pylint showed:
Unable to import '<module>'
Finally figured:
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
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)
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!
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
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:
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.)
Select Terminal menu > New Terminal, and create an virtual environment directly inside the same directory.
python3 -m venv env
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.
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.
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
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.
First I will check the python3 path where it lives
And then in the VS Code settings just add that path, for example:
"python.pythonPath": "/usr/local/bin/python3"
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
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
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")
In my case, I tried flake8, bandit, didn't work, eventually I uninstalled the extension called python (pylance) and everything worked perfectly.
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.
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.
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.
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: ⌘ / Ctrl⇧P → Workspace 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.
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.
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"
},

Categories