"How do I set an environment variable from a script?" This question is
"Imprecise" here means that, as it stands, the question could mean several quite different things. Most typical is this: in a command-line environment, a user wants to invoke a small script which re-assigns environment variables. While the examples here refer to Tcl, exactly the same considerations and conclusions apply with other languages. The short answer is: you can't. For sound security reasons, child processes cannot modify a number of different characteristics of their parents, let alone independent processes; environment variables are among these protected characteristics.
However, there are a number of variations on this theme which have the potential to give all the satisfaction of an envvar-setting script. Tom Wilkason provides a recipe for setting envvar for new Windows sessions in "Setting /bin/sh environment variables in the script".
More important, and more broadly applicable, are several cooperative models from the early days of Unix. In these, a process asks for a change of envvar. For example, under Unix, a parent sh-interpreting process might invoke
source `my_example.tcl`
If my_example.tcl contains
#!/usr/bin/tclsh puts "export MY_VAR=some_value"
teamwork between the two processes will result in the parent receiving some_value in its MY_VAR environment variable. LV hints at other examples in "Setting /bin/sh environment variables in the script".