Run the code through console by pycharm - python

Hi I am new to programming and pycharm. I want to run the code below through the python console. I need to enter 3 values to run it. I tried python p13.py one two three but it didn't work.
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
Could you help me with what am I meant to type into the python consle in pycharm in order to run this.

Related

Running a program in Spyder that takes a command line argument is giving me list index out of range error [duplicate]

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...'

Learn Python the hard way Exercise 13 (Import) no response in powershell

I'm a beginner, learning python with the book "Learn Python the hard way"
Exercise 13 is about importing. This is the code I am meant to run through powershell.
from sys import argv
# read the WYSS section for how to run this
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)
In windows powershell I am typing:
python ex13.py first 2nd 3rd
I am getting no errors, nothing at all.
I am writing the code in Notepad++ and the file name is ex13.py

Script Parameters with PyCharm - Learn Python the Hard way

I started to learn Python with learn Python the Hard Way and I am facing some issues with ex13. I would like to now if it is a mistake of mine or because of the way PyCharm works.
Basically to make the script work as the exercise suggests I saw I have to manually enter the parameters names in PyCharm with run>edit configuration
I put "first" "second" and "third"
But I would like to combine raw_input and argv so I can let the user choose the name of the parameters. Here's what I wrote:
from sys import argv
first = raw_input("First argument: ")
second = raw_input("Second argument: ")
third = raw_input("Third argument: ")
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
It runs but it returns:
ValueError: need more than 1 value to unpack
It seems that in PyCharm I have to enter manually all the script parameters ? There is no way to combine it with raw input ?
Thanks for your help.
note check out Joran's answer which shows a good combination of using command line args and prompting for user inputs. Below is a break down of what is going on:
This is expected behaviour in PyCharm to specify the parameters you want PyCharm to execute your script with. Think of it like PyCharm doing something like this:
python my_script.py
However, PyCharm does not know the arguments you want to pass, you need to provide this, so it knows how to run your script. If you look near the bottom of your PyCharm window, there is a Terminal tab. You can use that to quickly execute your script and provide args.
Secondly, the reason why you are getting the error you are getting is because you are not handling your script inputs properly when trying to use argv.
You are mixing up using raw_input, which takes in user input when your Python script is running, as opposed to argv which takes parameters in to your Python script when you run it.
So, what you are looking to actually do here, is not use raw_input, but simply argv. Here is a small demo to clarify all this:
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
Now, go in to your command prompt and do this:
python my_script one two three
You will get:
The script is called: my_script.py
Your first variable is: one
Your second variable is: two
Your third variable is: three
This is a very simplified example, and you're probably going to need to add some handling of your inputs, or you're going to get a lot of errors with different inputs to your script. With that, I suggest maybe looking at argparse instead
Im not sure i understand the question ... but the code below will use the command line arguments if there are 3(or more) ... otherwise it will prompt and split the same way as the shell splits command line arguments
import shlex # shlex.split will split the string the same way that the shell would for command line args
if len(sys.argv) < 3:
args = (list(sys.argv) + shlex.split(raw_input("Enter Args:")))[:3]
else:
args = sys.argv[:3]
print "Using Args:",args
one,two,three = args

Learn Python The Hard Way Exercise 13 (using pycharm) - execute a script in console

I am obviously new to python and I've spent the last 1.5 hours beating my head against the wall trying to get this exercise done. It should be relatively simple, and I'm sure using an IDE when I'm new is probably not doing me any favors.
Basically I have this script:
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 type the above in the editor and then I have the file saved as "ex13.py". When I then try to run the script in the python console I am continually told that the name is not defined. I would really appreciate any help getting this resolved, I am basically stuck and can't progress until I figure out how to run this damn script in pycharm.
Under the run dropdown menu at the top of the pycharm IDE you should find edit configurations. Click on that and write your three arguments in script paramaters. Then run it
in that book pointed you need pass three argument in terminal like this
python ex13.py first 2nd 3rd
and if you pass more than 3 argument like what am i did, it will make you an error, like this:
python .\ex13.py first 2nd 3rd 4th
Traceback (most recent call last):
File ".\ex13.py", line 3, in <module>
script,first,second,third= argv
ValueError: too many values to unpack
so, pass just three argument according to that code in book, Ex13, you'll see it work

In python, what does script mean?

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
basically, does script here mean the file of the programm? like ex14.py?
im complete new to python and prgramming...if this question sound stupid to you all.
Running it with the arguments: 1 2 3
Output:
The script is called: E:\Proyectos\Eclipse\Python\Test\__init__.py
Your first variable is: 1
Your second variable is: 2
Your third variable is: 3
The first line prints the script name. Quoting Python Docs:
... argv[0] is the script name (it is operating system dependent whether this is a full pathname or not).
The following 3 lines just prints the arguments I passed 1 2 3.

Categories