Trying to Learn Python: Problems running Python Scripts
Hello,
I am trying to learn the basics of using Python (version 2.7) using Google's free course (link to the overview). If you check out the set up, they talk about a script "hello.py" to run to see if things are installed properly.
I can pull up python on my computer and use it for basic commands, such as setting values and do math with those values, but I cannot get it to find the hello.py script on my computer when I type "python hello.py" into Python.
Any ideas where I have made an error?
Thanks for the assistance!
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
Mith
I am trying to learn the basics of using Python (version 2.7) using Google's free course
(link to the overview). If you check out the set up, they talk about a script "hello.py" to run to see if things are installed properly.
I can pull up python on my computer and use it for basic commands, such as setting values and do math with those values, but I cannot get it to find the hello.py script on my computer when I type "python hello.py" into Python.
Any ideas where I have made an error?
You'd type "python hello.py" into a system command prompt, not into Python itself. That's what the "open a command line" bit in the Python Set Up section is trying to tell you.
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
Jasdoif
You'd type "python hello.py" into a system command prompt, not into Python itself. That's what the "open a command line" bit in the Python Set Up section is trying to tell you.
WHen I do that, I get the following:
"python: can't open file "hello.py" ..."No such file or directory"
But I have the file "hello.py" in a Documents folder on my computer. Should I be moving this to a different location for Python to find it?
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
Mith
WHen I do that, I get the following:
"python: can't open file "hello.py" ..."No such file or directory"
But I have the file "hello.py" in a Documents folder on my computer. Should I be moving this to a different location for Python to find it?
Well, you could move the file and/or change where the command prompt is running programs from; as long as it's the same place, it should work. A thorough walkthrough of command prompt navigation is...too much for me to provide here.
However, since I'm lazy and I see the Google page gives you an entire zipped folder of their Python exercises....What I'd do, assuming you're running Windows, is copy the one "google-python-exercises" folder in their zip file (the folder itself, not just the files in it) to the top of the drive; so you'd have a new "google-python-exercises" folder there. Then when you open the command prompt, you should be able to do
Code:
cd \google-python-exercises
to change the directory the command prompt's running from to that folder, and then "python hello.py" should be able to find the file it's looking for.
Re: Trying to Learn Python: Problems running Python Scripts
You can also drag and drop a file from "Windows Explorer" into the command line and it'll give the full path to that file. You could type "python" and then instead of typing hello.py open up Windows Explorer, find your hello.py file wherever you saved it and "drag" it into the command line. You should get something like python "C:\Users\Mith\Documents\hello.py" when you do that.
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
Jasdoif
Well, you could move the file and/or change where the command prompt is running programs from; as long as it's the same place, it should work. A thorough walkthrough of command prompt navigation is...too much for me to provide here.
However, since I'm lazy and I see the Google page gives you an entire zipped folder of their Python exercises....What
I'd do, assuming you're running Windows, is copy the one "google-python-exercises" folder in their zip file (the folder itself, not just the files in it) to the top of the drive; so you'd have a new "google-python-exercises" folder there. Then when you open the command prompt, you should be able to do
Code:
cd \google-python-exercises
to
change the
directory the command prompt's running from to that folder, and
then "python hello.py" should be able to find the file it's looking for.
Thank you for your response. I apologise for the silly question. I am thinking the problem is that I am working on the ground floor without a foundation, so to speak. I had the files stored to be in a folder that the initial directory (so the path using the central filing system was (C:)>Users>Mith>google-python-exercises. My reasoning was that "(C:)>Users>Mith>" is where the directory already was when starting the command window. The file and program will just be extended from there.
From what I can gather now that I can get things to work, it needs to be (C:)>google-python-exercises.
If this comes across as incorrect, it's because I written things poorly, since I at least got things working.
Once again, thank you for your assistance. I apologise for any headaches I may have caused.
Re: Trying to Learn Python: Problems running Python Scripts
A quick and easy way to get to the correct directory is hold shift and left click inside the directory. An extra option of "open command line here" (or "open powershell here") will appear that will automatically be in the correct folder.
Re: Trying to Learn Python: Problems running Python Scripts
Does your python come with Idle? If so it is probably easier to edit with Idle and simply hit [F5] to run your script.
There must be better python IDEs out there, but I got used to IDLE and still use it (it is *extremely* primitive as an IDE, but I just like primitive tools. And it generally won't get in your way while learning python (you don't waste time figuring out if you issue is with your program or your tool usage).
It might also be better to start with python 3 as the python world might finally be going that way. It isn't *that* big a deal (mostly in how printing is handled, and remembering that you don't have to bother with "xrange"). Since pypy (and plenty of other projects) are still on 2 it isn't a big deal but I suspect that you might be picking up old habits.
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
Mith
From what I can gather now that I can get things to work, it needs to be (C:)>google-python-exercises.
There's a basic primer on console commands here, but just remembering 'cd' and 'dir' will get you a long way.
I'd recommend using PyDev with Eclipse, but if you're still figuring out the command-line that might be a little complicated?
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
wumpus
Does your python come with Idle? If so it is probably easier to edit with Idle and simply hit [F5] to run your script.
There must be better python IDEs out there, but I got used to IDLE and still use it (it is *extremely* primitive as an IDE, but I just like primitive tools. And it generally won't get in your way while learning python (you don't waste time figuring out if you issue is with your program or your tool usage).
It might also be better to start with python 3 as the python world might finally be going that way. It isn't *that* big a deal (mostly in how printing is handled, and remembering that you don't have to bother with "xrange"). Since pypy (and plenty of other projects) are still on 2 it isn't a big deal but I suspect that you might be picking up old habits.
The free course I'm using as an introduction was developed for lat python 2, so I figure I should start there. And I do have access to IDLE.
Re: Trying to Learn Python: Problems running Python Scripts
Quote:
Originally Posted by
Mastikator
A quick and easy way to get to the correct directory is hold shift and left click inside the directory. An extra option of "open command line here" (or "open powershell here") will appear that will automatically be in the correct folder.
Good to know!
Re: Trying to Learn Python: Problems running Python Scripts
back in ye olden days you would have to add the python fodler to the "system environment" variable. Thus you could access the python executible from where ever you happened to be. Not sure how this is done anymore. Hopefully the installation did it for you.
But your problem is basically the same as back when. The python file you are calling is not in the place it's looking because it's only looking in the folder you are currently in.
If you don't have python registered to be able to run from everywhere you'll have the opposite problem trying to run said file from the folder it resides in.