1

Python novice here. I'm attempting to pass a tuple of large numpy arrays to a script for processing, so I need to use their variable names to pass them from the IPython terminal.

The capability I'm looking for can be simplified to the following:

Suppose script.py is a script that simply prints the variable passed to it.

>>> var_name = (1,True)
>>> %run script.py var_name
(1,True)

var_name here is a variable that is known by and created by the IPython terminal. As of yet, I've only succeeded in returning "var_name" or Namespace(data=('var_name',)) in attempts at using sys.argv[1] or argparse.

Latest attempt:

import sys

data = tuple(sys.argv[1])

print data

The result:

>>> t = (1, True, 3.5, "hi")
>>> t
(1, True, 3.5, "hi")
>>> %run script.py t
('t',)

In MATLAB, the task of importing is simply accomplished in the first line:

function[] = scriptName(inputVar)
disp(inputVar)

Calling the script from the MATLAB terminal would look something like this:

>> scriptName(700)  
   700

inputVar can be an int, double, string, matrix, etc.

Is there an equivalent action in Python? Must I be using sys.argv[1] or argparse incorrectly? I know this is a beginner's question, but in the past 2 hours of searching and reading, I've found no solutions.

Thanks!

3
  • 1
    why are you calling tuple on sys.argv[1]? Commented Jul 16, 2015 at 0:23
  • Also, welcome to Stack Overflow! Commented Jul 16, 2015 at 0:33
  • I was calling tuple on sy.argv[1] in an attempt to force the input to be the tuple that was stored in t. Alas, it simply turned the string 't' into a tuple. Commented Jul 16, 2015 at 17:38

3 Answers 3

2

IPython is calling the program on the command line. It's passing t, which you think is a variable name, as a string. So the only argument your script is getting is "t".

If you want to pass variables into a script, what you want to do is import the script, and then call the function. So here would be your script.py:

def main_function(argument1, argument2):
    # all the stuff you want to do in your script.

Now, you can just import the script and call that function:

>>> import script
>>> script.main_function(variable1, variable2)
Sign up to request clarification or add additional context in comments.

2 Comments

great answer and more correct than mine imho ... for some dumb reason I assumed the OP knew what he wanted to do, but I suspect he really wants this :P
I was almost ready to submit an answer using pickle.dumps() before I realized the same thing :D
1
%run script.py json.dumps(var_name.tolist()) #im not sure if run can accept variables

you could alternatively try

% import subprocess
% subprocess.check_output('./script.py "%s"'%(json.dumps(var_name.tolist()))

script.py

import sys

data = json.loads(sys.argv[1])

print data

1 Comment

Large NumPy arrays, if they can be serialized into JSON at all, will be very ineffecient to pass as command line parameters. You could use pickle, which will work with NumPy arrays, but it will still be inefficient. Best to just import the script and call it.
0

In MATLAB one function is defined per file (with matching name) - at least that was the orginal arrangement. So >> scriptName(700) loads the file, and calls the function with the argument you give.

In Python, a file contains a module, which may contain many functions (as well as class definitions, variables) and actions. Such a module can be imported, or it can be run as a script. When run (as from OS shell) code protected by if __name__=='__main__' is run. And sys.argv contains command line arguments - as a list of strings.

ipython can import modules. But it also has this %run magic command. Functionally it is like calling the script from a shell, including passing the sys.argv strings. What is unique about %run is that the results of the run are placed in the main ipython workspace. This is a bit like the from xxx import * command.

So if you wrote a script file foo.py:

def foofn(anarg):
    print(repr(anarg))
if __name__=='__main__':
    import sys
    foofn(sys.argv)

And ran it:

%run foo testing 1 2 3

you would see the argv strings

['foo.py', 'testing', '1', '2', '3']

ipython also allows the use of {} in magic commands

%run foo {t}

but your t would expand to a set of strings

'(1,', 'True,', '3.5,', 'hi)'

In effect, foo.py is responsible for parsing those strings (e.g. with argparse).

You could also call foofn directly:

foofn([1,2,3])

In this the function get a list of numbers, not the sys.argv strings.

In other words, the function defined in foo.py is now available for use. This is close to what you can do in MATLAB. You just have first load it with the run.

If instead you did:

import foo

you have to specify the foo namespace.

foo.foofn([1,2,3])

In ipython do

%run?

to see its doc; and

%run??

to see the doc and code. There's a lot going on. Note that

It thus sees its environment as if it were being run as a stand-alone program (except for sharing global objects such as previously imported modules). But after execution, the IPython interactive namespace gets updated with all variables defined in the program.... This allows for very convenient loading of code for interactive work, while giving each program a 'clean sheet' to run in.


%run takes a '-i' option, that gives the script access to variables defined in the ipython session. So if the script contained foo(t), and t is defined in ipython, that would used. I've never had occasion to use it.

Comments

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.