How can I test commands in Python? (Eclipse/PyDev) - python

I just setup PyDev with Eclipse, but I'm a little confused. I thought that in the console I would be able to type certain commands such as print("Hello World") and directly observe the result without having to incorporate that in any sort of file.
The reason I would like this is because it would allow me to test functions real quick before using them in scripts, and I'm also following a tutorial which tells me to check if NumPy is installed by typing import NumPy in the command line.
Thanks!

There should be an interactive console in PyDev.
Try Ctrl+Alt+Enter or Cmd+Alt+Enter.

Open up terminal and type python then it should load python shell then type
import numpy
I have used pydev and find its easier just to use terminal to run small commands

Related

Giving interactive control of a Python program to the user

I need my Python program to do some stuff, and at a certain point give control to the user (like a normal Python shell when you run python3 or whatever) so that he can interact with it via command line. I was thinking of using pwntools's interactive() method but I' m not sure how I would use that for the local program instead of a remote.
How would I do that?
Any idea is accepted, if pwntools is not needed, even better.
Use IPython
If you haven't already, add the package IPython using pip, anaconda, etc.
Add to your code:
from IPython import embed
Then where you want a "breakpoint", add:
embed()
I find this mode, even while coding to be very efficient.

How to do debugging in a Python console without reloading the package in PyCharm?

PyCharm runs an interactive Python console (IPython in my case), but when I make changes in the code, PyCharm doesn't reimport the modules I've been editing, so the console runs the old code.
More so, if I have an old package installed via Run setup.py Task, Python imports the old one after import mymodule as mm in the console.
One workaround is to edit the code in a file and rerun it without the interactive console, but that's not a very elegant solution.
How can I keep the interactive console up-to date and update modules on-the-fly?
I'm using Python 3.4.3 in Pycharm 4.0.6 currently, and building on Anton's answer above, I add the following to my Starting script:
from importlib import reload
then you can simply use 'reload(my_module)' as you please - however this would be manual for each reload and I'm not aware of a fully automated solution if that's what you're really after.
IPython has a very nice function that recursively reloads all modules. It is called dreload, from deep reload. You just have to manually run in the console:
dreload(myCoolModule)
It is manual, but it doesn't bothers me. If I remember well, it doesn't work if you remove the namespace importing like: from myCoolModule import *

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

How do I create a custom python interpreter? i.e. with certain modules already included?

If you've used Ruby on Rails, I'm thinking of the feature where the user types
'rails console'
and instantly gets a Ruby console with rails and the current app already loaded.
I want to make something like this for a python program I'm working on, does anyone know how I would get to type say,
'python myPythonConsole.py'
and open up a regular python interpreter but with my program and all its dependencies loaded?
If I understand you correctly then you might want python -i myPythonConsole.py. It gives you a console when the script has finished so you have to run your application in a different thread.
To create a console in a script you would use the code module.
If you are using IPython (if you are not you should, it is an awesome python shell with TAB completion and many shortcuts) it is possible to set up profiles, which basically are named configurations.
Each configuration can import modules (and do other stuff) at startup.
Django does this with its "shell" command:
./manage.py shell
will open a Python shell with your Django settings loaded so you can import your project code interactively.
Source: http://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py
The real answer here is to use the PYTHONSTARTUP environment variable. See the tutorial section The interactive startup file.
Do your custom imports in a file interpreter.py, and configure
PYTHONSTARTUP=/path/to/interpreter.py
Next time your start Python, the custom code will be executed before you're dropped in the REPL shell.
Here's my customization:
import os
import sys
from pathlib import Path
from pprint import pprint
pp = pprint
P = Path
version = ".".join(str(number) for number in sys.version_info[0:3])
print(f"\nCustomized Python interpreter v{version} running from {sys.prefix}")

How to pause python execution in eclipse and return to an interactive prompt

I am using Eclipse as a Python IDE. Is there anyway for me to Debug my program and break to an interactive prompt. I am interested in exploring the existing data and running/testing commands.
I believe there has to be a way, but I am so used to compiling languages that I have not been able to find where the options are.
Any ideas?
You can easily do that by using PDB (Python Debugger) inside a python shell.
Look at http://docs.python.org/library/pdb.html for more info.
Anyway I believe Eclipse will let you inspect you data when setting a breakpoint.

Categories