VSCode displays "Module numpy could not be resolved" - python

I have just installed VS Code and trying to run python code. In the past, I had already created some virtual environments (which I am able to see in VS Code), but for the moment I am using the base one.
I am trying to import some standard libraries which are present in the base environment (I tried also the conda environment), but VS code displays that the modules could not be resolved (Pylance).
I have noticed that when I open VS Code, the terminal tries to activate conda with "conda activate PATH_TO_MY_FOLDER/.conda", so it's trying to activate an environment in my folder, but it displays this error: conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I have also noticed that VS Code warns me of the environment variable PATH which has some """ characters:
EDIT: I add here also the Path environment variable relative to my user (not relative to the system)
Do you have any suggestion on how to proceed?
Thanks.

Try to add path to environment variable.
C:\Users\username\AppData\Local\Programs\Microsoft VS Code\bin
check the path of your VS Code bin folder

Related

Why 'pip' is not recognized even if Anaconda path is set to environment variable?

I installed Anaconda and added path in environment variable. When I try to use pip command in VS Code it's displaying error. What I'm doing wrong here?
I could able to fix this issue. This is the process I followed in VS Code.
Install Python latest version in your local machine. Once completed Open VS Code and next press Ctrl-Shift-p to select Python interpreter. Next Choose default python.exe location C:\Users\username\AppData\Local\Programs\Python\Python39\python.exe

Python 3.9.1 path variable

Good day!
Installed the Python 3.9.1, checked "Add to path", the cmd did not work though.
Added Environment Variable Path, both folder
C:\Users\XXXXX\AppData\Local\Programs\Python\Python39
(file manager opens the path to python.exe just fine)
and script lines:
C:\Users\XXXXX\AppData\Local\Programs\Python\Python39
Still the commands python -version and pip --version do not work from the command line.
Py --version works just fine though.
Anyone might share and idea what might be the reason?
This happens more often than one would think.
When you installed python from python.org and follow the installer, post install, you should check your environment variables, and verify that you have no other python installation (or if you do just name them appropriately).
Often you can find that there is an Environment Variable, that includes a python version on the global Environment Variable Path.
To verify that you are targeting the correct python version put these two directories (replace, user and python version), at the top of your PATH (user's path), and check that there are no conflicts with the rest of the PATH variables.
C:\Users\XXXXX\AppData\Local\Programs\Python\PythonXX
C:\Users\XXXXX\AppData\Local\Programs\Python\PythonXX\Scripts
If you had Python installed in the system before, the new path is added at the end of PATH system variable and when system looks for python.exe it finds first the old version that is available under a different folder.
If you used a command window opened before the new version got installed, it is also possible that system variables did not reload. Close it and use a new one to check.

Create Python PATH

I'm trying to do pip installs, but I get errors saying "is not a recognized as an internal or external command", so I tried following the guide below, but after creating the PATH and typing "Python" in the CMD, I still get "Python is not recognized as an internal or external command, operable program or batch file.
https://projects.raspberrypi.org/en/projects/using-pip-on-windows/4
I installed python from https://www.python.org/downloads/
Also, I'm not sure if this has an effect or not, but I do already have Anaconda installed on my computer, but I need to use PyCharm, which is why I'm trying to "reinstall" Python.
Since you have anaconda and you intend to use PyCharm, I would strongly recommend you to use the conda environment as the interpret in PyCharm, unless you want to use other virtual environment management tools.
If you insist to use the system Python, what you should do is
Add the system python path into your environment variables
Ensure you have removed other python path (such as anaconda) in your environment variables
To better help you, you could share your environment variables here.

'Unable to import' errors for anaconda environment in VS Code

I am trying to solve partial differential equations with Python using FEniCS. I installed it with anaconda and conda-forge and to use it, I activate the fenicsproject environment
source activate fenicsproject
I run my scripts in jupyter (that works), but often it is more convenient to use VS Code for more elaborate code. When I run the scripts written in VS Code in the (built-in) terminal, they run without error as long as I have the fenicsproject environment enabled.
But in the editor I get a lot of errors like this
[pylint] Unable to import '...' [E0401]'
[pylint] Undefined variable '...' [E0602]
How can I get rid of those errors in the editor, so that the real errors can stand out.
What would be even better, make it that auto-complete and suggestions work for the packages like fenics, mshr etc.
According to the Python in Visual Studio Code docs, this is probably due to Visual Studio Code pointing at the wrong Python version.
1. Unable to import (pylint)
Scenario: You have a module installed, however the linter in the IDE is complaining about; not being able to import the module, hence error
messages such as the following are displayed as linter errors:
.. unable to import 'xxx' ..
Cause: The Python extension is most likely using the wrong version of Pylint.
Solution 1: (configure workspace settings to point to fully qualified python executable):
Open the workspace settings (settings.json)
Identify the fully qualified path to the python executable (this could even be a virtual environment)
Ensure Pylint is installed for the above python environment
Configure the setting "pythonPath" to point to (previously identified) the fully qualified python executable.
"python.pythonPath": "/users/xxx/bin/python" ```
Solution 2: (open VS Code from an activated virtual environment):
Open the terminal window
Activate the relevant python virtual environment
Ensure Pylint is installed within this virtual environment
pip install pylint
Close all instances of VS Code
Launch VS Code from within this terminal window
(this will ensure the VS Code process will inherit all of the Virtual Env environment settings)
A slight correction to "Solution 1" above: use
"python.defaultInterpreterPath": "/users/xxx/bin/python" ```
source: https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath

The Python executable is not recognized on Windows 10

I recently installed Python 3.6.3 on my device. When I type python in my cmd window, it gives me this error. I do have the PATH in the environment variables. Path Lists. This should normally fix it, but python is still not recognized.
I had the same problem, due to a stupid decision from Microsoft.
I had another line in my system path variable:
C:\Users\MyUserName\AppData\Local\Microsoft\WindowsApps
And Windows 10 put a python.exe file there that only redirected me to the Python page of the Microsoft Store:
Since this line was above my true Python path, typing python in the cmd prompt opened the Microsoft Store...
Solution:
Type App execution aliases in the Windows 10 search bar, and then uncheck the aliases for python.exe and python3.exe. More information is here.
The path to the Python executable needs to be in the System PATH variable. Note this is not the User PATH variable.
The OP had Python 3.6.3 installed in C:\Python\Python36-32, however a common default location for installation is in the users AppData\Local\Programs\ folder. This post will assume a Python 3.10 installation in this location. Please adjust as needed for your current Python version and path.
You can confirm the path from the command prompt by checking the Python executable directly from the folder.
cd %LOCALAPPDATA%\Programs\Python\Python310
python --version
This should print the Python interpreter version. For example, Python 3.10.4.
Add the following entries to the System PATH:
%LOCALAPPDATA%\Programs\Python\Python310 for the python executable
%LOCALAPPDATA%\Programs\Python\Python310\Scripts for tools such as pip
After adding the path to the System PATH variable, make sure you close and reopen any command prompts, so they use the updated PATH.
If it is still not working, as mentioned in the previous answers, then simply move up the path, as shown in the screenshot.
You get this error, because the python.exe path has not been added to the System environment variable. To do that, simply:
go to the path C:\Users\%Username%\AppData\Local\Programs\Python\Python37-32 which contains python.exe
copy the path and open System environment variables and in the variable section look for variable called Path. If not, create a new variable with Variable Name as Path and value as the copied path
Once you do that, close the window and simply open cmd. Type python and you should get something like this:
If you are trying to install the new Python version, python-3.9.6, then click the checkbox of Add Python 3.9 to PATH
This happens because the path doesn't exist in environment variables.
To fix this:
Rerun the installer
Choose Modify
In optional feature click "Next"
In advanced option tick the "Add Python to environment variables"
Install
All the answers on Stack Overflow_ about it were obsolete, so I decided to add this. The path variable that needs to be added is,
C:\Users\Username\AppData\Local\Programs\Python\Python36-32
Note that everyone may have a different Username.
If you are working with the command prompt and if you are facing the issue even after adding the Python path to system variable PATH:
Remember to restart the command prompt (cmde.exe).
I too was struggling with this issue, and everything was configured correctly. I installed and setup my system path variables as one would. And everything was fine. Then upon reboot windows would not recognize 'python' as a command as if it were unaware of the path variable. Double checked to confirm everything was still setup correctly (it was).
It only started to work again after I manually browsed to the directory in which python.exe existed, and ran 'python'. After that windows seemed to recognize my path variable and I could call 'python' from where ever need be. Very odd. Figured I'd share in case someone else runs into this.
For Python 3.6, there are actually two path entries. Also, it's best to use the %USERPROFILE% rather than hard coding it.
%USERPROFILE%\AppData\Local\Programs\Python\Python36
%USERPROFILE%\AppData\Local\Programs\Python\Python36\Scripts

Categories