mwi*_*.me 5 python configuration numpy environment-variables configuration-files
我经常在命令行中使用Numpy,并且始终要记住应用一些重复设置,例如设置输出格式:
np.set_printoptions(threshold=np.NaN, precision=3, suppress=True, linewidth=180)
Run Code Online (Sandbox Code Playgroud)
是否有一个针对新Python Shell或在导入过程中可以自动执行的全局Numpy配置文件?如果没有,是否有一种优雅的方法来达到这种效果?
我不知道这样的配置文件numpy(例如matplotlib您有该matplotlibrc文件)。
但是,作为解决方法,您可以将环境变量PYTHONSTARTUP设置为指向 Python 脚本,该脚本将在每次 Python 部分启动时执行您想要的操作。
就我而言import numpy as np,import matplotlib.pyplot as plt每次我想在 Python 上快速尝试一些东西时,我都会使用它来节省少量的“开销”。
Windows 上的示例:
set PYTHONSTARTUP=C:\Users\yourlogin\somewhere\startup.py
Run Code Online (Sandbox Code Playgroud)
Linux 上的示例:
export PYTHONSTARTUP=/usr/local/bin/startup.py
Run Code Online (Sandbox Code Playgroud)
您应该使用“控制面板\系统”仅设置一次,例如在 Windows 上。