Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Help 

How do I Run a File from Gedit Using Command Prompt through Python?


I am currently reading "Learn Python the Hard Way" as a means to learn the programming language python. However, once I reached exercise 1 and followed the instructions, the outcome was not satisfactory.
The exercise asks for me to create a text document using the text editor "Gedit." It tells me then, to name the file "ex1.py" From there it tells me to run the file using the terminal (command prompt) by simply typing in "python ex1.py"
Nothing happens but an error. So I tried something else I type in "start ex1.py" and python pops up for like five seconds before closing out again. I feel like I'm on the right track, however my spirits are beginning to fall. Please help me!
I use Windows 7 (which I understand is not the best for learning python, but I am determined to do so)
What error do you get? Have you added Python to your path? (type "python" in cmd)
Lastly, are you sure you have no errors in the code?
Crypto

Well first the error was pointing at something I had done wrong, after I fixed it, nothing happened, I'd press enter and literally nothing would occur. After that happened was when I added "start" at the beginning which made python pop up but close out after less than five seconds. :/
(Dec 8, 2012 03:00 PM)Hawyke Wrote:  Well first the error was pointing at something I had done wrong, after I fixed it, nothing happened, I'd press enter and literally nothing would occur. After that happened was when I added "start" at the beginning which made python pop up but close out after less than five seconds. :/

Python compiles at runtime, you must use "python file.py" for that to work. Starting the file will just open it with the default program.

What happens when you only type "python" in the cmd?
Crypto

(Dec 8, 2012 03:04 PM)Mohatu Wrote:  Python compiles at runtime, you must use "python file.py" for that to work. Starting the file will just open it with the default program.

What happens when you only type "python" in the cmd?

It says
" 'python' is not recognized as an internal or external command, operable program or batch file."

but if I put "start python" it opens python.


I was told to try running python using the full path, and I did, and it worked. This is what comes up:
"C:\Users\Name>C:\Python27\python.exe
Python 2.7.3 <default, Apr 10 2012, 23:31:26> [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>>
So then I tried adding the following text: python ex1.py afterwards so that the last line looked like this:
Type "help", "copyright", "credits" or "license" for more information.
>>> python ex1.py
And this is the message that followed:
"File "<stdin>", line 1
Python ex1.py
^
SyntaxError: invalid syntax
>>>
(Dec 8, 2012 09:56 PM)Hawyke Wrote:  It says
" 'python' is not recognized as an internal or external command, operable program or batch file."

but if I put "start python" it opens python.


I was told to try running python using the full path, and I did, and it worked. This is what comes up:
"C:\Users\Name>C:\Python27\python.exe
Python 2.7.3 <default, Apr 10 2012, 23:31:26> [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>>
So then I tried adding the following text: python ex1.py afterwards so that the last line looked like this:
Type "help", "copyright", "credits" or "license" for more information.
>>> python ex1.py
And this is the message that followed:
"File "<stdin>", line 1
Python ex1.py
^
SyntaxError: invalid syntax
>>>

Alright so python was not added to your path, search on Google how to do that, it's very simple.

The second error you're getting means you have a problem with your code, are you sure you copied it exactly how it is in the tutorial? If so, post it here, I'll take a look at it.
Crypto

Try doing :
Code
C:\Python27\python.exe yourfile.py

After adding C:\python27 to the environment variables (like Mohatu said), you can do python file.py and it will successfully run it. Maybe then you can get Geany for Windows and use it as a lightweight IDE, it supports a variety of languages including python. Or some other IDE, I don't know much about those though..
(Dec 9, 2012 05:13 AM)Mohatu Wrote:  Alright so python was not added to your path, search on Google how to do that, it's very simple.

The second error you're getting means you have a problem with your code, are you sure you copied it exactly how it is in the tutorial? If so, post it here, I'll take a look at it.

Alright, I added python to my path. I was told that the reason it didn't work the way I did it was because it was through python and not through command prompt, so I tired again like this:
At first I got the [errno2] message, but then I did change the directory over to where the file is located, typed in the command and then absolutely nothing happened. This is what my command prompt screen looks like:
C:\Users\Name>cd test1
C:\Users\Name\test1>python ex1.py
C:\Users\Name\test1>

But here is what the tutorial tells me to type into gedit and what should happen:
1 "Hello World!"
2 "Hello Again"
3 "I like typing this."
4 "This is fun."
5 'Yay! Printing.'
6 "I'd much rather you 'not'."
7 'I "said" do not touch this.'

Once that is what the Gedit screen looks like, I was to save it under the file name: ex1.py and then the following instructions were given:
"Then in Terminal run the file by typing: python ex1.py
If you did it right then you should see the same output as I have below. If not you did something wrong. No, the computer is not wrong.
What you should see:
$ python ex1.py
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.
$

However, obviously that is not what is happening to me. And I promise I typed it into gedit verbatim, I checked it over 10 times.
I even started over, uninstalled gedit and reinstalled it and recreated the file verbatim and the same exact thing happened: Absolutely nothing.


(Dec 9, 2012 10:26 AM)Eight Wrote:  Try doing :
Code
C:\Python27\python.exe yourfile.py

After adding C:\python27 to the environment variables (like Mohatu said), you can do python file.py and it will successfully run it. Maybe then you can get Geany for Windows and use it as a lightweight IDE, it supports a variety of languages including python. Or some other IDE, I don't know much about those though..

The same thing happened as I explained in my response to Mohatu, nothing. :(


Also, I just received the following e-mail from the author of the course:
"You don't do it from gedit actually, you do it from powershell. Are you familiar with powershell?"

However, the issue is, is that powershell appears to be nothing more than another terminal just like command prompt, following the same exact syntax and everything, just under a different name. How I am supposed to use gedit and powershell interchangeably is beyond me. Please explain this.
I would suggest downloading the Python Eclipse addon.

They have syntax highlighting and a run button to make running your code a hell of a lot easier.
Mind giving a link to the tutorial ? I'm no python expert, but as far as I know, to print something to the screen you use the print construct :
PHP Code

<?php 
print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.' 
(Dec 9, 2012 04:03 PM)Eight Wrote:  Mind giving a link to the tutorial ? I'm no python expert, but as far as I know, to print something to the screen you use the print construct :
PHP Code

<?php 
print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.' 

There's not a link to the tutorial, it's a book. I copied the directions from exercise 1 verbatim just as they are in the book on here already.


(Dec 9, 2012 03:46 PM)Gpure Wrote:  I would suggest downloading the Python Eclipse addon.

They have syntax highlighting and a run button to make running your code a hell of a lot easier.

I understand that there are tools available to make things easier, but that's not what I'm looking for right now, I want to /understand/ python and be able to use it without addons first and then when I get to be more advanced in the language I'll start using them. Thanks though, I'll look into it at a later date. o/
Does Windows even have Gedit by default?
(Dec 9, 2012 07:57 PM)Greyersting Wrote:  Does Windows even have Gedit by default?

No? You have to install it separately.
To Add Python to your path variable:
Code
@echo off
set bckpath=%path%
echo %bckpath% > backuppath.txt
setx %bckpath%;C:\Python26 /M

Run it as admin.

[Image: gaqzA.png]
(Dec 9, 2012 09:52 PM)Greenp0ison Wrote:  To Add Python to your path variable:
Code
@echo off
set bckpath=%path%
echo %bckpath% > backuppath.txt
setx %bckpath%;C:\Python26 /M

Run it as admin.

Thanks, but I've already added python to my path variable. o/


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Help] by chatgptproApr 3, 2024 01:42 AM 9 177 Apr 3, 2024 01:42 AM
Last Post: siliconvalley
by apknanJan 30, 2024 05:58 PM 2 356 Jan 30, 2024 05:58 PM
Last Post: Klonopin
by 41phafr34kJan 31, 2017 11:03 PM 4 121 Jan 31, 2017 11:03 PM
Last Post: 41phafr34k