Debugging in VSC is not changing the environment and fails with "no module named" - visual-studio-code

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?

Related

Stale environment variables in Jupyter Notebooks running in vscode using a conda environment kernel

I'm using jupyter inside a conda environment in vscode on Ubuntu and all works great. However, recently I've experienced issues with AWS access tokens expiring inside my jupyter notebook.
It seems that these access tokens get cached at some level, either in the code environment or somewhere in the jupyter layer, but I couldn't figure out where and how to clean it up.
Example:
I'm using aws-vault to generate SSO credentails, that can be accessed in the environment.
After starting a session, I can access the AWS_SESSION_EXPIRATION from my terminal:
echo $AWS_SESSION_EXPIRATION
2022-04-20T17:40:36Z
when I start vscode, open a terminal, I get the same.
When I activate my environment, and run
python -c "import os;print(os.environ['AWS_SESSION_EXPIRATION'])"
I get the same.
However, when I open a new jupyter notebook and select the same environment, and run
import os
print(os.environ['AWS_SESSION_EXPIRATION'])
I get
2022-04-06T11:09:03Z
I've tried locating any files that could cache the env variables either in the conda env or jupyter extension. I've also tried restarting vscode and the machine itself - all without success.
Can anyone help me clean this up?
Thanks!!
Edit:
Another example:
If I set in my terminal
export FOO=BAR
then I can access $FOO as expected through the terminal and jupy notebook within vscode.
Then I close vscode, run
unset FOO
reboot the machine, start vscode new and open a new notebook with the same conda environment.
Within, running
import os
print(os.environ['FOO'])
STILL returns BAR
There seems to be an issue in the vscode-jupyter extension, that somehow stores the environment variables in the kernel.json file.
At the moment of writing this, the issue is still open. Working in ubuntu, this workaround from the issue's thread works for me:
Close code completely
find ~/.vscode/extensions/ms-toolsai.jupyter*
code .
Run a notebook and it will pick up the changes.
With closed vscode, issue the following command to delete cache:
find ~/.vscode/extensions/ms-toolsai.jupyter* -name kernel.json -delete
Source: https://github.com/microsoft/vscode-jupyter/issues/9774#issuecomment-1110328329

why my vscode does not have the same libraries installed in wsl?

I'm using wsl and it runs codes in vscode pretty fine and I have different libraries which I installed through pip and conda in wsl but when I run that code using vscode itself it doesn't recognize the libraries or even pip itself.
I don't have any other environment.
I should add that I installed the packages globally using conda install ... or pip install ... in base environment and I only have base environment and I run my code through code . and I also have python and remote wsl extensions installed in my vscode.
what can be the problem?
I don't have much personal experience with this, but I found some useful information in this Stack Overflow question (even though it doesn't utilize conda), along with https://code.visualstudio.com/docs/remote/wsl-tutorial#_python-development.
I also found this blog post useful, even if it doesn't cover WSL.
In short, make sure you:
Have installed the Python extension (by Microsoft) in VSCode. This is critical for being able to detect and select the Python interpreter. You don't mention having this in place, so I believe this is your likely problem.
You have done this already, but including it for others who might read this later -- Install the Remote - WSL extension (or the Remote Development extension pack) in VSCode.
You are also doing this already -- Start VSCode from inside your WSL distribution. Alternatively, you can start VSCode from Windows and then select the Remote WSL - Reopen Folder in WSL from the Command Palette (also accessible from the "Remote" Status Bar).
In VSCode, open the Command Palette with Shift+Ctrl+P, search for the Python: Select Interpreter command, and you should find your Conda environment in the list.
After selecting this, you should find that your project is using the interpreter and modules that you have installed via conda.
One thing I did to overcome this issue is go to Extensions -> Local (You should have two tabs there, Local and WSL:DISTRO) DISTRO refers to whatever DISTRO you're using, you will see that some of the local extensions are disabled in the current workspace (WSL) and there is a little cloud icon in the WSL:DISTRO tab that says install Local Extensions in WSL:DISTRO once you click that it will let you choose which extensions to install and you should be good to go!

Adding click autocompletion to conda env activate script

I'm using a Python library which uses click autocompletion. Since I've installed the library in a conda env, I'd like the autocomplete to be associated with it. (Also, since it isn't installed in my primary Python env, adding eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)" to my .zshrc doesn't work.) The documentation for the library I'm using says "if gradient was installed in a virtual environment, the following has to be added to the activate script":
eval "$(_GRADIENT_COMPLETE=source gradient)"
I originally added this to ~/miniconda3/envs/my_env/lib/python3.6/venv/scripts/common/activate, but the autocompletion didn't work. Running
source ~/miniconda3/envs/my_env/lib/python3.6/venv/scripts/common/activate
does work, but my shell prepends via __VENV_DIR__ to the prompt, and the fact that this doesn't happen automatically when I run conda activate myenv makes me think this is the wrong way to do it (for one, it isn't disabled when I do conda deactivate my_env).
What I'm looking for is the canonical way to add a script to run upon conda activate x, then end upon conda deactivate x. This seems very close, but it's for adding shell variables with export and unset. Is there a way to do it with click's autocomplete?
Following a small modification of the instructions in the docs seemed to work for me - I placed the eval statement in env_vars.sh, and nothing in deactivate.d.
My understanding is that export is persistent in the shell throughout sessions, and so must be undone with a corresponding unset. Whereas eval only works for that session, so as soon as the conda env is deactivated it no longer has an effect.
Would be happy to hear more from someone with a deeper understanding of bash/conda under the hood!

VSCode unable to import python libraries

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.

"Running cells requires Jupyter notebooks to be installed" error in VS Code

I'm trying to use the new Jupyter integration for the Python extension in VS Code, and I'm getting the above error even though I have Jupyter installed and it works fine from the command prompt.
Here's my environment:
Python extension version 2018.10.1, and I see Run Cell/Run All Cells tooltips above #%% comments.
I've used the Python: Select Interpreter command to select my Anaconda environment, which is at ~/AppData/Local/Continuum/anaconda3/python.exe.
I have Jupyter installed in that interpreter (jupyter.exe is in the Scripts sub-folder under that location), and it runs fine with the jupyter notebook command at the Anaconda prompt.
But whenever I click on Run Cell or press shift-enter, I get this error message:
"Running cells requires Jupyter notebooks to be installed." Source: Python (Extension)
Is there something else I need to do to configure this?
You may give one try by restarting VS Code in following mentioned way [ It worked for me. ]
Open Bash or any other cmd
Activate any conda environment [ See below command ]
source activate base [ means activate base environment ]
Run VS Code instance [ See below command ]
code .
Now when you'll click on Run Cell or press shift-enter, it should work.
The problem is an issue in the VS Code python extension itself. There are a number of issues related to this open in the repository: #3354, #3343, #3330, and the issues are being worked on, see #3374.
The reason, as far as I understand, is that in this case - and in some other cases - the anaconda environment is not activated before running the command. Situations where the environment is activated are e.g. opening a python terminal or running a file in the python terminal, but this also needs to happen for Jupyter, Tests, and so on.
While theoretically, adding the Scripts folder to your PATH, as David mentioned, could help, it did not help in my case. This may just not be enough to properly reflect what happens on activation.
My guess is that we will have to wait for this issue to be resolved in the repo, but if someone else finds a workaround, I'd be happy.
Simply running vscode from the activated environment did not work for me, here is what did:
In terminal (bash) I ran:
conda activate <environment-name>
conda install jupyter notebook
When the install finishes, open vscode from terminal (the same shell with activated environment) with the command:
code .
Notes:
Replace '.' with the path to the directory you want to open if it's not the current directory.
I've written 'conda install ...' but mamba also works.
If the terminal command for 'code ' does not work, it's likely you need to add it to environemnt variables; in such a case, this post might help.