21

how can I execute Python 3.3 script in Spyder console, and that has variables?

My sample code (C:/test/myfile.py) is

from sys import argv
script, first, second, third = argv
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

I have tried exec(open("C:\test\myfile.py").read()) - and the error I get is "ValueError: need more than 1 value to unpack. I want to supply the variables first = "1st", second = "2nd", third = "3rd". How can I write the exec() so that it can handle the inputs?

I'm using Python 3.3, 64-bit installation, Windows OS, installation: WinPython.

uday
  • 6,453
  • 13
  • 56
  • 94
  • Do you mean something like this: http://stackoverflow.com/questions/89228/calling-an-external-command-in-python/89243#89243 – User Aug 29 '13 at 09:38

2 Answers2

18

You need to go

Run > Configuration per file

(or hit Ctrl+F6) and in the dialog that it appears you need to check

Command line options

and write (for example) there

1 2 3

After closing this dialog and hitting F5, you'll see the output you are expecting.

Note: Please remember that these command line options are saved between Spyder restarts as part of the file run config, so if you want to change them, you need to hit Ctrl+F6 again.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
  • How can I use environment variables in the `Run->Configure->Command Line Options` ? I tried something like `%myEnvironmentVar%\file.txt`, but Spyder appends the current working directory to it. – user3731622 Sep 16 '16 at 20:16
  • 1
    You can't pass a file environment variables in that dialog, sorry. – Carlos Cordoba Sep 17 '16 at 13:12
  • Is it possible to save the config in the environment or anywhere else ? Each time I restart my machine I have to re-config... – Bastienm Apr 29 '19 at 09:30
  • Configurations per file are saved on disk, unless there's an issue while doing that (e.g. a permissions error on Spyder's config directory). – Carlos Cordoba Apr 29 '19 at 11:17
13

What also works is the IPython console of Spyder is:

In [1]: runfile('C:/yourfolder/myfile.py',args='one two three')

DieterP
  • 141
  • 1
  • 2