Why is VSCode running the wrong jupyter server? - python

macOS v. 11.2.3.
2 versions of python installed
/opt/homebrew/bin/python3
/usr/bin/python3
I created a virtual environment (venv) to work with a jupyter notebook, but every time I open a document in a VSCode, I see 2 servers start up at the same time and the last one that is selected is not the one that is in the virtual environment:
Moreover, every time I open a document, I have several files in the trash with something like this name:
chunks-3a679475-8ceb-44eb-9ac3-fcefe0595bc1.ipynb
How can i fix this?

In VS Code, it is normal that the python environments of the two are different. The python kernel used by Jupyter Notebook can be independent of the python environment selected in the lower left corner of VS Code, and usually it is the python environment used by VS Code by default. If the python extension has not been loaded yet, Jupyter uses the last python environment by default.
Please click on the location of the python kernel displayed on the upper right of Jupyter, and select the one you need from the expanded options. In addition, please try to reload VS Code.
Reference: Working with Jupyter Notebooks in Visual Studio Code.

Related

Python: Use Jupyter kernel in VSCode

I am using Python 3 in Visual Studio Code (latest version).
I am wondering if there is a way to run Python scripts (in .py format) in VSCode adopting Jupyter Notebook as default terminal (instead of Powershell).
In a few words, I would like to make VSCode similar to Spyder IDE, which uses Jupyter Notebook as terminal and shows script variables in Variables Explorer.
Thanks in advance.
Using Jupyter extension you can create notebooks and interactive windows.
By clicking the Variables icon in the top toolbar after running code and cells, as mentioned on vscode documentation, you can view your variable explorer.

Python Module Can Not be Found Even Though It Is Installed

I used python and pandas so many times in VS Code but It causes a problem right know : problem
The terminal looks like this : terminal and I checked if matplotlib is installed or not but seems like it is installed matplotlib check. I don't know why this is happening but It was working until now.
The reason is that the python environment you are currently using in VS Code is different from the python environment used by the VS Code terminal.
Solution: Please close the currently used VS Code terminal (click the icon "Kill Terminal"), then select the python environment that has installed the required module at the bottom left of VS Code, and then use the shortcut key Ctrl+Shift+` to open the new VS Code terminal.
In addition, I noticed that you are using a powershell terminal. If you are using a virtual environment or conda environment, we need to activate this environment in the terminal (Usually it will automatically activate when a new terminal is opened.):
Check module:
Run:
Reference: Using Python environments in VS Code.

My Visual Studio Code is not using the Python interpreter that I have set it to

I am currently working on setting up pytorch to be used on Visual Studio Code on my Windows 10 machine. Currently I can use it when I am running it on Anaconda prompt. When running on VS Code, I changed the python interpreter to match the one I am using on Anaconda, but when I try to run it is still using the original, and does not recognize "import torch". I am fairly new to using VS Code and running python on Windows, as I usually code in Linux, any help would be much appreciated!
I have attached a photo showing the current environment as well as the result when typing in "python --version" in the command line on VS Code. Screenshot
The reason is that the VSCode terminal is not refreshed and it is still in the previous state.
Solution:
We can use the shortcut key Ctrl+Shift+` (or you can also click "Terminal", "New Terminal") to open a new VSCode terminal, and it will automatically enter the currently selected Python interpreter environment.
In addition, if you need to import and use the module "torch", please install it in the currently selected environment.
Update:
Please check whether Python is available:
Please enter "where python" in the cmd window:
Please check whether there is a python path in the python environment variable:
Generally, for the python global environment, when we enter "python --version" in the terminal, the system will find the first python in the environment variables by default, so it is recommended that you put the commonly used python at the top of the environment variables and restart VSCode.
For conda environment and virtual environment, VSCode terminal will automatically enter the currently selected environment.
Reference: Environment in VScode.
You can click in Python 3.8.. Conda and then vscode shows a list of python interpreters, select your interprete and vscode creates a settings.json in the .vscode folder with the path of your python interpreter in the current folder open in vscode and everytime you open that folder vscode automatically detects your python interpreter.

Using system environment variables in spyder

I have a program coded in Python 2 that I need to run and I would like to run it in spyder via anaconda software.
The issue is that to run the program via the terminal I had to previously add in my system environment variables two new variables:
1) one folder in the computer where some required packages are
2) a variable with the ip address for the required license.
Although I have these variables define and the program runs without issues in the terminal. When I run it in spyder there is an error because it is not using those variables.
How can I fix this? I head something about anaconda creates virtual environments that is not loading those variables but I could not understand anything else. For example if I use pycharm, outside anaconda, it also works flawlessly
Thank you!
Emanuel
I believe Anaconda offers instructions on setting environment variables but that approach seemed complicated.
Setting an environment variable no longer seems to be an option in Spyder.
As a workaround Spyder does allow you to specify a startup file:
Preferences -> IPython Console -> Startup
In the startup file you just run something like:
import os
os.environ['my_var'] = 'my_value'
os.environ['test'] = 'test_value'
Every time you start a new ipython console this code will run.
There's an option in the Tools menu that let's you check/insert/update/remove the currently active environment variables in Spyder (pic for reference).
Spyder Environment Variables Feature
Edit: Regarding your PS question, when you install packages (through pip?) you are doing so through the currently active Python installation, which you can check by running
python -V
Therefore, if you intend to install a package in multiple installations, you need to use the corresponding package manager executable.
That dropdown menu is on Spyder 5.1.5

VS code runs python with no errors only if started from conda terminal

One of the projects that I work on in Python is pretty big and uses a lot of libraries.
I started developing it in Spyder then switched to VS Code.
I also use conda environment.
The problem is with VS Code only: when I open VS Code itself and try to run the project - I get all kinds of import errors.
However, if I open the conda terminal first and just type "code" in it and execute it - the VS Code opens, I can select my project and everything runs just fine, no errors.
In both cases the environment is the same, I did the Ctrl+Shift+P to select it, plus it shows as selected in the bottom left corner anyway. It's the default env.
How can I fix this so I don't have to start VS Code from conda terminal each time?
at the conda the modules are installed by default.
in vscode or any other of ides you have to install it manually.
open your terminal or cmd and type:
pip install module-name
I found what the problem was so I'll just leave it here in case it may happen to someone else.
My VS Code was using Powershell as the default shell, even though the input was right with the correct conda env selected (the base in my case), even with running
conda activate base
command to ensure it, it would still somehow mess up and complain about missing imports
So the solution was to set the default shell in VSC to regular Windows cmd.exe - that was the only needed change

Categories