About PyBat

PyBat is a python module and batch file that allow you to share environment variable changes between Python scripts and batch files. The original problem comes from the fact that when the batch file's cmd.exe process calls the Python script's python.exe process (or vice versa), any changes to the environment variables in the child process won't be reflected in the parent.
NOTE: You only need to use PyBat when the called batch file/python script makes environment variable changes that need to be reflected in the calling process.

How PyBat is Used

What Works:

A batch file can call another batch file, and have the environment variable changes carry back.

foo.bat
set spam=Original!
call bar.bat
echo %spam%

bar.bat
set spam=Changed!

Calling foo.bat prints out "Changed!"

A Python script can call another Python script, and have the environment variable changes carry back.

cats.py
import os
os.environ['spam'] = 'Original!'
import dogs
print os.environ['spam']

dogs.py
import os
os.environ['spam'] = 'Changed!'

Calling cats.py prints out "Changed!"

What Doesn't Work (without PyBat)

If a batch file calls a Python script, the environment variable changes don't carry back.

eggs.bat
set spam=Original!
python bacon.py
echo %spam%

bacon.py
import os
os.environ['spam'] = 'Changed!'

Calling eggs.bat prints out "Original!" If a Python script calls a batch file, the environment variable changes don't carry back.

alpha.py
import os
os.environ['spam'] = 'Original!'
os.system('beta.bat')
print os.environ['spam']

beta.bat
set spam='Changed!'

Calling alpha.py prints out "Original!"

Making it Work (with PyBat)

First, download PyBat. There are several files in that zip.
To call Python scripts from a batch file, you'll need pybat.bat.
To call batch files from Python scripts, you'll need pybat.py.

If a batch file calls a Python script with pybat.bat, the environment variable changes carry back.

apples.bat
set spam=Original!
pybat.bat callpy oranges.py
echo %spam%

oranges.py
import os
os.environ['spam'] = 'Changed!'

Calling apples.bat prints out "Changed!"

If a Python script calls a batch file, the environment variable changes carry back.

boy.py
import pybat, os
os.environ['spam'] = 'Original!'
pybat.callbat('girl.bat')
print os.environ['spam']

girl.bat
set spam=Changed!

Calling boy.py prints out "Changed!"