# Run one Python script from another on Windows, Linux and Unix machines that use the same code

I have a series of rather complicated codes that I developed on a Linux machine, run on a Unix cluster and from time to time need to run on a Windows machine to gain access to a private network. They are all running Python 3.6 and I have not had a lot of success moving the subprocess.call or similar command between platforms.

The codes do not take any inputs and I want to be able to run them on their own, therefore, I do not want to make them function that I call from another script. Currently, the only way that I have found to run the codes is to import the code on the first time it is called and then recall it using importlib.reload(Code_2_NLLS_capacity) the next time.

Here is an example (Added for simplicity as it is easier to read) of what I am doing:

import importlib as iml

for i in range(10):
if i == 0:
import Code_1
import Code_2
import Code_3
import Code_4

else:


This works well enough for a loop, but it gets rather complicated when I try to run the codes out of order. Lastly, the code takes a fairly long time to run (5+ hours per code) so I do not want to simply pre import the code and then reload the code as needed.

The following code is my actual code. It is run on a cluster machine that provides a .out file of the terminal. This is why errors are ignored and I print large dividing headers.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This code runs all the other codes in the system.
@author: downey
"""
#ignore warings to prevent large file sizes on .out files
import warnings
warnings.filterwarnings('ignore')

import matplotlib.pyplot as plt
import numpy as np
import pickle as pickle
import os as os
import importlib as iml

plt.close('all')

#%% Build the dynamic bounds data set

#set the bound limits
bound_limits = np.arange(0.05,.8,0.05)

# for each bound limit, build the linear bound, and cycle though all the code
for bound_i in range(bound_limits.shape[0]):

# set the battery life in cycles, so the dynamic bound reaches its target
mass_pos = np.zeros((250,8))

dynamic_bounds = np.zeros(mass_pos.shape[0])
dynamic_start = 0
dynamic_end = bound_limits[bound_i]
x_bounds = np.arange(0,mass_pos.shape[0])
dynamic_bounds = np.interp(x_bounds, [0,mass_pos.shape[0]], [dynamic_start,dynamic_end])

plt.figure()
plt.plot(x_bounds,dynamic_bounds)
plt.xlabel('number of charge cycles')
plt.ylabel('enforced bounds on the NLLS')
plt.title('dynamic bounds on the NLLS')

os.chdir('data')
pickle.dump(dynamic_bounds,open('dynamic_bounds_temp.pickle','wb'))
os.chdir('..')

#%% run the codes
# buid the data sets
if bound_i == 0:
import Code_1_build_data_sets
# run the NLSS on a parameter basis
import Code_2_NLLS_capacity
import Code_2_NLLS_slip_pos
import Code_2_NLLS_mass_neg
import Code_2_NLLS_mass_pos
# solve the half cell model on a model basis
print('***********************************************************************\n Code_3_cell_prognostics_model_1\n***********************************************************************')
import Code_3_cell_prognostics_dynamic_bounds
import Code_4_RUL_dynamic_bounds

else:
# run the NLSS on a parameter basis

• iml.reload(Code_1) is really bad, can you not make them all have a function main that you can run? import Code_1; for _ in range(10): Code_1.main(). Either way you'd need to add more code for us to properly review this. – Peilonrayz Jul 11 '17 at 10:25