Jupyter notebook: how to run python shell command on current kernel? - python

I'm trying to run a simple
!python script.py
command within a Jupyter notebook running on a custom kernel (virtual environment) where I installed a number of modules. When I run the above command, I am getting some errors related to missing modules, meaning it is not running that code on the same virtual environment as the jupyter notebook.
Is there any way to solve this?

First activate the virtual env then start your notebook.Then it will have your activated virtual env packages
source stackpy/bin/activate
sudo jupyter notebook --allow-root

Related

VS Code does not find Python kernel

I am running VS Code on a Mac OS. I have installed Jupyter extension but I'm unable connect to my Python virtual environments. When I create a new virtual environment with Anaconda, VS Code appropriately recognizes the virtual environments but the ones I create manually do not work.
I have no problem launching jupyter notebook in the browser from the command line but I'd like to be able to use VS code. Also, I have no issues running plain python scripts in the virtual environment I have created.
Any help is greatly appreciated!
Thanks!
I have tried the following steps:
Create a new virtual environment:
cd ~/.virtuaenvs/
python3 -m venv new_venv
Activate the new virtual environment:
source new_venv/bin/activate
Install ipykernel and jupyter
pip install ipykernel jupyter
Add environment to kernels list
python3 -m ipykernel install --name "new_venv" --user
Restart VS code. But the new_venv does not show in my list of kernels when opening a Jupyter notebook.
I did the same in my vscode. Unfortunately, I can get the kernel directly from the list.
I suggest you manually add the virtual environment path in the settings.json file (use shortcuts "Ctrl+shift+P" and type Preference: Open user settings(JSON)):
"python.venvPath": "${fileDirname}/new_venv/Scripts/python",

MacOS SSH Server find password

I have been trying to use Jupyter Notebooks and pipenv within Visual Studio Code using the Jupyter notebooks extension. My notebook file that just does print("hello") works fine using the global venv, but when using a pipenv env of the local project, it throws this error:
The kernel failed to start as '/Users/username/.local/share/virtualenvs/
crypto-analytics-N-3y3yvZ/lib/python3.10/site-packages/psutil/_psutil_osx.cpython-310-darwin.so'
could not be imported from '/Users/username/.local/share/virtualenvs/
crypto-analytics-N-3y3yvZ/lib/python3.10/site-packages/psutil/_psutil_osx.cpython-310-darwin.so,
0x0002'.
I am running this on an M1 Macbook. (that might be why I get the
Prior to running the notebook, I ran pipenv install ipykernel
Moreover, running jupyter notebook from outside of the local env (just running on my main env) does not let me open the .ipynb file at all Error loading notebook.
How can I fix this? What am I doing wrong? I just want to use pipenv and jupyter notebook in vscode.

How to use a virtualenv from inside a jupyter notebook

I am using a jupyter notebook, and inside this running jupyter notebook I want to use a virtualenv.
I was able to create and source(?) a virtualenv by using these commands
!virtualenv venv
!. venv/bin/activate
but it looks like python is still being used from the standard location
!which python
/opt/conda/bin/python
Is there any way to create and switch virtual environments from inside a running jupyter notebook?
After activating the venv
pip install ipykernel
ipython kernel install --user --name=venv
jupyter notebook
In jupyter Notebook you would be able to choose between venv and python x.

Anaconda and jupyter notebooks: How to switch the used virtual environment?

I have got two anaconda environments. The base one and my testing environment called testenv. This environment works without an issue for example with the PyCharm IDE.
When starting a jupyter server straight from the pictured directory by issuing jupyter notebook my scripts/notebooks complains about missing packages.
This tells me it is using base environment instead of my testenv environment. But as the screenshot show, its not the one thats activated.
So the question is: How can I set the virtual environment used by jupyter?
Used versions:
Windows 10 Pro x64
Jupyter Notebook 5.7.4
Python 3.7.1
Conda 4.5.12
[Edit] It turned out that jupyter was not even installed in my environment. I was assuming to be able to use the installation coming from base. After installing jupyter within my environment everything was working as expected.
This is usually foolproof for me. However, it does not allow switching virutal env on the fly. Conda docs.
Windows
$ activate testenv
$ jupyter notebook
Linux
$ source activate testenv
$ jupyter notebook
If you'd like to be able to switch between your envs even on the fly from within Jupyter, this should also do the trick.
How do I add python3 kernel to jupyter (IPython)

Load Keras library in Jupiter notebook using virtual environment

I installed TensorFlow and Keras using virtual environment as explained here and here.
Then inside the virtual environment I installed Jupyter notebook and executed this command to create a new kernel:
ipython kernel install --name "jupyter3_Python_3" --user
When I run jupyter-notebook from the console and change the kernel to jupyter3_Python_3 in Jupyter notebook, I get the following error:
It looks like keras in not installed.
However, when I run python from the virtual environment and do import keras, then everything works.
I assume that I incorrectly launch Jupyter notebook, therefore it does not get access to the virtual environment. But I followed all instructions. Any idea?
Try to first enter your virtual environment from the console:
source /var/venv/virtual_environment/bin/activate # or whatever your path is
and then run jupyter notebook

Categories