In my Pycharm projects I use Jupyter to experiment and when something is mature enough I move it to Python files within the project, and make the notebook use it.
From time to time, while running a notebook I change that Python code but am forced to manually restart the server in order to make the notebook use the changed code.
I'd like Pycharm to automatically do it for me.
Is there a way?
Related
I've started playing around with the new VSCode notebooks feature and I find one aspect of it particularly annoying.
I open my "jupyter_notebooks" directory the same way I would when using a typical jupyter server, except I open it in VSCode. And I click on a script to bring it into the editor.
However, when I go to run my first cell, it asks me which python environment I want to run it in. This happens every time I start running any script... this is particularly annoying because the "suggested" environment isn't the one that I always use.
Is there a way to set the default python env to a vscode workspace or something so that when I open that folder in vscode it just assumes which env I want to use and stops asking me?
This is an image of the menu that keeps popping up every time I want to run a new script.
You can use "Ctrl+Shift+P" and choose "Python:Select Interpreter"
Then reload window can work.
For a couple of months I've had issues with working with virtual envs, python notebooks & jupyter in VSCode, for some reason, a lot of the time, it cannot find kernels I added, even though I can see them when running jupyter kernelspec list. It seems random whether they appear or not. Reloading the screen and restarting vscode does not help, neither does disabling and re-enabling the python and jupyter extensions. I tried both from the button, select kernel and through the command palette. I usually start vscode from the command line, with my virtual environment activated. Moreover, a lot of kernels which I've deleted a long time ago still show up in the list.
Vscode and laptop are both up to date.
I've had issues with the integrated jupyter notebooks before, at first formatting and autocomplete didn't work in the notebook (but they did in regular python files).
I'm on MacOS, if that matters.
Sorry to tell that this is a bug, see bug report:
Can't see or select Jupyter kernel's from Kernel drop-down
And now there's a workaround that:
Find where python is installed
Select the command Python: Select Interpreter
Next select Enter interpreter path
Next enter the path into the input box and hit Enter
This interpretr will now work in VS Code and you might need to re-load
VS Code just once more to get it working in jupyter extension.
Please have a try.
For my current job it would be extremely helpful to be able to configure a virtualenv with the appropiate libraries versions, and be able to run either a python project, or cells in jupyter. This is because some people at my job work with jupyter, and some with Python, and sometimes both, and this way I would have a centralized program that could run both types, which I have not found outsaid of the paid version of PyCharm, which my company does not provide.
I just learned a few days ago about Windows Subsystem for Linux, WSL, and that it can be launched from withing Visual Studio Code, so I feel like this is my best bet to achieve that dual nature of programming from just ONE program, instead of running several like in the past.
As of right now, I have a repository cloned with WSL for a git project with different ".py" files, I open it with VSC, then open the terminal inside VSC, and I can both edit the Python code, and run it on the terminal, using bash commands as I would if I were in Ubuntu (I am doing all this from windows but can switch to Ubuntu if it would mean to be able to do this type of setup).
When I run with "Run Python file in terminal", it uses the virtualenv I have previously created.
The problem is, with Jupyter, it does not detect I have the libraries installed (like Pandas for example)
Description of my process with Jupyter so far: With the WSL console, I launch a jupyter notebook &. I then connect to that server, usin the VSC option for "Specify Local or Remote Jupyter server for connections", use the "Existing" option, copy the URL, then I go to the ".ipynb" file and start running code.
If, in a Jupyter cell, I do
import os
os.environ['VIRTUAL_ENV']
I can see my virtual enviroment. If right after that, I run import pandas I get ModuleNotFoundError: No module named 'pandas'
If I do !pip freeze I can see all the libraries and right versions that I have installed in that enviroment.
I feel like I am almost there but something is missing. My guess is that import might be going to some default installed Python, and not the one from the enviroment, for some reason I am missing.
Solved by doing what this answer suggests. The rest of answers may be of help, too:
https://stackoverflow.com/a/51036073/6028947
I want to use Jupyter Notebooks inside VS Code running within WSL 2. Regular Python works within WSL without a problem, that means the base conda enviroment is connected and I can run .py files. Moreover, I’m able to run the command jupyter notebook which will start the notebook server within WSL exposing the localhost to the Windows system.
However, I want to test the new Jupyter Notebook feature within VS Code, which is not working for me; VS Code shows: Jupyter Server: No Kernel and Python: Not Started (see screenshot below).
My testing machine is:
WSL 2 (using Pengwin distribution) on Windows 20H1 (19041.21)
VS Code 1.41 (having the Python extension installed)
Anaconda
What I did:
Open WSL
Open VS Code in my target directory (code .)
Create a new Notebook file from the command palette Python: Create New Blank Jupyter Notebook
Create dummy print("test")
Run Cell will trigger the warning:
Is there anything I’m missing?
The VS Code insiders actually works out of box with Jupyter notebooks. Just remember to run code-insiders . instead of code ..
Another solution would be, according to this website, start the VS Code in windows, then use WSL 2 as a remote server.
The code installation on wsl2 needs a restart to work properly, I was told by a notification, and in the question's comments you may see people experiencing the same issue(' it's working now. not sure why'). So before looking upon any elaborate solution, just restart code.
I have a custom Jupyter kernel which runs IPython using a custom IPython profile which uses a matplotlib stylesheet.
I know to run this successfully normally I would put:
The matplotlib stylesheet in ~/.config/matplotlib/stylelib/
The IPython profile in ~/.ipython/
The kernel json in ~/.jupyter/kernels/my_kernel/
But I am doing this as part of larger program which runs in a virtualenv, and if I put the things as above then any notebook server running on the computer will be able to see the custom kernels, even if it is running outside the venv. I don't what this because I don't want my program to interfere with other notebooks on the computer.
I think what I need to do is put the things above somewhere equivalent inside the venv but I can't figure out where they should go. Doe anyone know where they would go? Or is this just a thing IPython/Jupiter can't/won't do?
It's probably worth mentioning that in the case of the stylesheet for example I don't want to just put it in the working directory of my program (which is one option matplotlib offers).
You can put kernelspecs in VIRTUAL_ENV/share/jupyter/kernels/ and they will be made available if the notebook server is running in that env. In general, <sys.prefix>/share/jupyter/kernels is included in the path to look for kernelspecs.
You can see the various locations Jupyter will look, you can see the output of jupyter --paths:
$ jupyter --paths
config:
/Users/you/.jupyter
/Users/you/env/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/you/Library/Jupyter
/Users/you/env/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/you/Library/Jupyter/runtime
Kernelspecs are considered data files, and will be found in any of those directories listed under data:, in a kernels subdirectory, e.g. /usr/local/share/jupyter/kernels.