Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute Python 3.3 script in Spyder console with variables?

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.

like image 302
uday Avatar asked Aug 29 '13 05:08

uday


People also ask

How do you run a particular code in Spyder?

Option 1 - Select parts of the code and hit F9 This runs the selected code. No surprise there. Hit F9 again, and the same selection is executed once more.

How do I run Python code on Spyder line by line?

The key to run the current line by itself is F9 . The shortcut ctrl+F10 is used if you are in debugging mode. You can see a list of shortcuts by selecting Preferences in the Tool menu, and then clicking on Keyboard shortcuts . In Spyder 3.3.

How do I run Spyder on console?

Running Spyder You can launch it in any of the following ways: From the command line: Type spyder in your terminal (or Anaconda prompt on Windows). From Anaconda Navigator: Scroll to Spyder under Home, and click Launch. *Windows Only*: Launch it via the Start menu shortcut.


2 Answers

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.

like image 98
Carlos Cordoba Avatar answered Oct 14 '22 03:10

Carlos Cordoba


What also works is the IPython console of Spyder is:

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

like image 42
DieterP Avatar answered Oct 14 '22 02:10

DieterP