Is there a configuration file for Numpy?

I often use Numpy from the command line and always have to remember to apply some repetitive settings, like setting output formatting:

np.set_printoptions(threshold=np.NaN, precision=3, suppress=True, linewidth=180) 

Is there a global Numpy configuration file that runs automatically for a new Python shell or during import that can do this? If not, is there an elegant way to achieve this effect?

+4
source share
2 answers

I am not aware of such a configuration file for numpy (for matplotlib for example, you are a matplotlibrc file ).

But as a workaround, you can set the PYTHONSTARTUP environment variable by pointing to a Python script that will do whatever you want every time the Python section starts.

In my case, I use it for import numpy as np , import matplotlib.pyplot as plt as such ... saving a small amount of β€œoverhead” every time I want to quickly try something in Python.

Windows example:

 set PYTHONSTARTUP=C:\Users\yourlogin\somewhere\startup.py 

Example for Linux:

 export PYTHONSTARTUP=/usr/local/bin/startup.py 

You should install it only once using the "Control Panel \ System", for example, in Windows.

+2
source

I do not know the configuration file specifically for numpy, but if you use IPython, you can configure the profile so that these fingerprints 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 be in ~/.config/ipython/profile_fancyprint/ipython_config.py

Uncomment this line:

 # c.TerminalIPythonApp.exec_lines = [] 

And add the lines you want to execute at startup:

 c.TerminalIPythonApp.exec_lines = [ 'import numpy as np', 'np.set_printoptions(threshold=np.NaN, precision=3, suppress=True, linewidth=180)' ] 

Then you can run IPython with a named profile

 $ ipython --profile=fancyprint 

Array print options should be set automatically.

+3
source

Source: https://habr.com/ru/post/1498675/


All Articles