Python Forum
Run python script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Run python script (/thread-7626.html)



Run python script - AndyDD_UK - Jan-18-2018

Hi

Newbie here. I have searched for this but cannot see an answer, apologies if I've missed something...

I'm currently learning python but am running a script someone has created each day.

The way they've told me to run the script (laptop/windows) is to go into the cmd line and then to locate the programme eg by typing cd desktop then cd folder_name and continue for each subfolder

Then to python programme_name.py and return

This feels a bit long winded

I've opened up the programme_name.py file directly and pressed return and then it (appears to) run ok.

Is this the best way of executing the code (ie rather than via cmd line)?


RE: Run python script - buran - Jan-18-2018

(Jan-18-2018, 10:41 AM)AndyDD_UK Wrote: I've opened up the programme_name.py file directly and pressed return and then it (appears to) run ok.
where did you open the program?
Or do you mean that you browsed to program location in Explorer, select it and run it by simply hit Enter?


RE: Run python script - AndyDD_UK - Jan-18-2018

hi, thanks very much for your prompt reply!

its in a folder on my desktop so I opened the folder, then right-click-open the python file then pressed enter

this seems a much easier/quicker way to execute the file, unless i'm missing something (am newbie!)

many thanks


RE: Run python script - buran - Jan-18-2018

in general case it's not a problem to do what you do, as long as on windows .py extension is associated with the python interpreter.
There are plenty of IFs:

1. If you have multiple python installations - e.g. python2, python3 it's not clear which interpreter will run the script. It will depend on your environmental variables
2. If your script expects some sort of command line arguments (it looks this is not the case in this particular example), these command line arguments will be missing if you start the script by double click on it/hit Enter. In particular cases this could be resolved by creating shortcut with specific command line arguments included in the shortcut properties. Or by creating bat file to execute it.
3.If your script has some output, it MAY not be possible to see the output itself. I.e. console window will close immediately after script finish.

What python version do you use - I will give some example of the above


RE: Run python script - AndyDD_UK - Jan-18-2018

Thanks!!! :-)

Python 2.7


RE: Run python script - buran - Jan-18-2018

Open your Notepad (or other text editor) and put this in file named myscript.py

print 'Hello, World'
save it. If you click on myscript.py comamnd prompt window will open, print Hello, World and will close, before you can even read it.
Now, try to run it from the command promprt - the way they told you to start python file

c:\>path_to_script>python myscript.py

now, add second line
raw_input('Press any key...')
Start it by double clicking - now the command prompt window will stay open until you press some key.

Now, replace the two lines with following
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('name', help='name of the person to greet')
args = parser.parse_args()

print 'Hello, {}'.format(args.name)
Start it by double-click and from the command line. In the former you will not see the output, while in the latter you will see that it expects some argument.
Now start it from command line with c:\>path_to_script>python myscript.py AndyDD

Now, add at the bottom of the file
raw_input('Press any key...')
Now, create new file runme.bat (with the text editor). put in it following
python myscript.py AndyDD
now, run it by double click on runme.bat
This is how you can create bat file to run the script and supply some arguments.

bottom line - it depend on your script, furthermore if you want and are allowed to change it - you can introduce different functionality


RE: Run python script - AndyDD_UK - Jan-18-2018

thank you so much, i will try this out later (working on something right now)

really appreciate your help!


RE: Run python script - snippsat - Jan-18-2018

(Jan-18-2018, 11:32 AM)AndyDD_UK Wrote: Python 2.7
As a new user you start with Python 3.
Python 3.6 and pip installation under Windows
Python environment Windows


RE: Run python script - buran - Jan-18-2018

I was about to make the same comment, however OP just runs a script they inherited from someone else and it's running 2.7. No much flexibility, unless they are willing to port to 3