How do I load and run specific packages when I launch 'matplotlib'? - python

I see how to change certain settings for matplotlib in such a way that the are used to configure it each time I launch, including when I launch interactively with
ipython --pylab
but I'm not sure how run arbitrary code each time I launch in this way, or how to ensure that certain packages have been imported. For example I'd like to do the following whenever I launch as above:
from mpltools import style
style.use('ggplot')
How do I load and run specific packages when I launch 'matplotlib'?

From the ipython website it seems you can place any .py file in the startup folder of your profile and it will be run when ipython is initiated. For help finding the profile where your startup folder is [see here].
To get the effect your looking for you need to check that matplotlib has been imported, the only way I can think of doing that is to add the following
import sys
if "matplotlib" in sys.modules:
# Do something
print
print "This seems to be working"
I have left it with the test so you can quickly determine if it is working.
Two things to note: I am using ipython 2.0.0-dev (in case this has any bearing), and secondly I suspect that calling sys.modules is not recommended, or at least that was the impression I had off the SO post I borrowed the idea from, perhaps someone better than I can enlighten us.

Related

Is it possible to run ipython with some packages already imported?

Is it possible to run ipython with some packages already imported?
almost every time when I run ipython I do import numpy as np, is it possible to automate this process? i.e. just after I run ipython I want to be able to write something like np.array([0,1]). Is it possible?
In the case of Mac add a script like load_numpy.py under ~/.ipython/profile_default/startup/ directory and add the import statements you need to that script. Every time you run ipython, all the scripts in the startup directory will be executed first and so the imports will be there. in case of Ubuntu add the file to ~/.config/ipython/profile_default/startup/

Asking for overwrite while trying to run script in ipython

I am fairly new to programming in python. I installed anaconda and am running iPython (the Jupyter qtconsole) v.4.3.0 and python v.3.6 on a Mac. Currently, I am trying to import a module with functions located in my home directory.
I have looked at stackoverflow and python documentation and found that it could be done with:
%run "Users/myUser/python_functions.py"
or
import python_functions
However, when I try both of these approaches, I get prompted to overwrite the file that I am running or importing:
File `python_functions.py` exists. Overwrite (y/[N])?
This is changing the previous file and not getting the functions I want to be imported.
What may explain this, and what can I do to import my module?
this is wrong but leaving it up for shame
import on ubuntu (and I'm guessing many other unix-like OSs including Mac) is a utility that saves any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.
My guess if you are running the import command in your console, and it's about to take a screenshot and save it over an existing file - python_functions
Before you the use the python import command, start a python interpreter:
$ python
>>>import yourfile
edit: on re-reading your question, I'm not so sure about my guess anymore, but leaving it up until you tell me I'm wrong :)
Running Jupyter qtconsole as an interpreter is likely causing the problem in this scenario. Instead using a IDE or command line interpreter will resolve it .
Since anaconda was installed, trying it with the IDE Spyder executes the code just fine without the overwrite prompt. It works on others (e.g PyCharm, Rodeo, etc.) as well.

Jupyter reload custom mplstyles

I want to develop custom styles for plots in matplotlib. For this reason I have a custom stylesheet (my_style.mpstyle) located in the default directory (~/.config/matplotlib/styles). I can do this with Python scripts without a problem but recently I find myself using Jupyter more. Although the stylesheets generally work, I have the problem, that changes in the stylesheet are not considered unless I restart the Kernel - is there a way of reloading the stylesheet when I execute the cell such that I can develop the style with Jupyter?
Thanks.
I got around this problem by defining a PYTHONSTARTUP file. This is a file that contains a set of python commands (usually import statements) that are automatically executed every time you launch a python interpreter, even when launching a jupyter application or restarting a kernel within one.
Here's a portion of my startup file that imports pyplot and sets a style sheet.
try:
import matplotlib.pyplot as plt
print('import matplotlib.pyplot as plt')
except:
print('Could not import matplotlib.pyplot')
try:
plt.style.use('mystylesheet')
except:
print('No mpl style sheet set; could not find "mystylesheet"')
To make python execute the startup file, you need to define the environmental variable PYTHONSTARTUP=full/path/to/startupfile. In Linux, it's quite easy; just modify your .bashrc to include
export PYTHONSTARTUP="full/path/to/startupfile"
For windows it is a little more involved, and of course the process varies from one version to another. Here is one website that appears to summarize the process for several versions.

PyCharm and IPython Notebook: Include files from project

Within my project XYZ, I have a file superSource.py, which contains some functions.
Now, I've used the new cool pyCharm feature of creating an IPython notebook, which I calltest test.ipynb, and saved it in the projects main directory (next to superSource.py).
However, when I run import superSource; foo = superSource.parameters() nothing happens, I don't even get a warning. pyCharm will underline superSource within the code though, warning me that there is no module called superSource.
How can I include other files from the same directory using the IPython notebook and/or pyCharm?
I've had the same problem and have a partial solution.
To include your file, add the following to a cell:
execfile("superSource.py").
This should load and execute it and make its contents available for reuse so that you can access variable and call functions defined or imported by it in other cells.
Unfortunately, PyCharm does not know about it, so that as you type, there is no statement completion and if you have "Show import popup" enabled in PyCharm, it will suggest adding an import but highlight it as an error afterwards. It should still work, however.

Synchronize .ipynb and .py to use ipython notebook and eclipse at the same time

I started programming some scripts with ipython notebook but now the project is becoming to big for a notebook. Nevertheless I love to execute my stuff in an ipython notebook (load de data only once, online figures...).
What I would want is to program everything with eclipse but executing it in ipython. I know I can save the notebooks as .py by adding the --script option at the beginning. But now I want to automatically make the process the other way around. I mean, I want my ipython notebook to reload de code I modify with Eclipse.
Is it possible?
Should I make a program that makes it using the converter?
Thanks!!
I found the solution for manually updating the functions without rerunning the whole .ipynb file. However, I do not know how to make it automated. Here is the solution:
Synchronizing code between jupyter/iPython notebook script and class methods
To cut it short you need to put reload function from importlib module in the cell of interest:
import babs_visualizations
from importlib import reload
reload(babs_visualizations)
Just a little addition: make sure that you are addressing the function in the form of moldule.function. If you previously imported function by from module import function and then reloaded the module the function will not be reloaded. You can put the function inside the notebook cell and rerun it to see, how the changes in the module affected the function output in the notebook.
I hope this was helpful for someone.

Categories

Resources