rth
rth

Reputation: 3106

how ot import python modules by default?

Could you tell me, how add importing of python's modules by default? 99% of my scripts starts from

import os,sys,csv
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp

Is it possible to tell python load all this modules by default? maybe it is possible just add these few lines into configuration file (something like .pyrc at home directory)?

Thanks, ~R

Upvotes: 0

Views: 488

Answers (2)

rth
rth

Reputation: 3106

the simplest way to do it is that:

(1) run python to define your local directory for modules:

python -m site --user-site

(2) Then create this directory

mkdir -p <name of you local direcotry>

(3) create tiny file msite.py there

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

Now we need only import everything from this file in our script:

$python
Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from msite import *
>>> np.linspace(0,10,11)
array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])
>>> 

Upvotes: 1

sheh
sheh

Reputation: 1023

if you write some python module, you can import it __init__.py

Upvotes: 2

Related Questions