How to preload some libraries and scripts in python before I call python command? Is there something like .bashrc file to deal with predefining some functions/variables befo
Create a file, 'my_imports.py' and add the import lines for all your modules.
import math
import anothermodule
import anotherothermodule
Then set an environment variable 'PYTHONSTARTUP' and set it to '/path/to/my_imports.py'.
Now when you run python at the command prompt, it will load your modules first.
i wanted to sometimes run python with a bunch of statistics/math stuff loaded (numpy, matplotlib, etc), but othertimes just a simple python without having the overhead of loading modules i wasn't going to use.
i use ubuntu linux, so i created a python script python-preload.py
with the following:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
and added an alias to my ~/.bashrc
:
alias pym='PYTHONSTARTUP=/home/$USER/path/to/script/python-preload.py python'
so when i want normal python i run python
, and when i want all the math stuff i run pym
.
hope this helps. based off Tony Blundell's answer.
How about this:
python -i -c "import math"
And you can put this into a bash file, like b.sh
#! /bin/bash
python -i -c "import math"
Then you can set whatever you want.