VScode jupyer not loading ipython instance installed in a conda environment - python

I have noticed this both on Linux and MacOS. I have a conda environment for data science stuff, which I have installed ipython, ipykernel, jupyer, and a bunch of other data science dependencies. In VSCode, when I try to select a python interpreter, it shows just fine.
I have been able to run regular python files without issue. However, in Jupyter notebooks, when I try to select a kernel, it is only showing the system installed python interpreter (/usr/bin/python and such). Oddly, sometimes if I click the 'Select kernel' button early enough, it will temporarily show the conda environment as an option, but then in a second it will disappear. If I click the option fast enough, it also just resets to the system python and when I try again to select the kernel, it only shows the system options.

This problem occurs in python 3.11.
Open the extension store and change the jupyter extension to pre-release version.
Use command python -m pip install jupyter in the terminal.
Use shortcuts "Ctrl+Shift+P" and search the following option:

Related

Jupyter notebook setup in VS Code was working fine; now get "Running cells with 'Python 3.9.12 64-bit' requires ipykernel package."

I have a python project (folder) that I'm working on in VS Code (in Windows) and it uses Jupyter notebook. The project uses a virtual env. It was working fine a few days ago. Today when I open up one of the .ipynb files in the project, I see:
Running cells with 'Python 3.9.12 64-bit' requires ipykernel package.
Run the following command to install 'ipykernel' into the Python
environment. Command:
'c:/Users/myName/AppData/Local/Programs/Python/Python39/python.exe -m
pip install ipykernel -U --user --force-reinstall'
I don't understand why VS Code is looking at the system installation of python. I have the following in settings.json in the .vscode subfolder of this project:
{
"python.pythonPath": "proj_env\\Scripts\\python.exe",
"python.terminal.activateEnvironment": true
}
And in the virtual environment for this project, I have confirmed I have ipykernel installed. It was working fine a few days ago. I see that ipykernel was updated 10/31/2022, so just to try it, I updated the ipykernel package in the virtual env from 6.15.2 to 6.17.0.
But clearly for some reason VS Code is looking to the system installation of python instead of the virtual env.
Pulling my hair out because I changed nothing in the past few days. In fact I just opened the project today for the first time since a few days ago when it was working fine.
When I press CTRL+SHIFT+P to select python interpreter, the virtual env python interpreter is already selected. But I still see the warning posted above re: ipykernel package needing to be installed.
Where am I going wrong?
Here's my attempt at an explanation of the solution. I'm sure someone understands this better and can explain it better (please do and I will pick your answer as the solution!):
VS Code uses one python interpreter for .py files and terminal and a different python interpreter for Jupyter notebook (.ipynb) files. The kernel I wanted wasn't there, and it was using the system installation of python, which is why I was getting this message (ipykernel not installed to system environment, and I don't want it there), while the virtual env python (which has ipykernel) was being used for .py and terminal in the same project, hence the confusion.
To 'install' the kernel for Jupyter, I activated the virtual env in VS Code powershell terminal. Then ran:
python -m ipykernel install --user --name=project_name
Then exit VS Code, restart, open project folder and in upper right corner of Jupyter notebook, use the kernel picker to select the kernel. The kernel I just created wasn't there, so I used CTRL+SHIFT+P in VS Code, then type Jupyter: Filter Kernels, and there I could see the kernel I created, and it was checked, then went back to kernel picker and was able to select the correct kernel.
So now the .ipynb code has access to all the packages installed in this virtual environment.
One annoyance is VS Code doesn't 'remember' that this is the kernel I want for this project, so I have to select the kernel each time I open this project. I'm sure there's a solution for this, but I'll have to look for that later.

Jupyter notebook and anaconda environment bug

I located a really weird behavior of Jupyter and caused me to reinstall it but the problem persisted.
I have a Windows machine where I have installed anaconda (the latest version). There are two environments:
(base) Which contains nothing special maybe seaborn pandas etc. and is on python version 3.8.12.
(tf-gpu) Which contains tensorflow-gpu and is on python version 3.9.7.
Mind you, these two environments are created directly from a fresh installation of anaconda, and I didn't touch anything except installing tensorflow-gpu.
When I launch Jupyter through Anaconda, it launches normally on the home folder "C:\User\user" for both environments i.e. correct packages installed and correct version of python on both. I can also launch an instance of Powershell through Anaconda or Windows, (always in home dir), and launch Jupyter both environments behave as they should (packages, python version). So far so good...
The problem starts when I want to launch Jupyter from a directory other than home dir. I keep all my project files in a separate partition (D:), thus I navigate to that directory through a cmd/powershell (launched through Anaconda or Win) and type "jupyter notebook". The notebooks open on that director and:
When the base env is selected the notebook reports a correct name "base"(os.environ['CONDA_DEFAULT_ENV']) and python version 3.8.12 (sys.version_info). Everything behave as base env was setup.
When the tf-gpu env is selected the notebook reports a correct name "tf-gpu" but I get a python version 3.8.12, which is completely wrong! It should be 3.9.7. I can't import tensorflow since I get an error. Essentially I am in the base env without knowing it.
I then check in the same cli window (tf-gpu) that launched jupyter and python --version reports 3.9.7 and of course can import tf run it etc.
Jupyter just reports the environment name but in reality it uses the base environment even though it launched from said env. How can this change?
Why does Jupyter works normally when launched from the user directory?
If this is not solved easily, what is a workaround? Jupyter doesn't recognize symbolic links, and thus I can not navigate to another directory if it is not a subdirectory of home.
Finally, I also tried adding the env as kernel through python -m ipykernel install --user --name tf-gpu --display-name "Tensorflow GPU (tf-gpu)", but it doesn't seem to change a thing.
Update 1:
Of course I activate the tf-gpu through conda activate tf-gpu and check the python version and tensorflow (correct results), the problem is when launching jupyter notebook. The notebook reports the tf-gpu env but wrong python version and tf is missing.
Update 2:
After some searching around it seems that jupyter notebook has much trouble selecting a python version if multiple versions of python are installed in the system (in my case 2, 1 on each environment environments). Which is ridiculous if you think that that's why we are using environments for...
The reason you are having this problem I’m guessing is because your launched jupyter notebook from your conda base environment. You will see a (base) in your comment prompt.
To remedy this, When you use the command prompt to launch, do conda activate tf-gpu before you launch Jupyter notebook, now check if your versions are correct.
What I did and worked was run ipython3 notebook in the environment which launched the notebook server and everything was working properly correct environment name and correct python version and packages (tensorflow is working as it should). This must have updated some configuration files since I can now run jupyter notebook from a different folder and the gpu-tfu env works correctly.
Please remember that these problems were occurring only if I was launching jupyter outside the home directory (C:\User\user) on a different partition. Also keep in mind that this is a Windows installation and more complicated things may be happening with user paths, environments, etc than in linux.

Import arcpy to spyder

I am trying to work with Arcpy from Spyder, but when I import the Arcpy package, I receive the following message:
ModuleNotFoundError: No module named 'arcpy'
I tried to install Arcpy package 2.7 (my ArcMap and IDLE version) through Anaconda prompt:
conda install arcpy=2.7 -c esri
but I wasn't successful.
So far I've been working with IDLE, but what I really want is to work with arcpy directly from Spyder. Specially to extract vector and raster data (geometry coordinates, atribute table values, etc), so that I can work with it afterwards.
Is there any way of achieving that?
Thanks in advance.
You could try switching environments to the directory where your current python environment is located. For example, if you are using ArcGIS Pro your *.exe file is normally found here: C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3
https://docs.spyder-ide.org/current/faq.html#using-existing-environment
To work with an existing environment in Spyder, you need to change Spyder’s default Python interpreter. To do so, click the name of the current environment in the status bar, and then click Change default environment in Preferences.
This will open the Preferences dialog in the Python interpreter section. Here, select the option Use the following Python interpreter, and use the dropdown below to select your preferred environment. If its not listed, use the text box or the Select file button to enter the path to the Python interepreter you want to use. See the IPython Console for more information.
Click Restart kernel in the Consoles menu for this change to take effect.
Or you could install Anaconda, which comes with Spyder, and create a new environment. That way you could start fresh and install whatever packages you like, including arcpy. conda install -c esri arcgis
This cheat-sheet is handy once you've installed Anaconda.
https://docs.anaconda.com/anaconda/install/
Edit:
Since you're still using ArcMap v.??? try pointing to your python environment that comes with the ArcMap installation from the Spyder Preferences menu. Sorry, I've moved on from ArcMap and Python 2.x years ago (wink).
https://desktop.arcgis.com/en/arcmap/latest/analyze/python/importing-arcpy.htm#ESRI_SECTION1_5E64CCAB40C24B0DB1ED80EF96176F73
ArcGis Pro with Python 3.x have Anaconda and Spyder installed. Older ArcGis Desktop with python 2.x Do not have Anaconda/Spyder installed. I think it's impossible to install ArcPy as library. It's only working within python environment delivered with ArcGIS.

Anaconda Environment on VS Code

In general I am new to python so keep it easy. I have started my coding in Spyder since it was easy to install libraries with Anaconda.
Now we want to switch to VS Code for version control. I have switched interpreter and python path to Anaconda folder, but if I open python files I have created in Spyder I can't use in VS code, issue always is with importing libraries.
Screenshot
The first rule with Anaconda is to not manually mess around with the paths. Anaconda has a different philosophy: Before you can use Python, you need to activate a conda environment.
c:\> conda activate
(Anaconda3) c:\> python
>>>
VSCode detects conda environments and you just have to select one for your project (Python: Select interpreter). Now open a new terminal and watch VSCode activating the environment for you (like shown above).
However, when you start your script for the first time, VSCode sometimes invokes the interpreter even before the conda activation has finialized. Then simply restart your script.

Pycharm jupyter notebook wsl: Jupyter package is not installed

I would like to use Jupyter notebook inside Pycharm. The project interpreter is a python2.7 from a virtual environment inside WSL (ubuntu 18.04).
The Jupiter package is correctly installed inside the virtual environment (I can run it by jupyter notebook).
My problem is that when I want to use Jupyter notebook inside Pycharm, I get the following error: Run Error Jupyter package is not installed (see picture).
Any idea what's going on here?
I had this problem in Python 3. Below are the steps I took to resolve the issue; I believe they should resolve the issue for you too:
I had Jupyter Lab installed. Pycharm only works with Jupyter Notebook. Long story short, if you have Jupyter Lab installed you need to uninstall all your packages using:
$ pip freeze | xargs pip uninstall -y
Restart your computer
Follow Jupyter Notebook installation instructions
Make sure WSL is set up through pycharm instructions: wsl pycharm instructions
In Pycharm, open an .ipynb file. Click the dropdown that says "Managed Jupyter server" It's right above the text editor. Select "configure Jupyter server". Check configured server.
In your wsl terminal, type jupyter notebook. Copy and paste the text that looks like: http://localhost:8888/?token=874asdf687asd6fasd8f74ds6f4s9d8f7sddf into the cofigured server box in Pycharm.
That's it. You should be able to run the jupyter cells in pycharm now.
I have Pycharm 2020.3 For me the issue was I was using a virtual environment with "inherit global site packages." I had Jupyter installed in global site packages but NOT the virtual environment.
Once I installed Jupyter within the virtual environment Jupyter notebook worked. Not sure why inheriting Jupyter from global packages wasn't working for me.
The above solution using a designated url with token seems to work with older versions of PyCharm. A simpler solution is to upgrade to the latest PyCharm. I no longer had an issue with the auto server using PyCharm 2019.3.2 (Mac)
I had this problem with Datalore plugin enabled on 2020.2 linux, running on bare metal but displaying to a remote X server (probably doesn't matter). My solution was to disable the Datalore plugin (it's enabled for professional pycharm by default).
This way I was still able to use the "managed" auto-start version with better integration / debugging vs the "configured" option (or at least with less hassle).
Note since it's been a year, my problem is probably different than OP.
This happened for me, when the interpreter was a remote one. I fixed this by changing the interpreter to one from a local env.
This can be done by selecting the Configure Jupyter Server.
I also meet this problem,and i solved it
i create the new project with the global sit-packages like below
then i meet the problem
i create the new project with no global sit-packages and install jupyter notebook in the virtualenv
then the problem is gone

Categories