1

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.

1
  • Can you clarify how exactly are you looking to combine? Are you wanting to check if enough parameters were provided, and if not, prompt for them? Commented Mar 8, 2016 at 4:16

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

I think maybe OP is actually asking how to accept 3 arguments from command line but if they are not present to prompt for them ... (although I would not bet money on that interpretation)
@JoranBeasley re-reading the question, I can see what you mean now. Hopefully, OP will chime in and clarify. I'll ping them in their question.
Thank you very much! I didn't understand I should use the terminal.
2

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

1 Comment

Yeah. This looks like what OP is looking for. I'll put a note in my answer too to reference yours

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.