I often use Numpy from the command line and always have to remember to apply some recurrent settings, for example setting the output formatting:
np.set_printoptions(threshold=np.NaN, precision=3, suppress=True, linewidth=180)
Is there a global Numpy config file that is automatically executed for a new Python shell or during import that can execute this? If not, is there an elegant way to achieve this effect?
I am not aware of such a configuration file for numpy
(for matplotlib
for example you have the matplotlibrc
file).
But, as a workaround, you can set your environment variable PYTHONSTARTUP
pointing to a Python script that will do whatever you want everytime a Python section starts.
In my case I use it to import numpy as np
, import matplotlib.pyplot as plt
as so on... saving a small amount of "overhead" everytime I want to quickly try something on Python.
Example on Windows:
set PYTHONSTARTUP=C:\Users\yourlogin\somewhere\startup.py
Example on Linux:
export PYTHONSTARTUP=/usr/local/bin/startup.py
You should set it just once using the "Control Panel\System", for example, on Windows.
I'm not aware of a configuration file specifically for numpy, but if you use IPython you could set up a profile such that these print options are set automatically when you open a new IPython shell.
Create a new profile (you can omit the name to create a default profile)
$ ipython profile create fancyprint
Then edit the configuration file, which should live in ~/.config/ipython/profile_fancyprint/ipython_config.py
Uncomment this line:
# c.TerminalIPythonApp.exec_lines = []
And add the lines you want to execute on startup:
c.TerminalIPythonApp.exec_lines = [
'import numpy as np',
'np.set_printoptions(threshold=np.NaN, precision=3, suppress=True, linewidth=180)'
]
Then you can launch IPython with the named profile
$ ipython --profile=fancyprint
Your array printing options should be set automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With