It's common to create small scripts which we want to combine into a larger script. We don't want to copy and paste the code. We want to leave the working code in one file and use it in multiple places. Often we want to combine elements from multiple files to create more sophisticated scripts.
The problem we have is that when we import a script it actually starts running. This is generally not what we expect when we import a script so that we can reuse it.
How can we import the functions (or classes) from a file without having the script start doing something?
Let's say that we have a handy implementation of the haversine distance function called haversine()
, and it's in a file named ch03_r08.py
.
Initially, the file might look like this:
import csv import pathlib from math import radians, sin, cos, sqrt, asin from functools import partial MI= 3959 NM= 3440 KM= 6373 def haversine( lat_1...