This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 8 years ago.
So i've been at this one for a little while and cant seem to get it. Im trying to execute a python script via terminal and want to pass a string value with it. That way, when the script starts, it can check that value and act accordingly. Like this:
sudo python myscript.py mystring
How can i go about doing this. I know there's a way to start and stop a script using bash, but thats not really what im looking for. Any and all help accepted!
Try the following inside ur script:
import sys
arg1 = str(sys.argv[1])
print(arg1)
Since you are passing a string, you need to pass it in quotes:
sudo python myscript.py 'mystring'
Also, you shouldn't have to run it with sudo.
Related
This question already has answers here:
What is the difference between an inline variable assignment and a regular one in Bash?
(2 answers)
Closed 2 years ago.
I would like to execute a python file app.py like below:
python app.y
But I would like to pass to this file a new value = 'ok' for the env variable called 'training'.
How can I do this from shell with a single command?
I think the approach should be a little different:
set an environment variable: $ export training=ok
inside your Python script, read the environment variable:
import os
print(os.getenv('training'))
Another option would be to use parameters and parse them with something like argparse.
This question already has answers here:
drop into python interpreter while executing function
(3 answers)
Closed 5 years ago.
Is there a way to cause a python script to enter the interpreter at a given line? I know I can use import pdb; pdb.set_trace() to begin debugging at a certain line, but that means all the pdb commands may get in the way.
Is there a way to kick into the regular interpreter (or ipython) outside of pdb?
The easiest way is code.interact:
import code
code.interact(local={**globals(), **locals()})
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
This question already has answers here:
Pasting multiple lines into IDLE
(4 answers)
Closed 9 years ago.
I'm getting an error message quoting that I have a syntax error when trying to load my functions.
I can load them one at a time into the IDLE, but when pasting the full script, an error is returned. I believe its to do with the second function calling the first. To test this, the simple code below also returns this error :(.
def hello():
print('Hello there!')
def boo():
hello()
I'm unsure why this happens because the first function is defined before its called in the second. So it should be loaded in memory already shouldn't it?
Thanks for any help you can give. :)
IDLE interactive interpreter can only handle one task at once, you can't do more.
Save the script to a python file (.py extension), and run it.
From the command line:
$ cd /script_path
$ python script_name.py
Or if you want to run it in IDLE:
Ctrl+N - Paste the code - Save - Press F5
Hope this helps!
This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 20 days ago.
I would like to write a program that responds to inputs from the command line. Typically, I will start the program from the command line:
c:\>python my_responder.py
It will then give me a prompt:
responder> Hello. Please type your question.
responder> _
I will then type something, and hit enter:
responder> How many days are there in one week?
The responder will then reply with the result of a function (the input of which is the typed string). e.g.
def respond_to_input(input):
return 'You said: "{}". Please type something else.'.format(input)
responder> You said: "How many days are there in one week?". Please type something else.
I cannot seem to connect this kind of command line input / output to a function in python.
Additional Info: I have no understanding of stdin/stdout, and they feel relevant. I also have no understanding of how to get Python to interact with command line in general (other than running one python program that can print to the window).
Apart from raw_input() there is also the sys.argv list, (http://docs.python.org/2/tutorial/interpreter.html#argument-passing) which is pretty useful:
from __future__ import print_function # Only needed in python2.
from sys import argv as cli_args
def print_cli_args():
print(*cli_args[1:])
print_cli_args()
You would save it in a file, lets say echo.py and run it like this in the shell:
$ python2 echo.py Hello, World!
And it would print to stdout:
Hello, World!