How to run a python program from command line and pass an argument to it?
Someone, please help me with this problem I have tried but I'm not able to do so.
How to run a python program from command line and pass an argument to it?
Someone, please help me with this problem I have tried but I'm not able to do so.
Your question a bit unclear for me but I'll try to answer. If you run .py extension file from the command line without virtualenv. just type:
python filename.py
Make sure you already in python folder.
But if you have virtualenv. you can run python anywhere using the same command or just type:
path/file_name.py
But if you confused on how to run compiled python file or you've make an exe from python file make sure you put input()
in the end to hold the screen or put your code under a conditional loop.
IF you want to run command line from your python program try use subprocess module.
A very basic example using sys,
import sys
for x in sys.argv:
print(f'Argument: {x}')
I'm using fstring which will work in python 3.6 if you're using 3.5 you can use print function as
print("Argument: ",x)
You can then run this file, whatever you've named it as
python filename.py hello
You'll get the following output,
Argument: test.py
Argument: hello