import
statement¶import numpy as np # Access as np.functionName (USE THIS)
import numpy # Access as numpy.functionName
import numpy as numnum # Access as numnum.functionName
from numpy import sin, cos # Access as x=cos(5) or x=sin(5)
from numpy import * # Access as functionName (NEVER DO THIS)
Create a code file
ft_to_m = 1.0/3.28084
m_to_ft = 3.28084
lbm_to_kg = 1.0/2.20462
kg_to_lbm = 2.20462
def K_to_C(T_K):
return T_K - 278.15
def F_to_C(T_F):
return (T_F - 32.0)/1.8
Now you can import this file and its variables/functions
import units as u
x_ft = 10
x_m = x_ft * u.ft_to_m
print(x_m)
T = 212 # F
TC = u.F_to_C(T)
print(TC)
3.047999902464003 100.0
Load certain variables or functions from the module directly
from units import F_to_C
F_to_C(212)
100.0
The code in the module is run when the module is loaded
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
import glob
from all_imports import *
from all_imports import *
The module you load has to be in the same directory
PYTHONPATH
which you can edit.import sys
sys.path.append('/path/to/folder/with/the/module')
dir()
function
dir(u)
function will show a list of names that module u definesdir(u)
will show all currently known names (except builtin names)dir(u)
['F_to_C', 'K_to_C', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ft_to_m', 'kg_to_lbm', 'lbm_to_kg', 'm_to_ft']