How to run startup commands on loading Python Interactive window in VSCode - python

I'm trying to execute some startup commands before starting or resetting the Python Interactive Window (IPython) within VSCode.
As I have been check in the documentation and in the parameters settings, I was able to identify the correct parameter:
Python > Data Science: Run Startup Commands
A series of Python instructions or iPython magic commands separated by '\n' that will be
executed when the interactive window loads. For instance, set this to
'%load_ext autoreload\n%autoreload 2' to automatically reload changes
made to imported files without having to restart the interactive
session.
However, when I insert my desirable python commands they not work as expected. As an example I tried to load the numpy package inserting 'import numpy as np', but when I start a new Python Interactive Window it outputs:
np is not defined
What I'm doing wrong? Thank you!

Remove the quotes '', just import numpy as np and don't forget to separate instructions with \n.

Related

IPython notebook: debugging by stopping the execution at specific point

i want to interactively debug code that is deeply hidden in some class.
I know that it is possible open the ipdb using
from IPython.core.debugger import Pdb
Pdb().set_trace()
This work also in ipython (jupyter) notebook.
Another possibility that I find very convenient when I am working in the terminal is to open a shell at a specific point using
from IPython import embed
embed()
and then use ipython directly to interact with variables etc.
However, if I use this in notebook, I get a ipython shell embedded into the notebook, and this shell does not even work well (e.g., if I use print, the kernel blocks).
What I would find most convenient is to interrupt the kernel execution at some predefined point in the code and keep all current variables, so that I can use another cell for debugging and the possibility to, e.g., develop and test new code with the current variables.
Is there some way to achieve this?

Using ipython exclamation point outside of ipython

I want to use the useful ! call-to-shell feature of ipython in regular python script, i.e. I want the following code:
for i in range(5):
!echo {i}
To work in an in-house python module that can be imported (thus simply using ! will not work).
I've tried using subprocess but wasn't able to get the output of the shell run into the Jupyter notebook, where most of our scripts are running at.
So, what I'm looking for is using ! functionality as an IPython import some how..

Python code snippet that runs always when a ipython notebook closes

I would like to add a python code to an ipython notebook that will run every time I close the ipython tab. I tried to see if I can set a cell to do it but I had no luck.
Is this possible either using an ipython API or some other hook mechanism?
One option could be using the atexit python module to register an exit handler. This would work if your page in the IPython notebook is actually a python process.

breaking into IPython notebook server at arbitrary line?

I usually do the following trick for debugging, add following snippet to a place where I want to break into IPython shell:
from IPython.terminal import embed
ipshell = embed.InteractiveShellEmbed()
ipshell()
Does anyone know of a way to do something similar, but instead of spawning shell, start an interactive notebook session in browser?
For that to work, you'd have to either
have the thing you're trying to debug already running in your python notebook daemon's control,
or you'd have to have a debugging backend that you can attach to your process started from within your notebook daemon.
Since the second, to my knowledge, doesn't exist (yet), your only option would be to start the program you want to debug from within your notebook.

Automatically import modules when entering the python or ipython interpreter

I find myself typing import numpy as np almost every single time I fire up the python interpreter. How do I set up the python or ipython interpreter so that numpy is automatically imported?
For ipython, there are two ways to achieve this. Both involve ipython's configuration directory which is located in ~/.ipython.
Create a custom ipython profile.
Or you can add a startup file to ~/.ipython/profile_default/startup/
For simplicity, I'd use option 2. All you have to do is place a .py or .ipy file in the ~/.ipython/profile_default/startup directory and it will automatically be executed. So you could simple place import numpy as np in a simple file and you'll have np in the namespace of your ipython prompt.
Option 2 will actually work with a custom profile, but using a custom profile will allow you to change the startup requirements and other configuration based on a particular case. However, if you'd always like np to be available to you then by all means put it in the startup directory.
For more information on ipython configuration. The docs have a much more complete explanation.
Use the environment variable PYTHONSTARTUP. From the official documentation:
If this is the name of a readable file, the Python commands in that
file are executed before the first prompt is displayed in interactive
mode. The file is executed in the same namespace where interactive
commands are executed so that objects defined or imported in it can be
used without qualification in the interactive session.
So, just create a python script with the import statement and point the environment variable to it. Having said that, remember that 'Explicit is always better than implicit', so don't rely on this behavior for production scripts.
For Ipython, see this tutorial on how to make a ipython_config file
I use a ~/.startup.py file like this:
# Ned's .startup.py file
print("(.startup.py)")
import datetime, os, pprint, re, sys, time
print("(imported datetime, os, pprint, re, sys, time)")
pp = pprint.pprint
Then define PYTHONSTARTUP=~/.startup.py, and Python will use it when starting a shell.
The print statements are there so when I start the shell, I get a reminder that it's in effect, and what has been imported already. The pp shortcut is really handy too...
As a simpler alternative to the accepted answer, on linux:
just define an alias, e.g. put alias pynp='python -i -c"import numpy as np"' in your ~/.bash_aliases file. You can then invoke python+numpy with pynp, and you can still use just python with python. Python scripts' behaviour is left untouched.
While creating a custom startup script like ravenac95 suggests is the best general answer for most cases, it won't work in circumstances where you want to use a from __future__ import X. If you sometimes work in Python 2.x but want to use modern division, there is only one way to do this. Once you create a profile, edit the profile_default (For Ubuntu this is located in ~/.ipython/profile_default) and add something like the following to the bottom:
c.InteractiveShellApp.exec_lines = [
'from __future__ import division, print_function',
'import numpy as np',
'import matplotlib.pyplot as plt',
]
You can create a normal python script as import_numpy.py or anything you like
#!/bin/env python3
import numpy as np
then launch it with -i flag.
python -i import_numpy.py
Way like this will give you flexibility to choose only modules you want for different projects.
As ravenac95 mentioned in his answer, you can either create a custom profile or modify the default profile. This answer is quick view of Linux commands needed to import numpy as np automatically.
If you want to use a custom profile called numpy, run:
ipython profile create numpy
echo 'import numpy as np' >> $(ipython locate profile numpy)/startup/00_imports.py
ipython --profile=numpy
Or if you want to modify the default profile to always import numpy:
echo 'import numpy as np' >> $(ipython locate profile default)/startup/00_imports.py
ipython
Check out the IPython config tutorial to read more in depth about configuring profiles. See .ipython/profile_default/startup/README to understand how the startup directory works.
My default ipython invocation is
ipython --pylab --nosep --InteractiveShellApp.pylab_import_all=False
--pylab has been a ipython option for some time. It imports numpy and (parts of) matplotlib. I've added the --Inter... option so it does not use the * import, since I prefer to use the explicit np.....
This can be a shortcut, alias or script.
I created a little script to get ipython initialized with the code you want.
Create a start.ipy file at your project root folder.
Edit the created file with all the things you need to get into ipython.
ipython profile create <your_profile_name>. Tip, do not add the word "profile" to the name because ipython already includes it.
cp start.ipy ~/.ipython/profile_<your_profile_name>/startup/start.ipy
Run ipython --profile=<your_profile_name> everytime you need everything loaded in ipython.
With this solution, you don't need to set any env variable up. You will need to copy the start.ipy file to the ipython folder every time you modify it, though.

Categories

Resources