use ipython to get REAL code-completion in pycharm - python

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

Related

Open Python interactive session and editor

I am looking for some pieces of advices in order to accomplish a tiny task regarding Python. If someone would ask to provide a pic of a 'started interactive session of Python with your favorite editor with a Python script', what would you show to this person? Should it be a void script? How do you interpreter 'started interactive session'? How about your own favorite editor (I mean that you would suggest for Windows 10)?
Sorry for the triviality of my question,but I have just started with beginners' Python course
Just to make sure I am on the right way, if I have to submit to someone else a started interactive session and your favourite editor with a Python script, will be it sufficient to show the following windows as in the picture?
If you want to do interactive things, you probably just want to use jupyter notebook: https://jupyter.org/install#jupyter-notebook
You can always just type python at your terminal prompt if you have python installed, this will start an interactive session in your terminal, but jupyter is definitely easier to use once you get it set up.
Edit: regarding favourite editor, this is very much opinionated but I love sublime text. https://www.sublimetext.com/
Note that you probably would use one or the other: you would use a text editor to write scripts that could be run in a terminal, for example
# hello_world.py
print("Hello, world!")
then in your terminal
python hello_world.py
whilst you would use a jupyter notebook for example for quick experimentation or for demonstrating usage of your software to others.
In terms of interactivity, you should really check out Jupyter Notebooks. It's industry standard for a lot of tasks, widely used and with great performance and support. Also, Jupyter has an in-built code editor than can be run via localhost in your browser.
However, for a code editor, I will never stop recommending VSCode. Huge game changer, light-weighted and with support for pretty much any language. Jupyter notebooks can be run directly from VSCode, and the latest features that Microsoft introduced in this regard make using Jupyter inside VSCode really easy and intuitive. Also, extensions are a positive point as well.
If you're only planning on programming with Python, maybe checking out PyCharm is also a good idea, but I have working mainly in Python for the past 3 years and I have never missed anything on VSCode, even though I've tried PyCharm several times.
The simplest answer: go with the IDLE IDE, it comes bundled with Python by default. It starts with an interactive session, so you just type at the prompt (>>>):
print ("Hello, world!")
and your task is done, if it prints out your "Hello, world!" line.
In general, you start your python interactive session (python REPL, python terminal, python console, there are several terms for the same thing) and, since it's an interpreted language, everything you type in, Python will interpret and execute, if valid. Nevertheless, except for atomic examples, you'll want to use a code editor or IDE.
As a beginner, I'd avoid jupyter for the time being, it could get cumbersome. Stick with the default IDLE editor (you open a new file and type away) until you feel confident, then make the switch toward some editor or IDE that you fancy - Visual Studio Code is popular and has python debugger, vim is old as Bible, can run on a calculator, but it's a modal editor, best used with touch typing, Notepad++ is also good for coding...

Pycharm: Using Jupyter instead of prompt for debugging

When debugging a .py file within pycharms debug mode i can interact using a python prompt when hitting a breakpoint. Is there any way to use the current state of pycharm within a jupyter notebook istead of the python prompt?
It would make debugging quite a bit easier since you could reuse code snippets for debugging purposes.
I couldn't find anything about it, but for me it feels like it could be a thing.
Thanks for any help!
Based on this post, the functionality you are looking for does not exist. The key piece here is that you're trying to import an existing python interpreter into a new IPython kernel. However, from the post linked, you can attach a notebook to an existing IPython kernel with a little bit of work.
A possible solution would be for you to switch to using an IPython interpreter in PyCharm, then attach to that exising kernel in a notebook when needed (as described in the section above).

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

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.

VScode run code selection

I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

How to run code with Hydrogen

I am completely new to Atom.
I installed it and it felt quite easy to use and set up. I read that the Hydrogen package enables functionality similar to the Jupyter Notebook. So I installed the package.
Unfortunately, I have no Idea how to use it. I read the entire documentation (which isnt too extensive) and searched for everything I could.
So here is my problem:
I created a file called testfile1.py
In that file i put the very simple line
print(‘Hello’)
just to see how it works. I marked the line and pressed Ctrl+Enter. At the top right, a window pops up saying “Hydrogen Kernels updated: Python 3”.
But then nothing happens. I dont see the result of the code that I tried to run anywhere. I tried different lines of codes, tried differen run-combinations, nothing gives me any results.
I am using arch linux, installed Anaconda through the AUR to /opt/anaconda.
Using the terminal and running
jupyter notebook
for example works just fine and opens a Notebook in Firefox (as it should) and running code that imports modules that came along with Anaconda also work fine once i run them with the script package in Atom
(things like
import numpy as np
is letting me work with all the numpy funtions as expected. So I think that it shouldnt be any issues related to the Anaconda packages itself)
I tried to look everywhere I could, but I do not find any solution on why hydrogen would not give me any results.
Is there anything I am missing or did wrong?
I hope someone might be able to help me, thanks already in advance
I'm using Windows 10, Atom version 1.30.0 with Hydrogen 2.6.0 (both with default settings).
If you navigate to Packages > Hydrogen you can see the default key bindings.
To make a cell there are many options noted in the manual as shown below.
I found I had to re-start Atom after updating the python kernel. Then the run code commands worked as expected.
You need to select all the lines which you want to execute with the cursor and then press ctrl+enter (or shift+enter depending on your keybinding).
There are two ways to tell Hydrogen which code in your file to run.
Selected code:
If you have code selected when you hit Run, Hydrogen will run exactly that code.
Current block:
With no code selected, Hydrogen will try to find the complete block that's on or before the current line.
If the line you're on is already a complete expression (like s = "abracadabra"), Hydrogen will run just that line.
If the line you're on is the start of a block like a for loop, Hydrogen will run the whole block.
If the line you're on is blank, Hydrogen will run the first block above that line.

Categories