Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

I am runnning through 'Python the Hard Way' and it says this: "You know how you type python ex13.py to run the ex13.py file?" Ummmm... No! It doesn't run files that way. I have a Windows computer, how would I run files straight through the Shell?

OpenStudy (razor):

You can run IDLE (Python GUI) and open the file there and choose run module or press F5.

OpenStudy (anonymous):

The way Z. Shaw tells you to run your program is via the command prompt. It's a matter of taste/style, but you will need to learn both ways, even to understand "Python the Hard Way", because he uses sys.argv in several examples. To run via cmd prompt, change directory to the place where your python is located (assuming you saved the .py file in the same directory that python is installed, generally Program Files/PythonNN/ where NN is the version), and then just type python ex13.py on the prompt.

OpenStudy (anonymous):

oh I see... it doesn't work for me because I save my files in a different spot... what if I can't save my files there (I can't, it asks for the administrator access) is there a way to direct it?

OpenStudy (anonymous):

I think you can just try ex13.py straight into the cmd, don't know if it works on any Windows dist, but mine (Windows 7 Pro) works that way also

OpenStudy (razor):

hmm, haven't tried that. But you can add python to your PATH variable so you can type 'python' in any directory. You add them via System Properties->Environment Variables. Search for 'Path' under System Variables and edit them.

OpenStudy (anonymous):

python name.py doesn't run anything for me, I get an error... PATH eh? I'll try that. Thanks both :)

OpenStudy (anonymous):

Yup, that works also. Anyway, worst case scenario, you can use python path\ex13.py, for instance: python C:\Users\Joz1\foo\ex13.py

OpenStudy (anonymous):

I'm still having substantial trouble with this. But, 1 step at a time, I can't even seem to put the path properly, I get a syntax error. Please have a look at my screenshot which shows the path in the open idle window and the command I put to attempt to open it... http://www.freeimagehosting.net/2baf1

OpenStudy (anonymous):

oh - and in the Shell it shows I previously ran the program, using the usual 'run module' command from Idle... it does work, but I can't open it the path way. Also, I have since changed computers to one in which I have admin access, hasn't helped yet...

OpenStudy (anonymous):

And... Hi BwCA been I while since I was here... lol In see you are far more determined and dedicated than I...

OpenStudy (anonymous):

are you trying to do that in the operating system's shell? at a command prompt? or are you trying to do that in idle?

OpenStudy (anonymous):

I originally wanted to do that in the Python shell. After some discussion and googling and reading and playing I managed to call the function through the CMD. It was really quite gratifying. That was on the other computer however, where i can save my scripts in the main folder. Importing scripts into idle from anywhere appears to be no issue. I have not worked out either how to use Shell to do it nor to call scripts that are not saved in the main folder directly.

OpenStudy (anonymous):

Hello mate, you can try the built-in function execfile() or you could use a, at least for me, better way: import subprocess, then use the method subprocess.call("C:/PATH/PythonNN/Python.exe", "foo.py"], where PATH is the path for the python directory, NN is the version of python, and "foo.py" is the name or the script to run. You can even add at the end of this call arguments to be passed to the script, like "arg1", "arg2"] , if you used sys.argv in the script itself.

OpenStudy (anonymous):

By the way, two typos in my answer: it's subprocess.call(["etc"]) (forgot the open bracket and close the parenthesis), and foo.py is the name OF the script, I wrote "or" by mistake.

OpenStudy (anonymous):

Do you think the following is a fundamental problem for me... that I have Python the actual program etc saved in the traditional place and my personal files saved elsewhere? Would these things be easier if I downloaded and saved the actual Python in a different directory so that I could saved my work in the same place? In the explanation above, are you using Shell or CMD? I wish I could try it out on the other computer lol. Well I'm still playing with the code you've given me. Just about to go through the docs on subprocess... but I thought to ask the above question as it may be what my problem really is, or at least might make these type of things a whole lot easier... Thanks for your answer and patience I really struggle when it comes to things like this, my problems are probably really foolish lol.

OpenStudy (anonymous):

No problem, mate. Yeah, it's a python function/module, you can run in the Shell, but I think that if the script is located elsewhere, you have to call something like: subprocess.call(["C:/PATH/Python/Python.exe", "C:/PATH/foo.py"]). Wild guess here, but I think it's possible to do something like that.

OpenStudy (anonymous):

But yeah, it would be easier I think if you had everything together, or at least in the same parent directory, like a /programs/ directory inside /PythonNN/, where you would save your scripts, but it really is something that involves taste/adaptation.

OpenStudy (anonymous):

in implementing the call(["etc", "etc"]) I get returned a value of 0. I can't figure out just how to add arguments, every way I try seems to fail. When I get the 0, I can't for example get it to recognise any of the variables in my script, thus essentially failing to get it to do what I want. I think at this stage I will try again next time I can get on the other computer. i don't really want to reinstall Python at this stage.

OpenStudy (anonymous):

You didn't get the zero, your code actually ran, but it ran on a command prompt that flashed. Import time and add a time.sleep(5) at the end of your code. The 0 output is just Python saying that it exited without errors/exceptions.

OpenStudy (anonymous):

Oh! okay...

OpenStudy (anonymous):

Well... Yay! LOL It does run... THANKYOU!

OpenStudy (anonymous):

No problem, mate :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!