0

I have a Python script that is being executed through a Jupyter Notebook running on VScode. I am running it using the run magic command. The script needs some NumPy arrays and strings as inputs, these arrays are stored as Jupiter variables. I though that using args = sys.argv was enough, but when debugging I realised that the args inside the script are actually the strings passed as inputs and not the arrays stored under those names. For instance in

%run myScript string1 string2 NumpyArray1  

the variable inside the script is the string "NumpyArray1" and not the array stored with the name "NumpyArray1" in the Jupyter:variables of the notebook. 'NumpyArray1' is a 2D matrix (432x532)

Can someone explain to me how to pass an array as input to the script?

Thanks in advance!

4
  • Does this answer your question? Pass input parameters to script from jupyter notebook Commented Aug 23, 2022 at 8:35
  • @Jeanot this comes close, but for some reason just the first element of the variable "NumpyArray1" (which is an array with hundreds of values) are passed through.... Commented Aug 23, 2022 at 8:56
  • present the entire situation. likely "running a subprocess" is a bad and wrong solution and should not be made to work. you should import that script and call functions contained within it, passing your data in. Commented Aug 23, 2022 at 9:18
  • @ChristophRackwitz it is a script running some multiprocessing functions. For many reasons I am unable to run multiprocessing on a Jupyter notebook so I have this script and use the notebook just to automatise the process avoiding using the terminal. I believe my multiprocessing will not work if imported... Commented Aug 24, 2022 at 2:22

2 Answers 2

0

You can't really pass an array as a command line argument (not on a very elegant way at least). What you can do, is giving the command line a a bunch of numbers separated by comma without brackets.

python myScript 3,1,4, etc.

Afterwards, just use a simple split on the argv[] like below

import sys
arr = sys.argv[1].split(',')
print(arr[2])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Unfortunately, I think this will not work since 'NumpyArray1" is a 2D matrix with hundreds of values
Oh, I did not read you were working with 2d-array. Kindly update your post and in the meantime, I will update my answer :-)
0

You can do something like this to automate

#python script.py '1,2,3,4,5,6,7,8,9'

import numpy as np
import sys
import numpy as np

def numpy_array_to_csv_string(np_array):
    csv_string = np.array2string(np_array, separator=',')
    return csv_string[1:-1]  # Remove the surrounding square brackets

def process_csv(csv_string):
    # Convert the CSV string back to a NumPy array
    np_array = np.fromstring(csv_string, sep=',')
    
    # Reshape the array if necessary (e.g., if it was a 2D array)
    # np_array = np_array.reshape((num_rows, num_columns))
    
    # Do whatever processing you want with the NumPy array here
    print("NumPy Array:")
    print(np_array)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python script.py 'csv_data'")
    else:
        csv_data = sys.argv[1]
        process_csv(csv_data)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.