How to code Matlab in VSCode via Jupyter Notebook? - python

My Jupyter kernel is running well since I can do Matlab jobs using a 'browser jupyter notebook' as well as when I was coding in Python. And I can also code Python in my notebooks via VSCode. But it seems weird that I can't code Matlab in notebooks via VSCode. Are there anything that I didn't notice which leads me to this error? Thanks a lot!

I had the same problem, and I tried several approaches.
Adding Matlab and Python kernel path into VSCode "settings.json" file -> didn't work.
Running VSCode with admin rights -> didn't work.
Running "jupyter notebook" and "activate jmatlab" (aka matlab_kernel) with the VSCode terminal -> didn't work.
So, there are two solutions.
Launch VSCode using Anaconda Navigator.
After selecting the jmatlab environment, select one of the two Matlab kernels and run the Matlab code.
Launch VSCode with the command line.
Open terminal (cmd).
Activate the jmatlab environment anywhere you want -> C:> activate jmatlab
Launch VSCode by simple typing -> (jmatlab)C:> code
Test your Matlab 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.

Why wouldn't Python code run in a Jupyter notebook when it runs in the terminal?

I've replicated Slack's API demo in a text editor and the terminal, but just for fun, I wanted to do it again using Jupyter notebook. I used the same virtual environment so as to rule-out dependency issues and the code was the same (cloned from the master repo). But for some reason, I'm always getting ModuleNotFound errors when I try to run the code in Jupyter cells and import the necessary packages.
I even re-built a fresh virtual environment, and ran the demo at a terminal from within Jupyter, and running the scripts works fine. I just doesn't execute in the notebook environment.
I'm using the same kernel across my Python interpreters, and I always start my virtualenv session before launching the notebook.
Anyone have any ideas why this would be the case?
Well, DUH. It was just a matter of uninstalling/reinstalling the package in a fresh virtual environment (and I'm pretty sure the latter wasn't absolutely necessary).
Thanks to this comment on Github for the guidance.

How does one connect a Jupyter Kernel to VS Code if one does not connect automatically?

I want to be able to have a working jupyter notebook working for VS code out of the box, with minimal work on my side.
I tried opening a jupyter notebook. I immediately found the interpreter and used my conda env with the command pellet (command + shift + P then in the drop down menu found my conda env). This seems to make the terminal work since which python points to the right place:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ which python
/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python
also running python scripts from VS Code seems to be working fine too, see output:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/automl-meta-learning/python_playground.py
x = 1
my_str = this is a string
y = 2
but when I try the jupyter notebook it doesn't work.
Most noticeably my VS code does not have a kernel connected, look at the screenshot:
I tried clicking on a couple of arrows as suggested on reddit (vs_code_jupyter_server_no_kernel_python_not):
When I try running things in my jupyter notebook I get the following error:
Error: Activating Python 3.7.6 64-bit ('base': conda) to run Jupyter failed with Error: StdErr from ShellExec, /Users/brandomiranda/.bashrc: line 31: jump-module.bash: No such file or directory
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
.
which is strange. My shell is not even bash so I don't know why that message is being shown to me (maybe VS Code doesn't know Mac OS Catalina uses zsh? not sure how to fix that for VS Code). Regardless, I proceeded to do what I think the error message is suggesting. So I did the following:
I ran conda init and then went down to the terminal inside vs code and restarted the shell by running zsh but the jupyter kernel still doesn't work on my notebook in vs code. Not sure what's wrong.
In addition, I did what the error message suggested:
(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ conda init zsh
no change /Users/brandomiranda/miniconda3/condabin/conda
no change /Users/brandomiranda/miniconda3/bin/conda
no change /Users/brandomiranda/miniconda3/bin/conda-env
no change /Users/brandomiranda/miniconda3/bin/activate
no change /Users/brandomiranda/miniconda3/bin/deactivate
no change /Users/brandomiranda/miniconda3/etc/profile.d/conda.sh
no change /Users/brandomiranda/miniconda3/etc/fish/conf.d/conda.fish
no change /Users/brandomiranda/miniconda3/shell/condabin/Conda.psm1
no change /Users/brandomiranda/miniconda3/shell/condabin/conda-hook.ps1
no change /Users/brandomiranda/miniconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change /Users/brandomiranda/miniconda3/etc/profile.d/conda.csh
no change /Users/brandomiranda/.zshrc
No action taken.
but it seems it made no difference.
Any idea how to fix this?
I was suggessted by the developers of the VS code python extension to follow whatever they did here:
https://github.com/microsoft/vscode-python/issues/9566
but I can't figure out exactly it is they want me to do.
How do I fix this?
Related resources:
reddit: https://www.reddit.com/r/vscode/comments/eq2bfv/vs_code_jupyter_server_no_kernel_python_not/
gitissue: https://github.com/microsoft/vscode-python/issues/9636
I initially thought it was a conda issue so I looked into this question: Activate conda environment stopped working in vscode
https://www.reddit.com/r/vscode/comments/eshxka/how_does_one_connect_a_jupyter_kernel_to_vs_code/
https://www.quora.com/unanswered/How-does-one-connect-a-Jupyter-Kernel-to-VS-Code-if-one-does-not-connect-automatically
You need to select the python interpreter for jupyter, you can do it by following the step
Open command panel
Mac: CMD+Shift+P
PC: CTRL+SHIFT+P
Then search for select Interpreter to start jupyter server then hit enter, it will list all the interpreter, then select any interpreter and done!
it's just a one time process, after this, it will get connected automatically.
As bizarre as it seems, I also noticed this the other day and the only thing that works so far for me is to open VS Code by launching it from the Anaconda Navigator:
Then I get, as expected:
If you see the very long discussion I had in the git issue (https://github.com/microsoft/vscode-python/issues/9566) once I removed all the errors thrown by my .bashrc and .zhrc, the jupyter feature in VS code started working for me again. It's super weird (specially because I am NOT using bash at all and I am using zsh as my shell, I would have expected VS code to be robust to my .SHELLrc files throwing errors but it's not).
If that doesn't work, then you might have to install the vsix view extension and install the ms-python-insiders.vsixlinked in the issue (https://github.com/microsoft/vscode-python/issues/9566).
If on Windows VSCode, what worked for me was installing and enabling the Python and Jupyter extensions, then CTRL+SHIFT+P, select Interpreter to start jupyter server. Those extensions were the bottleneck.

Just installed Jupyter using Anaconda but can't run it using terminal. Says command not found

I just bought my very first Mac after using Windows my entire life and have been trying to install things. I am also still kind of a beginner at programming.
I installed Anaconda for Python 2.7 version, and then I installed the 3.7 version right after.
I tried going to terminal and typing jupyter notebook but it says:
zsh: command not found: jupyter
I am able to launch a jupyter notebook (I think) when I search for jupyter notebook in spotlight and executing the top hit.
I've been reading a lot on forums about this issue and see a lot of people talking about PATH which I am not really sure about.
I've seen people also post their which python information and theirs seem very different from mine:
which python3
usr/bin/python3
which python
usr/bin/python
Any help would be appreciated! Still learning how to set up this Mac. Thank you.
You might want to refer to this link https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html
3.1.2. Change Jupyter Notebook startup folder (Mac OS)
To launch Jupyter Notebook App:
Click on spotlight, type terminal to open a terminal window.
Enter the startup folder by typing cd /some_folder_name.
Type jupyter notebook to launch the Jupyter Notebook App The notebook interface will appear in a new browser window or tab.
From what I can understand you are not changing to the startup directory of Jupyter.
You need to add jupyter to your path.
In your terminal, type in the following command:
export PATH="/PUT JUPYTER PATH HERE:$PATH"
Then try jupyter notebook
Reasoning: If installing using pip install --user, you must add the user-level bin directory to your PATH environment variable in order to launch jupyter lab

VS Code: python.exe works in terminal only when opened from Anaconda Navigator

When launching Visual Studio Code from the Anaconda Navigator page, the python command works correctly in the integrated terminal (Powershell or Bash).
However, when Visual Studio Code is launched normally (from start menu), python.exe is not found in the terminal (not in path).
Is there a way to always apply Anaconda's config to VS Code, to avoid having to launch it from the Anaconda Navigator page?
When you installed Anaconda you probably had it leave the copy of Python in your base environment off of PATH. That means when you open your terminal it won't be found normally (this isn't specific to VS Code). One way to deal with this is to create conda environments for your project as those will get activated in the terminal when you use the Python: Create Terminal command. That will then make python available as a command from your terminal.

Categories