Run python script then enter interactive session in ipython [duplicate] - python

My use case is I want to initialize some functions in a file and then start up IPython with those functions defined. Is there a way to do something like this?
ipython --run_script=myscript.py

In recent versions of IPython, you do need to add the -i option to get into the interactive environment afterwards. Without the -i it just runs the code in myfile.py and returns to the prompt.
ipython -i myfile.py

Per the docs, it's trivial:
You start IPython with the command:
$ ipython [options] files
If invoked with no options, it
executes all the files listed in
sequence and drops you into the
interpreter while still acknowledging
any options you may have set in your
ipythonrc file. This behavior is
different from standard Python, which
when called as python -i will only
execute one file and ignore your
configuration setup.
So, just use ipython myfile.py... and there you are!-)

You can use IPython profiles to define startup scripts that will run every time you start IPython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
Assuming you only need one profile, and always want the same startup files every time you start IPython, you can simply modify the default profile. To do this, first find out where your IPython configuration directory is in an ipython session.:
In [1]: import IPython
In [2]: IPython.paths.get_ipython_dir() # As of IPython v4.0
In [3]: print(ipython_config_dir)
/home/johndoe/.config/ipython
For this example, I am using Ubuntu Linux, and the configuration directory is in /home/johndoe/.config/ipython, where johndoe is the username.
The default_profile is in the profile_default subdirectory. Put any starting scripts in profile_default/startup. In the example here, the full path would be /home/johndoe/.config/ipython/profile_default/startup.

Nowadays, you can use the startup folder of IPython, which is located in your home directory (C:\users\[username]\.ipython on Windows). Go into the default profile and you'll see a startup folder with a README file. Just put any Python scripts in there, or if you want IPython commands, put them in a file with an .ipy extension.

You seem to be looking for IPython's %run magic command.
By typing in IPython:
%run hello_world.py
you'll run the hello.py program saved in your home directory. The functions and variables defined in that script will be accessible to you too.

The following is for the case when you want your startup scripts to automatically be run whenever you use IPython (instead of having a script that you must specify each time you run IPython).
For recent versions (i.e., 5.1.0) of IPython, place one or more python scripts you wish to have executed in the IPYTHON_CONFIG_DIR/profile_PROFILENAME/startup folder.
On Linux, for example, you could put your Python startup code into a file named ~/.ipython/profile_default/startup/10-mystartupstuff.py if you want it to run when no IPython profile is specified.
Information on creating and using IPython profiles is available here.

Update to Caleb's answer for Python 3.5 in Ubuntu 14.04 (Trusty Tahr): I made this answer self-contained by copying relevant parts of Caleb's answer.
You can use IPython profiles to define startup scripts that will run every time you start IPython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
Assuming you only need one profile, and always want the same startup files every time you start IPython, you can simply modify the default profile. To do this, first find out where your IPython configuration directory is in an IPython session:
Input:
import IPython
ipython_config_dir = IPython.paths.get_ipython_dir()
print(ipython_cofig_dir)
Output:
/home/johndoe/.ipython
For this example, johndoe is the username.
Inside the /.ipython folder, the default_profile is in the profile_default subdirectory. Put any starting scripts in profile_default/startup. In the example here, the full path would be
/home/johndoe/.ipython/profile_default/startup

Related

How can I run a .py file with its options in Python console?

I am trying to run this GitHub project in python, but I could only run it using the Terminal of Pycharm IDE.
According to the guide from the GitHub repository, I removed the $ sign from the beginning of $ python train.py RGCN PPI and could run it there. What does $ mean here and how can I run a file like this in Python Console (for example after >>> sign)?
The '$' isn't part of Python's syntax, it's a visual cue in the documentation representing the command prompt.
To answer the question from the title of this post, I'll provide some
instructions first on how to load scripts into the Python console.
However, for your specific case, you don't need this. Scroll down to
the part about debugging in PyCharm.
There's two ways you can get your script into the console. One is to simply load it using the right version of the two lines I give right below, or you can load it as a module - even if it wasn't intended to be one.
In general, to execute a script in the Python shell on Python 2 you can do
>>> execfile(r"<path to script here>")
On Python 3 it's more verbose:
>>> exec(open(r"<path to script here>").read())
The effect this has is as if you cut-n-pasted the script into the console. The console's global scope will get all the functions, classes, and variables that are leftmost indented in the file. Also it might not run your if __name__ == '__main__': block. But you could hack that.
If you want the vars/classes/etc to be put in another scope than your console's global scope, then there are two additional parameters to the above commands. The first one is a dictionary for the globals , and one for the locals. You can get away with only supplying the globals parameter - it's just an ordinary dictionary object you need.
If the file you want to load is a module, you could import it as you would any other module by appending its home folder to the Python module search path, and using the import directive. You can load your script this way even if it wasn't intended to be module.
>>> import sys
>>> sys.path.append(r'/Users/todd/projects/mymodule_folder')
>>> import mymodule
If you make modifications to it and want to reload it:
>>> import importlib
>>> importlib.reload(mymodule)
Loading your script as a module avoids polluting your console's global scope. After it loads, just prefix the names of your script's functions and variables with the module name. The module name will be the name of the file without the .py extension.
If the script requires command line options, you could just hard code values for those into the script and disable lines of code that try and get values from the CLI. If it gets complicated, consider running it in an IDE as described in the next section.
So the above is how you can run your python scripts in whatever Python REPL console you want.
BUT loading your scripts into the Python console may not be at all
required for your purposes. You wanted to debug some scripts (train.py,
test.py) from this project:
https://github.com/microsoft/tf-gnn-samples).
Debugging Command Line Script With PyCharm
In many cases, a Python script is written to run from the OS shell and take command line options from the user. These kinds of script could be loaded into the Python console, but most require some minor hacks to run. However, if all you want to do is debug such a script, you don't need to muck with the console.
PyCharm supports running these as is (as does Eclipse and other IDEs) like any other script. It's just a matter of creating a run/debug configuration for the project. I just installed PyCharm and gave it a try in order to record the details. Easy task.
Just open the project in PyCharm, and above the editor pane, on the toolbar, there's a menu option for Edit Configurations. Click that to open the Run/Debug Configurations dialog and click the + to add a configuration. A small dialog will appear with predefined templates - select Python as your template and accept.
Then in the main dialog, fill in Script path: with the path to train.py (or another script), then click the checkbox, [x] Emulate terminal in output console. Also, you can add command line options in the Parameters: text box (I put in the text: mymodel mytask just to satisfy the script's need for two parameters). Click OK at the bottom to accept the configuration and shut the dialog.
Now you should see a green bug icon on the toolbar.Set a breakpoint in the __main__ block of the script and click the debug icon to start debugging the script. That should do it!
Debugging Python Command Line Script with PDB
PDB - the Python Debugger can be run without an IDE. This is another way to debug a script of any sort. If it requires command line parameters, provide them from the OS shell when you start the debugger:
$ pdb myscript.py mymodel mytask
That's really all there is to starting a debug session. PDB requires some knowledge of its text based commands. After starting a session, you can get a listing of code near the current line of execution by entering l. Enter help to see a listing of the commands.
To step one line of execution, enter 's' for step, or enter 'step'. To set a breakpoint, enter break <line-number>, or set a breakpoint on an expression. A reference on the commands available can be found here: https://docs.python.org/2/library/pdb.html . There are also plenty of versions of pdb cheatsheets available online - just google "pdb cheatsheet" and select one.

Canopy get list of files in the shell

I'm using canopy python. The shell has commands "pwd" and "cd".
It even does autocomplete.
Is there a command - not something I have to write and keep track of - a built-in command that just lists the files in the directory.
I've tried ls, ls(), dir, dir(), directory, directory(), files, files(), filelist, filelist().
I've searched their documentation for listing files. It shows how to get a list of files in a python program - that's great. I do that all the time. I don't want to do that every time I want to list the contents of the current directory.
Is there a built-in shell command for this?
You can try
import os
fileList = os.listdir(directory_path)
so that you have the list of files in the python program to manipulate.
If you want the list of files in the current working directory in the way that ls would give you you can do.
fileList = os.listdir(os.getcwd())
If you really want a short command to type over and over you can write
ls = lambda : os.listdir(os.getcwd())
and then it will run this command when you type
ls()
You can use the python built in module, os, to query a directory using the system method. Please see below.
import os
os.system("dir")
Update
Please keep in mind that my solution is OS specific (i.e. ls vs dir) so Jon's solution is preferable.
The premise of the question seems inconsistent. If you can do the ipython pwd or cd standard magic commands, then you should also be able to access the ipython standard alias ls. If you cannot, then it almost certainly because you've overwritten / shadowed the meaning of the ls alias.
It's crucial to distinguish between (1) a plain Python shell (opened by typing python at a Canopy command prompt), which does not provide pwd nor cd nor 'lscommands; and (2) *any* IPython shell, whether the Python panel in the Canopy GUI or anipython` terminal in the Canopy Command Prompt, which does provide those, and many other Ipython "magic" commands.
(For more about IPython magic commands, see http://ipython.readthedocs.io/en/stable/interactive/magics.html; these are the docs for IPython version 6, but they are mostly the same for IPython version 5 which Canopy uses in order to support both Python 2.7 and Python 3.5+.)
The two previous answers are always correct in that they will work in any Python program or shell (depending on OS). That is because they only use the Python standard library. However they are not analogous to the cd and pwd commands that you mention, which are specific to the IPython shell (not to programs running in an IPython shell).
IMO, there is very seldom a good reason for opening a plain Python shell. If you simply want to run a script file from a Command Prompt, then by all means python myscript.py. But if you want an interactive shell, IPython provides so much extra capability that it has been for many years the de facto standard for Python shells (which is why it is used for the Canopy GUI's Python shell).
Got a note back from Canopy Support).
There is an environment variable, COMSPEC that was set to:
C:\windows\system32\cmd.exe;
That semicolon at the end is wrong. This is a single directory and not a list of directories. I removed that semicolon and suddenly the 'ls' command works as I remember!

Run my Python script at login on linux

I have a Python script, and I want it to be autostarted at every login. It's in a linux system. I followed a guide that explains that is enough to create a .desktop file in ~/.config/autostart/*.desktop and write:
[Desktop Entry]
Name=MyApp
Type=Application
Exec=python3 ~/.myapp/myapp
Terminal=false
I tried several times to reboot but the program doesn't execute, even if it seems to be active in the list of application of startup in my lxde environment.
Often things like the ~ (tilde) aren't evaluated when placed in a config file. Try using the complete path instead (/home/user/… instead of ~/…) and see if this works. If this works, you can try to use $HOME instead ($HOME/…) to make this more portable and abstract.
If you want to run your script on terminal login, place it to /etc/profile.d/
For KDE (at least, KDE 5) you could add applications to autorun in System Settings > Startup and Shutdown > Autostart (either *.desktop files or scripts), it adds links to ~/.config/autostart.
You can achieve this by adding this line python /home/user/program.py in your .bashrc file. It will be invoked each time when you login to your system.

Save and reload a series of commands into Python shell

I am using the Command Prompt in Windows 8 to perform some analytical tasks with Python.
I'm using a few external libraries, some .py files with functions I need, and some set up commands (like setting certain variables based on databases I need to load).
In all there are about 20 statements. The problem is, that each time I want to work on it, I have to manually enter (copy/paste) all of these commands into the Shell, which adds a few minutes each time.
Is there any way I can save all of these commands somewhere and automatically load them into the prompt when needed?
Yes,
Save your commands in any file in your home directory. Then set your PYSTARTUP environment variable to point to that file. Every time you start the python interpreter it will run the commands from the startup file
Here is the link to example of such file and more detailed explanation
If you need to have different start up files for different projects. Make a set of shell scripts one per project. The scripts should look like this:
#!/bin/bash
export PYTHONSTARTUP=~/proj1Settings.py
python
And so on. Or you can simply change the value of PYTHONSTARTUP variable before you start working on a particular project. Personally, I use MacOS with Iterm2, so I set-up multiple profiles for different projects. When I need to work on a particular project I simply launch a tab with the profile configured for the project.

IPython workflow (edit, run)

Is there a GUI for IPython that allows me to open/run/edit Python files? My way of working in IDLE is to have two windows open: the shell and a .py file. I edit the .py file, run it, and interact with the results in the shell.
Is it possible to use IPython like this? Or is there an alternative way of working?
When I'm working with python, I usually have two terminal windows open - one with IPython, and the other with a fairly customized Vim.
Two good resources:
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
Though it sounds like what you want is IPython's magic function %ed/%edit:
An example of what you can do:
In [72]: %ed
IPython will make a temporary file named: c:\docume~1\wjwe312\locals~1\temp\ipython_edit_ar8veu.py
In the file I put:
x = "Hello World"
print 3
After saving and quitting the file:
Editing... done. Executing edited code...
3
Out[72]: "x = 'Hello world'\nprint 3\n"
In [73]: x
Out[73]: 'Hello world'
You can define functions or anything else - just remember that the contents of the file will be executed when you close it.
Another similar workflow is to cd to the directory containing your Python script that you're editing with your favorite editor. Then you can %run the script from within IPython and you'll have access to everything defined in the file. For instance, if you have the following in the file test.py in your /home/myself directory:
class Tester(object):
def __init__(self):
print "hi"
def knightme(name):
print "Hello, Sir ", name
Then you can do the following:
In [42]: cd /home/myself
/home/myself
In [43]: %run test.py # <Tab> autocomplete also works
In [44]: knightme('John')
Hello, Sir John
In [45]: t = Tester()
Hi
Either a mix or one of those workflows should give you something very similar to the way you're used to working in IDLE.
Spyder, previously known as SPyderlib / Spyder2
Pretty lightweight, fast and support almost all features you will ever need to work with a python project. It can edit and run .py files in an embedded IPython instance and then interact with them, set breakpoints, etc.
full-size
Try Spyder, I have spent all day trying to find an IDE which has the functionality of ipython and Spyder just kicks it out of the park..
Autocomplete is top notch right from install, no config files and all that crap, and it has an Ipython terminal in the corner for you to instantly run your code.
big thumbs up
Take a look at DreamPie. Might be what you are looking for.
Personally, I like PyScripter. Unfortunately, it only works on Windows, but also runs perfectly in Wine.
The latest version of IdleX supports IPython within IDLE, as well as the %edit magic. You can run your files from the IDLE editor within the IPython shell many ways, either by F5 (run everything), F9 (run a selection), or Ctrl+Enter (run a subcode).
sudo apt-get install ipython
Once you are done with installing ipython.
Start ipython from terminal (just hit ipython in the ternminal)
To run ravi.py file all you need to do is
%run ravi.py
If you like the work-flow under Matlab, then you probably should try the following two:
1, Try the combination of Spyder and Vim.
Edit python files in Vim (Spyder can reload the file automatically)
Run the code in Spyder (in the same interpreter, which is important for me):
Use F9 to run the current file
Ctrl+F9 to run the selected block
2, Use Vim + conque-shell. (on google code)
Open your preferred Python interpreter in Vim,
e.g., just :ConqueTermSplit python.
then visual select some Python code
press F9 to paste and run it in the Python interpreter buffer.
Note: a few more:
:ConqueTermVSplit python,
:ConqueTerm python
:ConqueTermVSplit rlwrap python
If your interpretor misses readline, you can use rlwrap.
You might like PySlices...
It's kind of a shell/editor hybrid that lets you save your session as special (barely) modified python files called .pyslice files.
It's now part of wxPython, so just install that (v2.8.11 or later) and run "python -m wx.py.PySlices" on the command line to launch it.
That said, I still end up using an external editor for scripts (geany).
I want to suggest excellent plugin for vim that makes two-way integration between Vim and IPython: vim-ipython.
From project page on http://github.com/ivanov/vim-ipython:
Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object? and object. in IPython.
This plugin has one big limitation: it doesn't support python 3 (it's planned).
Personally, I use what #Wayne suggested, a combination of vim and ipython...
However, if you'd prefer a different approach, take a look at spyder.
As of the latest version (1.1) ipython should be fully integrated. If you download an earlier version, things will work fine with ipython as an external shell, but you won't get a few of spyder's nifty features (like viewing all of the currently defined variables in the workspace window).
Spyder is definitely a bit heavyweight, but it's an interesting project.
Another (very, very, new) similar project to take a look at is iep. It will (sort-of) work with ipython as shell, and I'd be willing to be bet that nicer ipython integration will be along before too long. At any rate, iep is essentially a more lightweight alternative to spyder.
Both of these are oriented towards scientific computing, and so have nice integration with things like matplotlib (and thus can automatically run gui main loops in a seperate thread). They're not quite like "normal" IDE's but they may fill the niche you're looking for quite nicely.
You can use the autoreload module in IPython to automatically reload code.
Open jupyter qtconsole or jupyter console and type:
%load_ext autoreload
%autoreload 2
from your_work_file import *
Now every time you save your_work_file.py, it will be automatically reloaded.
Hint: if you want this to happen automatically, put the followinglines in your ipython_config.py file:
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
Try Ptpython. It has much better integration with VIM. You can directly edit in VIM by just pressing V. It also allows browsing your history.. so you can pretty much code in the shell, and incrementally build up your code.
If you are already familiar with ipython, you can check the advantages of ptpython here:
https://www.youtube.com/watch?v=XDgIDslyAFM
I just use the exclamation mark (!) to run vi as a shell command
In [1]: !vi myScript.py
and when done with editing I just quit vi to get back to the Ipython shell.
To run the script one can then use
In [2]: %run myScript.py
as suggested in another answer and not !python ... because the Python version in ipython might be different from the one in the underlying shell.
If you want to dump some code in a file use the magic %%writefile
In [3]:%%writefile myScript.py
...: print("hello")
...:
...:
Be careful because this will overwrite myScript.py. To append use %%writefile -a.

Categories

Resources