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

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.

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.

What also works is the IPython console of Spyder is:
In [1]: runfile('C:/yourfolder/myfile.py',args='one two three')

Related

How to pass arguments in Spyder using sys argv [duplicate]

This question already has answers here:
How to use argv with Spyder
(4 answers)
Closed last year.
I am struggling with passing 2 arguments in spyder using the command line. I have used the run-->configuration per file and in the command line options put JPEGtoPNG.py/Poxedex/new/. The JPEGtoPNG is the python file and the arguments to be passed are poxedex and new.
Dilemma:
When i run print(sys.argv[0]) it prints:
runcell(0, '/Users/chideraokafor/JPEGtoPNG.py')
which i understand is the default.
However when i run print(sys.argv[1]) it prints:
IndexError: list index out of range.
I have tried everything but still, it's not passing the two arguments, and I really don't want to use pycharm.
If Poxedex and new are command line arguments for a script, JPEGtoPNG.py, execution should be:
python JPEGtoPNG.py Poxedex new
Not / separated.
Note that the error you get is because you are not passing command line arguments and as such sys.argv is a list of length one (that one being the script name) and indexing starts at 0 in Python so accessing the second element via sys.argv[1] is indeed out of range.
Since Spyder provides a Python console and the options you are passing via the menu are intended as that - options and not arguments - you might find it easier to run the script(s) from the command line, for example using VSCode.
Even if you don't want to use VSCode, you can just open a terminal window and invoke the script from there. Just check your environment variables to ensure Python is on your system (or user) path.

ValueError: not enough values to unpack (expected 4, got 1)

The error is at script, first, second, third = argv. I would like to understand why I am getting the error and how to fix it.
from sys import argv
script, first, second, third = argv
print("The script is called: ", script)
print("The first variable is: ", first)
print("The second variable is: ", second)
print("The third variable is: ", third)
Run it from the shell like this:
python script.py arg1 arg2 arg3
argv variable contains command line arguments. In your code you expected 4 arguments, but got only 1 (first argument always script name). You could configure arguments in pycharm. Go to Run -> Edit Configurations. Then create a new python configuration. And there you could specify Script parameters field. Or you could run your script from command line as mentioned by dnit13.
You could run it like this: python script.py first, second, third
I think you are running the following command:
python script.py
You are writing a program which is aksing for 4 inputs and you are giving onle one. That's why you are receiving an error. You can use the below command:
python script.py Hello How Are

Learning Python 3.4 - variable unpacking with argv [duplicate]

This question already has answers here:
What does "sys.argv[1]" mean? (What is sys.argv, and where does it come from?)
(9 answers)
Closed 7 months ago.
I am working my way through "Learning Python the Hard Way". I am totally stumped on "Exercise 13: parameters, unpacking, variables". I'm using Sublime Text 3 in a Windows 7 environment.
Here is my code:
from sys import argv
script, first, second = argv
print ("The script is called:", script)
print ("Your first variable:", first)
print ("Your second variable:", second)
Now my questions:
Do I put this in the scripts folder and then reference this from another .py file something like...
c:\scripts\python\ex11.py one_thing second_thing another_thing
...or do I use a file in my scripts folder to reference my file in another folder that is holding my .py files?
What is the syntax to point to another file in another folder?
It helps to determine what the arguments vector, or argv actually does. It's reading in what you pass in to it from the command line.
So, this means that this command should work (provided Python is in your path, and you can just execute the file in this manner):
C:\scripts\python\ex11.py one_thing second_thing another_thing
Note that you'll only see one_thing and second_thing come across; the first value is the name of the script.
When using the command line to run programs, arguments can be specified to get the program to run in different ways.
In this example, you will need to open up powershell, run cd c:\scripts\python\, and then python ex11.py one_thing second_thing another_thing

How to use argv with Spyder

I'm running the code below in Spyder.
I have typed it in a py file and simply hit the run button.
When I try to run it I get the error:
ValueError: need more than 1 value to unpack
As shown here you are meant to give the inputs for the argv variable before running the program but I don't know how to do this is spyder?
http://learnpythonthehardway.org/book/ex13.html
from sys import argv
script, first, second, third = argv
print "The script is called:", script
print "The first variable is:", first
print "The second variable is:", second
print "Your third variable is:", third
To pass argv to a script in Spyder, you need to go the menu entry
Run > Configuration per file
or press the Ctrl+F6 key, then look for the option called
Command line options
on the dialog that appears after that, and finally enter the command line arguments you want to pass to the script, which in this case could be
one two three
In addition to configuring in the Run->Configure as explained in other answers,
you can use "runfile" directly from the console.
Run the following:
runfile('ex13.py', args='first second third')
In Spyder, go Run > Configure and define your argv values as showing in following diagram and to run the script just press F6
Read the FAQ at the bottom of the page, it specifically mentions this error.
Common Student Questions
Q. When I run it I get ValueError: need more than 1 value to unpack.
Remember that an important skill is paying attention to details. If you look at the What You Should See section you see that I run the script with parameters on the command line. You should replicate how I ran it exactly.
Make sure you run the command:
$ python ex13.py first 2nd 3rd
>> The script is called: ex13.py
>> Your first variable is: first
>> Your second variable is: 2nd
>> Your third variable is: 3rd
You can ensure that the arguments are supplied.
if __name__ == '__main__':
if len(argv) == 4:
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
else:
print 'You forgot the args...'

Script will only run in Powershell

First off I looked in the documentation and found this:
http://www.tutorialspoint.com/python/python_command_line_arguments.htm
And then I also found this:
how to execute a python script file with an argument from inside another python script file
I am using pycharm 2.7.3 and when I try to run my script I get and error (the error is not pycharm related). THe only way around this error I have found out to be is to run the script through the windows PowerShell with a couple of "arguments" (is that what its called?)
The code that follows is what makes up my script, temp.py
from sys import argv
# this what the Ide fails to run
scripts, first, second, third = argv
print "The script is called:", scripts
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third
Next is what I types into the powershell
NOTE: The above code is temp.py
python temp.py first 2nd 3rd
1) What are those pieces of text called that appear after the "python temp.py", is it arguments?
2) Is it possible to run the code with the so called arguments in the IDE (I am using pyCharm)
3) If '2)' = True, how so?
1) What are those pieces of text called that appear after the "python temp.py", is it arguments?
Yes, the things you pass after temp.py on the shell are called "arguments", or "command line arguments". Sometimes they're called "parameters" instead, sometimes with the word "actual" as a prefix.*
And note that temp.py and all of its arguments are themselves arguments of python.
The sys.argv variable that you're using specifically gives you:
The list of command line arguments passed to a Python script…
2) Is it possible to run the code with the so called arguments in the IDE (I am using pyCharm)
Yes. Almost every IDE has a way to do this.
3) If '2)' = True, how so?
Read the PyCharm docs for details, but IIRC, they're part of the "run/debug configuration"; when you use the "Edit Configurations" command and edit one, there's a box labeled "Script Parameters" for you to enter the arguments.
* Using "parameters" is a bit misleading. Speaking technically, using the terminology Python uses for function calling, you should say that your script has a single parameter, a variable-positional parameter which gathers all of the arguments used to call it, which you can access as sys.argv. But as you can guess, nobody actually says things that way. If someone says "command line parameters" or "script parameters", they mean "the things in sys.argv".

Categories

Resources