The .env File
Some settings belong to the computer a program runs on, not to the program: where an instrument is plugged in, which network port a server uses, a password. They differ from lab to lab, and some of them are secret, so they must not be written into code or into a file that is shared with others.
plesty-lib reads such settings from environment variables. An environment variable is a named value that the program finds in its surroundings instead of in its code. There are two places to set one:
in the terminal, before starting the program:
export PM_ADDRESS=tcp://192.168.1.20:5555 # macOS, Linux $env:PM_ADDRESS = "tcp://192.168.1.20:5555" # Windows PowerShell
in a file called
.env, oneNAME=valueper line. This is the usual place, because the file stays where it is and you do not have to set anything again in each new terminal:# .env — lines starting with # are comments PM_ADDRESS=tcp://192.168.1.20:5555 PM100D_VISA=USB0::0x1313::0x8078::P0000001::INSTR
Where the file goes
plesty-lib looks for .env in the folder you start the program from, not in the
folder of the script. Start your programs from the module’s top folder and keep
.env there.
Which value wins
When a name is set in more than one place, the first of these wins:
a value given to the program directly, such as a command-line option (see
overridebelow);the terminal’s environment;
the
.envfile;the default in the code.
Variables plesty-lib reads by itself
You do not have to read these yourself: the parts of plesty-lib that need them do.
Variable |
Read by |
Holds |
|---|---|---|
|
|
the network address of that device’s server; the name in capitals, so a device called |
|
experiments, |
the folder where this computer sees the lab’s shared data disk (see Data on a Shared Disk); a leading |
Reading your own variables: EnvSettings
For settings of your own, use EnvSettings from plesty.lib.utils. It reads .env
and the terminal’s environment together, and converts a value to the type you ask
for:
from plesty.lib.service import build_server, run_server
from plesty.lib.utils import EnvSettings
from powermeter_device import PowermeterDevice
env = EnvSettings.load()
address = env.require("PM100D_VISA") # must be set
tcp_port = env.get("PM100D_TCP_PORT", default=5555, cast=int) # may be left out
device = PowermeterDevice(address, sensor_type="S155C")
run_server(build_server(device, address=f"tcp://*:{tcp_port}").run())
Call |
Does |
|---|---|
|
reads |
|
the value, converted with |
|
the same, but stops the program with an error naming the variable when it is unset. Use it for anything the program cannot run without. |
|
whether the variable is set and not empty |
Both get and require take override=: a value that wins over everything else
when it is not None. Pass a parsed command-line option there, and the option
replaces the environment for that one start:
address = env.require("PM100D_VISA", override=args.address) # --address, if given
load() also copies the values it read from .env into the program’s environment,
without replacing any that are already set, so programs it starts see them too.
Pass export=False to only read them.