VSCode unable to import python libraries - visual-studio-code

i am using vscode and in the python environment setup, whenever i am able to run the script it is showing the import error.
the compiler is failed to import the libraries

How are you installing your dependencies?
If it is just through local pip, then running pip install or pip3 install within the same directory as your requirements.txt file should work.
If you are using poetry, then it might get a little involved.
Again, if these are all local, first try running poetry install within the same directory as pyproject.toml file.
If you are using virtual environments with poetry, try:
Run the command poetry shell to get all of the values you need in order to update vscode's preferences.json file (to get to this file, open Code > Preferences > Settings and then click the document icon in the upper right corner)
When you see "Spawning shell within...." copy/paste that path into virtual environment path "python.venvPath": "<Virtualenv.Path value>"
While inside of the shell, type which python to get your second value, python path "python.pythonPath": "<Virtualenv.Python value>"
Last, click on the python version number in the status bar of your vscode window and make sure to set it to the value that's in step 3.

Related

Use micromamba virtual environment in VS Code

We have to use micromamba for our app because conda is prohibitively slow for installing our packages. We use a devcontainer to install micromamba and its packages. This works for the VS Code terminal but the editor still cannot find my packages.
I only see a way to activate the micromamba environment with a shell script snippet or shell rc file. The works for the terminal, but I dont see a way to activate it for the editor processes. The closest setting I found is specific to venvs.
The solution was to add a .env file setting PYTHONPATH to the modules. Then setting "python.envFile" in .vscode/settings.json to point to that .env file.

VS Code cannot find virtual environment on the interpreter, but can find it on integrated terminal

I have a fully working virtual environment installed on my Linux machine.
This venv can be regularly used by the terminal in VS code calling source /mypath/venv/bin/activate.
The problem is that the Python interpreter in VS code cannot access any of the packages in the virtual environment, despite setting up the path on the interpreter as described in most of the guides.
I decided to manually set up the path in the settings.json file inside the .vscode folder as follows:
{
"python.pythonPath": "/mypath/venv/bin/python3.8"
}
venv is still not accessible through the interpreter. Any other suggestions?
I will answer my own question.
Turned out my pip installation was pointing to a path (standard /home/username/.local/bin/pip) which was different from my venv directory (/my_path/venv/bin/pip).
You can display the path by executing the command which pip.
In my very specific case, there was some mix-up when I first setup my Linux machine, meaning that venv only had a small amount of packages installed, while the directory containing the Python libraries and actually being used was the pip path. In other words, activating venv did not make any difference, since the Python libraries where loaded from the pip path.
So, first I had to ensure that pip had to point to the my venv folder, by modifying the .bashrc file in /home/username/, replacing
export PYTHONPATH=/home/username/.local/lib/python3.8
export PATH=/home/username/.local/bin:$PATH
with
export PYTHONPATH=/my_path/venv/lib/python3.8/
export PATH=/my_path/venv/bin:$PATH
All I had to do after was re-installing each of the required packages in the newer venv (generating a requirements.txt file from the older pip path helped).
Then I selected the venv path in the VS Code interpreter and everything is working fine now.
You actually do not need the settings.json file.
You could try to remove the .venv folder and create a new one by
python -m venv .env
It seems that vs code have changed something from .venv to .env. I'm not sure why.
After doing python -m venv .env open the terminal in vs code and it will active your .env.
You could (if you froze your pip installations) do a pip install -r requirements.txt and you are all good to go.
The default for the "python.envFile" setting is "${workspaceFolder}/.env" change it to "${workspaceFolder}/.venv" and restart vscode.

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

Debugging in VSC is not changing the environment and fails with "no module named"

Initially, I pulled a repo and was able to run the debugger. I set up an environment using conda (to run Python 2.7) and used pip to install the dependencies.
I then wanted to test something else, elected to put the first project into a workspace. Now, when I try to run the project:
1) I see that the environment switches back to the (base), even though I have chosen the conda env/python
2) the modules within the project are no longer found
To illustrate, here's my starting point:
If I press F5, to run in debug mode, then it
Does not change the environment, even though I'm pointing to the conda env27
If I activate env27, it still doesn't work
And if I try this in a Ubuntu terminal window, where I've activated the conda env27, it works
What am I doing wrong?

ionic install fails on fsevents on Windows

I have tried to install ionic on Windows but installation has failed. What should I do?
I have tried more than 20 times but still I got the same problem.
Here I have attached the screenshot for this
Entered commands:
npm install -g cordova
npm install -g ionic
Note: this one windows server machine
My guess is that the NPM global path has not been added to your windows path.
These are the steps to try:
First verify that the command just installed can run. The output contains the full path to the command installed, just copy and paste it to the command line. In your case it might be something like this %APPDATA%\Roaming\npm\iconic -v
Lets assume that works. That means that the command was successfully installed and will run, now we need to add it to your PATH so you can run it from the command line.
Next, Press Windows key and type "path" and select "Edit environment variables for your account". From here. Add or append the path %APPDATA%\Roaming\npm to your PATH variable, and save the results.
Once it is on your PATH you can run it from any command shell window.
Next, start a new command shell (e.g., Windows+R, cmd, Enter) and then type the command iconic -v. Viola, it should work without requiring the full path to the script.
I'm doing most of this from memory so hopefully it's correct. But if it needs some tweeks, let me know and I'll update the answer.
For completness, this install didn't actually fail. The lines with fsevents are warnings. The fsevents package is only designed to work on Mac Unix so these warnings can be safely ignored.
As Suraj Rao mentioned, also see Nodejs cannot find installed module on Windows?.