How to show python environment objects in a pane in VSCode? - python

In R Studio, Matlab, and in some Python IDEs (like Thonny) you can see a list of the variables in your environment, together with some summary information like values, type etc.
Is there a way to do this in VSCode without going into debugging mode?

Seems like the closest as it gets to the RStudio Environment window is by installing Jupyter. Then you can type #%% in your .py file and a small "Run Cell" button will appear. Running the cell will open the "Python Interactive Window". There you can click the "See Variables Active" button to open up the Variable Window and see your variables... still a bit painful in comparison with RStudio, this should be integrated in VScode's IDE.
See steps here:
https://code.visualstudio.com/docs/python/jupyter-support-py#_variable-explorer-and-data-viewer

VS Code has an IPython mode with a dropdown box called Variables that basically does exactly this.
To enter IPython mode all you need to do is highlight a piece of code and hit Shift+Enter
The first time you do it there might be a bit of setting up to do but it was pretty easy to get it working on my Mac. Not sure what it's like on Windows/Linux though
Note: When I use VS Code I open it from the terminal (simply enter the command 'code') after I've activated my conda environment. That way the IPython interpreter uses my preferred conda environment when it launches.

Related

How to change script execution location in vs-code?

I want to run my Python project on my globally installed Python interpreter.
I don't want to be in the "Mosh Python Standard Library" virtual environment anymore but I can't figure out how to exit it.
This what is shown in my output window in VS Code:
Here is a picture of my VS code terminal, the venv doesn't appear to be active:
Sorry for any confusion in my post, I am new to programming.
You can go to the vs-code's command pallet and type in Python: Selected Interpreter. This will open a window which you can choose which interpreter you want your python files to debug. Or you can click on the bottom left corner's button and it will do the same.
It turns out my default python interpreter in settings.json was pointing to the virtual environment, so even though I selected the global interpreter in the bottom left it would still run on the unwanted venv

"Run Python File in Terminal" not working

I just pressed that triangle to run the code. I want that button to work because I want to use the debug mode from that button. When I click it, it tries to run three times and it somehow got into anaconda. I checked Python default interpreter path in preferences>settings and it is set to C:\Users\ucanc\AppData\Local\Programs\Python\Python39\python.exe, not the anaconda one. I am pretty new to this and I do not know what is going on.
edit: It seems like it is running as WSL termial that is why it has name#LAPTOP-xxxx:/mnt/c/ in the front.

How to switch environments for open jupyter notebook

If open a python jyputer notebook in vscode, then try to execute the notebook (so a kernel starts) and realize I need to activate a different environment, there doesn't seem to be a way to switch environments without completely quitting VSCode and coming back in.
If can switch environments in the terminal window or using the environment picker in the lower left, but it there doesn't seem to be a way to quit the current kernel and have it re-launch under the new environment. I've tried using the "Restart Kernel", closing the notebook and re-opening.
Is there a way to do this?
I'm a developer on this extension. The intention was for this feature to function as the Python Interactive Window did, where you could use the VSCode Interpreter picker to change out an open notebook to a new kernel. However it appears that we have a hitch that we shipped with this functionality. There is already an issue open in our github here which I believe that you filed (just going on user name match :)):
https://github.com/microsoft/vscode-python/issues/7862
That will be the right place to track our progress on fixing this, and it's on our immediate backlog.

Command window in Pycharm

Is there a interface in Pycharm where we can simply type in some commands and run it ?
Something like in Matlab, we can type in "a = 1; b = 2; c = a+b" then we get ans=3.
Thanks
PS: we know we can create a python file in Pycharm and run it, e.g., "a = 1; b = 2; c = a+b; print(c)" but it is not as convenient as a command window.
Short answer from the docs:
To launch an interactive console
On the main menu, choose Tools | Run Python console.
Description:
REPL console
PyCharm also helps those who love the full control of an
interactive console: on the Tools menu, you can find commands that
launch the interactive Python or Django consoles. Here you can type
commands and execute them immediately. Moreover, PyCharm's interactive
consoles feature syntax highlighting, code completion, and allow
viewing the history of commands (Ctrl+Alt+E or Up/Down arrows while in
the editor).
PyCharm also makes it possible to run in console source code from the
editor — just make your selection, and then press Shift+Alt+E (Execute
selection in console on the context menu of the selection).
Independent of Pycharm, you can also access the REPL in the terminal (or cmd shell on Windows) by typing
python
at the prompt.
In PyCharm, you navigate to the View -> Tool Windows menu and toggle the Terminal window See image here. It will likely show up at the bottom of your IDE window.
Another very good (and more general option) is to use IDLE. From its Wikipedia entry...
IDLE is intended to be a simple IDE and suitable for beginners, especially in an educational environment. To that end, it is cross-platform, and avoids feature clutter.
It ships with basically every standard version of Python since 1.5.2 so I'm pretty sure you have it available on your system (I've checked with Linux and Mac OS X).
To fire up IDLE in Python2, enter: /path/to/python/bin/idle where /path/to/python/bin is where you find the Python executable.
For Python3, use idle3 instead.
You should see a new separate terminal window open up (with syntax highlighting and all!)See image here.
Find the attached screenshot..
You can observe the o/p of the editor in Run window.
You can also find Python Console and Command Terminal at the bottom of the Pycharm.

use ipython to get REAL code-completion in pycharm

Many python IDE's boasts of providing code-completion (code insight), PyCharm is one of those IDE's. However, it seems to me that the provided code-completion is extremely limited. Let me give you an example to make it clear:
import numpy as np
m = np.random.random((3,5))
m.
Hitting CTRL-space after 'm.' will not give me any code-completion, -no matter how hard I hit it ;).. I guess this is because the IDE would have to do type inference to know the type of the variable 'm', and that this isn't trivial in the domain of dynamic programming languages.
Now, PyCharm comes with a setting called "Collect run-time types information for code insight", which indeed sounds promising. However, it doesn't seem to fix the problem mentioned above.. I am still not able to get code-completion on the variable 'm'.
Thus far, I have only found one way to get code-completion on variables in PyCharm:
import numpy as np
m = np.random.random((3,5))
''':type : np.matrix'''
m.
In this example I am able to get code-completion when pressing CTRL-space after 'm.', and this is because I am helping the IDE by specifying the type of the variable with a docstring. I am, however, not satisfied with this way of getting code-completion, because it adds unnecessary verbosity to the code with all these docstrings (not to mention all the extra keyboard-typing)...
IPython to the rescue.. (maybe?)
Now, if we start IPython in a linux-terminal, and enter the first piece of code, we will be able to get code-completion all the way, -even for the variable 'm'. (where the code-completion in IPython is achieved by pressing TAB instead of CTRL-space)..
I don't have much experience with IPython, but I believe that I've heard something about IPython constantly executing the code in a loop or something like that...
I am thinking that it should be possible to use IPython to achieve REAL code-completion on all variables in the editor of PyCharm....
Is there a way to setup PyCharm to use IPython for code-completion?
Note that I am not satisfied with sending the code to a terminal window/console, or something like that, I want code-completion inside the editor of PyCharm...
I have looked at questions like this Adding ipython as an interpreter in Pycharm Ubuntu, but it seems to be about using IPython in the console, -not in the editor... There are also plenty of questions talking about code-completion in IDE's, but they all seem to have the same unsatisfying level of code-completion as PyCharm...
My setup
OS: Debian testing
python: Python3 and IPython3
IDE: Pycharm 3.0.2 professional edition
It cannot tell what returns are from functions(with out actually running the script, thats why ipython knows what it is (it has actually run the code and recieved back an object it can introspect)
if you want code completion without having to actually execute your script up to where you are entering text you have to do an extra step
import numpy as np
m = np.random.random((3,5))
assert isinstance(m,np.ndarray)
m. #now you get code completion (since the IDE now knows the class of m, without having to execute your script)
I had the same question, I found the answer here:
https://www.jetbrains.com/pycharm/help/using-ipython-notebook-with-pycharm.html
What you need to do is to create a ipython notebook in the project tool window. This is a file with the extension '.ipynb'. Right click on the directory where you want to put the file, select 'New-->File', enter a file name in the dialog box and give the file extension .ipynb. Open the notebook file and when you start to type something in the window, a drop down window appears with objects in the namespace plus any commands that start with the letters already typed.
To achieve iPython like functionality in pycharm, you can do it two ways:
setup a breakpoint, and debug your code. when it reaches the breakpoint, go to the console tab right below the editor, and launch a command line by clicking the button that says show command line
Launch a python command line from your console (without running debugger), by clicking on Tools, Run Python console

Categories