Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run python script
#1
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)?
Reply
#2
(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?
Reply
#3
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
Reply
#4
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
Reply
#5
Thanks!!! :-)

Python 2.7
Reply
#6
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
Reply
#7
thank you so much, i will try this out later (working on something right now)

really appreciate your help!
Reply
#8
(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
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,171 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,868 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,287 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,615 May-11-2019, 08:12 PM
Last Post: keames
  How to run python script which has dependent python script in another folder? PrateekG 1 3,143 May-23-2018, 04:50 PM
Last Post: snippsat
  How to call one python script and use its output in another python script lravikumarvsp 3 32,375 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,821 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 6,178 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020