Line number of edit window not showing in interactive window using Visual Studio and conda enviroment - numbers

I created a python project with Visual Studio, expecting to develop with conda and ipython, so I set the enviroment to Conda, and made sure to check the ipython within it...
I am expecting the following result, which has references to line numbers in the interactive window:
Turns out that i am getting this (without the line number):
What am I missing to achieve the desired result?

For this walkthrough you should have the Anaconda environment installed, which includes IPython and the necessary libraries:
Open Visual Studio, switch to the Python Environments window (View >
Other Windows > Python Environments), and select an Anaconda
environment.
Examine the Packages (Conda) tab (which may appear as pip or
Packages) for that environment to make sure that ipython and
matplotlib are listed. If not, install them here. (See Python
Environments windows - Packages tab.)
Select the Overview tab and select Use IPython interactive mode. (In
Visual Studio 2015, select Configure interactive options to open the
Options dialog, then set Interactive Mode to IPython, and select
OK).
Select Open interactive window to bring up the Interactive window in
IPython mode. You may need to reset the window if you have just
changed the interactive mode; you might also need to press Enter if
only a >>> prompt appears, so that you get a prompt like In [2].
https://learn.microsoft.com/en-us/visualstudio/python/tutorial-working-with-python-in-visual-studio-step-01-create-project?view=vs-2019

Related

Does VS code have variable explorer object like we have it in spyder?

I will post the picture of what exactly I am asking variable explorer in spyder
So do we have this feature in VS code?
I tried a lot to find it on google but was unhappy to not find it.
Open your .py script in vscode
Right click anywhere on the script > Run current File in interactive Window
In the toolbar of the interactive window click on the variable icon
You can now consult the values of variables created by your script
spyder is probably running a REPL (Jupyter is doing that also). From that python process they show the local and global variables, just like a debugger would do on a breakpoint.
If you use Python Interactive you have similar functionality with the Variables Explorer and Data Viewer or use Jupyter notebooks
You can now find all variables in a Jupyter Notebook in VS Code in the Output panel under Jupyter: Variables

Intellisense autocomplete not working for wsl in vscode

I've been setting up vscode as an IDE for python development. I use windows subsystem for linux with my python packages installed through anaconda there.
My problem is that auto completion does not work as expected. For examples, outside of wsl if I am using the numpy package I can get autocompletion:
Autocomplete outside of wsl
However, inside of wsl autocomplete does not find numpy.linalg (as in example above):
Autocomplete inside wsl
Thanks in advance!
edit: I am currently using wsl1 if that information is relavent!
As mentioned by #Adriana Hernández you need to install the extensions inside WSL too.
The VSCode WSL docs are covering these here
In short if you go to the extensions tab, you shall see Python marked with "Install in WSL...". Once clicked install and reloaded, it should work.
If not you likely need to pick another interpreter (likely the one from your venv.)
hit Ctrl+Shift+P then
Python: Select interpreter
then select the desired python interpreter (most likely the one in your venv which has all the packages)
What works for me is creating a venv, activating then launching VSCode from WSL with code .
On the first time you will need to install your extensions to WSL, but then it should be good to go.

Jupyter notebooks in Visual Studio Code does not use the active virtual environment

I write Python code in Visual Studio Code and run the program from a terminal in which I have activated a virtual environment, and it works fine.
However, if I create notebook cells using #%% and run those interactively, the virtual environment is not used. How can I fix this?
It's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.
Follow the advice here: Using Jupyter notebooks with a virtual environment
And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:
From: ipython kernel install --user --name=projectname
To: python3 -m ipykernel install --user --name=projectname
(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)
*and don't forget to restart VSCode
All you need is to edit Vscode settings following these steps:
Open Open User settings using shortcut Ctrl + Shift + P
Type in search space "env"
Under Extentions -> Python , you will find Python: Venv Path
Type the absolute path to your enviroment "path/to/myenv/bin" in linux or "path/to/myenv/Script/"
Restart vsCode
Select the desired kernel using Notebook : Select Notebook kernel using shortcut Ctrl + Shift + P
Read more here: https://techinscribed.com/python-virtual-environment-in-vscode/
For VSCode, your Jupyter kernel is not necessarily using the same python interpreter you're using at the command line.
Use Ctrl + Shift + P to open the Command Palette, and select "Notebook: Select Notebook Kernel"
Then choose the interpreter you're using at the terminal
I find it easy to use pipenv install ipykernel to set up the virtual environment with the Jupyter kernel in one go (comment on rocksteady's answer).
Encounter the same behaviour. Python code works perfectly fine, but Jupyter refuses to pick up the local .venv.
The local venv is in Python: Select Interpreter but not in Jupyter's Select kernel list.
The problem is there're too many venv in the system!
If you encounter the same behaviour,
Press F1, then Jupyter: Filter kernels, uncheck everything, except the local env.
Then F1 -> Developer: Reload Window.
Jupyter will automatically use the default local venv.
for me solved by adding the path of my venv to the settings.json,
now the kernel is detected automatically
"python.pythonPath": "P:\Miniconda3_64bit\venv\Scripts\python.exe",
Make sure you have installed jupyter, notebook, ipykernel libraries in your virtual environment.
Then hit Ctrl + Shift +P , press >Python: Select Interpreter and choose your path of the venv.
After that, hit Ctrl + Shift +P again, run >Notebook: Select Notebook kernel.
If you have already opened the jupyter notebook window , reload it again and you can find your path of the venv in Jupyter's Select kernel list.
On macOS I have .venv/ in the same folder as my .ipynb
. .venv/bin/activate
pip install ipykernel
Then I restart VSCode in the project folder, open the notebook, and in the Select Kernel dropdown I see .venv/bin/python
Selecting that, now it works.
Here is how to do for venv with Jupyter Notebook on VSCode in Windows:
Create a venv and get the path to this venv in Windows. As an example, with Anaconda, I get: C:\Users\rascoussier\Anaconda3\envs\research310.
Now, we need to tell VSCode to use it. In VSCode, go to the Python Extension > Extension Settings. Search for Python: Venv Path. Add the path where the venvs are located. Here we added C:\Users\rascoussier\Anaconda3\envs\research310.
Restart VSCode.
Now launch command pallet(ctrl+shift+P) and run >Notebook: Select Notebook kernel. Normally the venv python should be available and it should then works.
Try a few things:
Make sure you can run the code from a Visual Studio Code terminal using the "ipython" prompt with the same Conda environment.
If it works then sometimes it is a caching issue, so close your file and open a new one.
Now let me show you a scenario. You select the interpreter in Visual Studio Code, and then you write codes below '# %%'. The moment you hit Ctrl + Enter, you are guessing that the IPython kernel that Visual Studio Code is using is not of the interpreters that you have selected. In this case you could write the following code to conform which interpreter is used for IPython kernel.
import sys
print(sys.executable)
This shows the executable path that the IPython kernel is using. If you see that it's not taking the correct interpreter then here's something that worked for me.
Just restart your computer. Then reopen Visual Studio Code and reselect the interpreter and again hit Ctrl + Enter. Now this time Visual Studio Code should take the correct interpreter and its IPython kernel.
See the final output image
If this happens while using WSL server, don't forget to install Python in the WSL as well, because it doesn't come automatically from the local installation to the server.
https://code.visualstudio.com/docs/remote/wsl-tutorial
Another alternative is to specify the folders where the environmental variables should be sought for.
Create your virtual environment using conda create --name ENV_NAME e.g conda create --name pwd
Then, conda activate pwd
It Should print out details like this:
Use that environment location
Edit Vscode settings following these steps:
Open Open User settings using shortcut Ctrl + Shift + P
Type in search space "env"
Under Extentions -> Python , you will find Python: Venv Folders
(See the image below)
Try installing the Anaconda Extension pack.
When I code in Visual Studio Code with this extension in the bottom left corner, I can select the virtual environment I want to execute my code in. Hence installing this package should make the trick.

how to open an IPython console connected to an exiting running kernel in PyCharm

Is there a way to open an IPython interactive console in pycharm that is connected to an existing running kernel (similar to "python --existing")?
btw: in case it's relevant, in my case, the running kernel is of a Jupiter notebook...
EDIT: To clarify, my question is NOT about how to open an interactive console in PyCharm. It is about how to connect that interactive console to a an existing running (Jupiter notebook) Kernel.
(tl;dr: Use jupyter console --existing in the PyCharm "Terminal" tool window (not the "Python Console" tool window) to connect to an existing iPython kernel running in a local Jupyter Notebook server.)
I can confirm that the comment by #john-moutafis suggesting ipython console --existing is the right idea. The command gives "WARNING | You likely want to use jupyter console in the future" so I tried that.
I have a project using a conda environment as its interpreter. Jupyter Notebook is installed in the conda environment.
I open the Terminal tool window. It automatically activates the conda environment.
I type jupyter notebook. The notebook server starts and a browser window opens.
I create a notebook in the browser, and execute a cell containing foo = "bar".
In PyCharm, I open another Terminal tool window by clicking the plus sign to the left of the terminal pane.
In the new terminal I type jupyter console --existing, and it starts an ipython console session.
At the prompt I type dir(), and foo is among the results, confirming that I'm attached to the same kernel as the notebook.
I don't know how it picks which kernel to connect to when there are multiple kernels running in the notebook server.
Don't type exit in the iPython session if you plan to continue using the notebook, it shuts down the kernel.
Unfortunately, tools like Debug and "Execute Line/Selection in Console", which are available for the "Python Console" tool window, are not available for the "Terminal" tool window. In fact, because the Terminal tool window is a simple tool, and that's where I've run my commands, this solution isn't very integrated with PyCharm. The terminal opens in the project directory and activates the conda environment, and it's conveniently adjacent to the editors and tools of the IDE, but otherwise there's no connection to PyCharm's tools.
If anyone can successfully attach PyCharm's integrated PyDev debugger to a running kernel, please chime in.
I'm using PyCharm 2016.3 on macOS 10.12.3.
The easiest way for me is just to type %qtconsole in a jupyter notebook cell and run it. A qt console will open already connected to the running kennel. No PyCharm involved.

IPython Autocomplete in PyCharm Run Window

I am coding in PyCharm Community Edition and making use of the IPython embed function to debug my code. When I run the code, the code stops on the line I have embed and the IPython interactive shell appears at the bottom of PyCharm in the "Run" window. I can inspect my variables and run commands, but all the IPython autocomplete (i.e. tabs) and up and down keys (to retrieve previous commands) don't work anymore. Does anyone know how to get this working from within PyCharm?
Thank you.