Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Windows command line not displaying print commands

Reply
Thread Tools

Windows command line not displaying print commands

 
 
JonathanB
Guest
Posts: n/a
 
      03-30-2009
Ok, I'm sure this is really simple, but I cannot for the life of me
get any print statements from any of my python scripts to actually
print when I call them from the windows command line. What am I doing
wrong?

hello.py:
print "Hello World!"

command line:
E:\Python\dev>python hello.py

E:\Python\dev>


I'm using Python 2.6.1
 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      03-30-2009
JonathanB wrote:
> Ok, I'm sure this is really simple, but I cannot for the life of me
> get any print statements from any of my python scripts to actually
> print when I call them from the windows command line. What am I doing
> wrong?
>
> hello.py:
> print "Hello World!"
>
> command line:
> E:\Python\dev>python hello.py
>
> E:\Python\dev>
>


Does just typing:

python

give you an interactive prompt at all?

Or do you have a module in your E:\Python\dev directory called 'os', 'sys' or something
else that may clobber one of the default library modules.

-irmen
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      03-30-2009
On Mar 31, 8:37*am, Irmen de Jong <(E-Mail Removed)> wrote:
> JonathanB wrote:
> > Ok, I'm sure this is really simple, but I cannot for the life of me
> > get any print statements from any of my python scripts to actually
> > print when I call them from the windows command line. What am I doing
> > wrong?

>
> > hello.py:
> > print "Hello World!"

>
> > command line:
> > E:\Python\dev>python hello.py

>
> > E:\Python\dev>

>
> Does just typing:
>
> * *python
>
> give you an interactive prompt at all?
>
> Or do you have a module in your E:\Python\dev directory called 'os', 'sys' or something
> else that may clobber one of the default library modules.


or perhaps there's a file named python.bat that does nothing.

What directory is Python installed in? What does your Windows PATH
look like? Is this your very first attempt to do anything at all with
Python or have you managed to get any output from a Python script
before? If the latter, what have you changed in your environment? Does
E: refer to a removable disk?

 
Reply With Quote
 
JonathanB
Guest
Posts: n/a
 
      03-30-2009
On Mar 30, 6:28*pm, John Machin <(E-Mail Removed)> wrote:
> On Mar 31, 8:37*am, Irmen de Jong <(E-Mail Removed)> wrote:
> > Does just typing:

>
> > * *python


Yes, just typing python takes me to my interactive prompt

> > Or do you have a module in your E:\Python\dev directory called 'os', 'sys' or something
> > else that may clobber one of the default library modules.


The only module in the directory is called pyfind.py

>
> or perhaps there's a file named python.bat that does nothing.
>
> What directory is Python installed in? What does your Windows PATH
> look like? Is this your very first attempt to do anything at all with
> Python or have you managed to get any output from a Python script
> before? If the latter, what have you changed in your environment? Does
> E: refer to a removable disk?


Unfortunately, this problem is on my work computer, so I'm not in
front of it right now. I've done the development on this in
PortablePython, but I have python installed in C:/Python25 and that
should be in my path (I went though and added it). I've never run a
script that output to the command line before, only django apps.
Django will output stuff though, which makes me wonder if I've somehow
borked my stdout in the script. Not sure how I could have done that,
but I'll post the script I've written in the next post just in case
I'm somehow messing up the calls (although "print var" seems fairly
user-proof...). E: does refer to a removable disc.
 
Reply With Quote
 
JonathanB
Guest
Posts: n/a
 
      03-30-2009
#This is pyFind, a python replacement for find(1)
import os, sys, re, fnmatch
from os.path import join
from optparse import OptionParser

usage = "usage: %prog --name <filename> [directory1 directory2]"
parser = OptionParser(usage=usage)
parser.add_option("--regex", dest="regex",
help="REGEX MATCHING DOES NOT WORK AT THIS TIME, ONLY USE THE --name
OPTION!!!")
parser.add_option("--name", dest="name",
help="Unix style pattern matching search for a file by filename.")


def set_directories(args, directories):
"""initialize directory list to ensure that all subsequent loops
will
loop correctly. directories is a created list and args is the
positional
arguments from OptionParser."""
if args != []:
for arg in args:
#We have to use os.splitdrive() to support Windows. Otherwise you
cannot search
#different drive letters.
(drive, tail) = os.path.splitdrive(arg)
directories.append([drive, tail])
#For handling the default case, which is to search in current working
directory
else: directories.append(os.path.splitdrive(os.getcwd()) )

def regex_matcher(value, names):
if value == None:
return names
else:
regex = re.compile(value)
return [name for name in names if regex.search(name)]

def glob_matcher(value, names):
if value == None:
return names
else: return [name for name in names if fnmatch.fnmatch(name,
value)]

if __name__ == "__main__":
(options, args) = parser.parse_args()

directories = []
set_directories(args, directories)

for directory in directories:
#If we are in windows, directory[0] will be True. In that case we must
switch
#to the root of that drive letter before we can traverse to the
requested
#directory.
if directory[0] == True:
os.chdir(directory[0])
#Now that we know we are either in the right drive letter or in a Unix
#environment, we can change to the proper directory.
os.chdir(directory[1])
for root, dirs, files in os.walk(os.getcwd()):
# results = regex_matcher(options.regex, files)
# regex = re.compile(options.regex)
# results = [name for name in files if regex.search(name)]
results = glob_matcher(options.name, files)
if results != []: print"/n".join(results)
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      03-31-2009
JonathanB wrote:
> Ok, I'm sure this is really simple, but I cannot for the life of me
> get any print statements from any of my python scripts to actually
> print when I call them from the windows command line. What am I doing
> wrong?
>
> hello.py:
> print "Hello World!"
>
> command line:
> E:\Python\dev>python hello.py
>
> E:\Python\dev>
>
>
> I'm using Python 2.6.1


I suspect that it opens the window, prints to it, and closes it in a
blink of an eye. If so, adding an input prompt after the print will
stop the window from closing until you respond to the prompt.

a = input("hit return to close")

tjr

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-31-2009
On Mar 31, 11:42*am, Terry Reedy <(E-Mail Removed)> wrote:
> JonathanB wrote:
> > Ok, I'm sure this is really simple, but I cannot for the life of me
> > get any print statements from any of my python scripts to actually
> > print when I call them from the windows command line. What am I doing
> > wrong?

>
> > hello.py:
> > print "Hello World!"

>
> > command line:
> > E:\Python\dev>python hello.py

>
> > E:\Python\dev>

>
> > I'm using Python 2.6.1

>
> I suspect that it opens the window, prints to it, and closes it in a
> blink of an eye.


What window? He's *already* in a Command Prompt window, he's typing a
command "python hello.py", and getting only a blank line and another
prompt.


> *If so, adding an input prompt after the print will
> stop the window from closing until you respond to the prompt.
>
> a = input("hit return to close")
>
> tjr


 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-31-2009
On Mar 31, 9:57*am, JonathanB <(E-Mail Removed)> wrote:
> On Mar 30, 6:28*pm, John Machin <(E-Mail Removed)> wrote:
>
> > On Mar 31, 8:37*am, Irmen de Jong <(E-Mail Removed)> wrote:
> > > Does just typing:

>
> > > * *python

>
> Yes, just typing python takes me to my interactive prompt
>
> > > Or do you have a module in your E:\Python\dev directory called 'os', 'sys' or something
> > > else that may clobber one of the default library modules.

>
> The only module in the directory is called pyfind.py


So what do you classify hello.py as? A script?

Please tell us what other files are in the directory.


>
>
>
> > or perhaps there's a file named python.bat that does nothing.

>
> > What directory is Python installed in? What does your Windows PATH
> > look like? Is this your very first attempt to do anything at all with
> > Python or have you managed to get any output from a Python script
> > before? If the latter, what have you changed in your environment? Does
> > E: refer to a removable disk?

>
> Unfortunately, this problem is on my work computer, so I'm not in
> front of it right now. I've done the development on this in
> PortablePython, but I have python installed in C:/Python25 and that
> should be in my path (I went though and added it). I've never run a
> script that output to the command line before, only django apps.
> Django will output stuff though, which makes me wonder if I've somehow
> borked my stdout in the script. Not sure how I could have done that,
> but I'll post the script I've written in the next post just in case
> I'm somehow messing up the calls (although "print var" seems fairly
> user-proof...). E: does refer to a removable disc.


If hello.py doesn't print, then the problem is unlikely to be in your
big script.

I suspect that your best approach would be to (a) ensure that you have
the latest release of Portable Python [there was one in the last few
days] and (b) ask the author for help.

Other things to try that might diagnose where the problem really is:
just follow my example below.


| C:\junk>python -c "print 9876"
| 9876
|
| C:\junk>python
| Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on
| win32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> print "hello"
| hello
| >>> print 9876
| 9876
| >>> ^Z
|
|
| C:\junk>copy con test1.py
| print 9876
| ^Z
| 1 file(s) copied.
|
| C:\junk>python test1.py
| 9876
|
| C:\junk>copy con test2.py
| 1 / 0
| ^Z
| 1 file(s) copied.
|
| C:\junk>python test2.py
| Traceback (most recent call last):
| File "test2.py", line 1, in <module>
| 1 / 0
| ZeroDivisionError: integer division or modulo by zero
|

HTH,
John
 
Reply With Quote
 
JonathanB
Guest
Posts: n/a
 
      03-31-2009
I think I found the problem. I recently removed Python 2.5 and
replaced it with 2.6. When I got in, I tried to run some django
commands and even they weren't producing output. On a hunch, I tried
to uninstall 2.6 and reinstall it, since now even django wasn't
producing output. When I tried, it told me that I couldn't because it
wasn't installed. I had to delete the folder and manually go through
and delete every instance of "python" in my registry. However, when I
reinstalled 2.6, it worked. Some of the registry entries were still
pointing to the defunct Python25 path rather than Python26. Now both
the simple hello.py script and the bigger script that I really wanted
to get working are producing output.

I apologize for the confusion caused by going the wrong direction with
my troubleshooting (from the simplest possible script to the more
complex script), next time I will be more sensible in my
troubleshooting.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: commands.getstatusoutput result is not command line exit value!!! Hari Sekhon Python 1 10-02-2006 04:43 PM
Re: commands.getstatusoutput result is not command line exit value!!! Fredrik Lundh Python 0 10-02-2006 04:32 PM
commands.getstatusoutput result is not command line exit value!!! Hari Sekhon Python 0 10-02-2006 02:45 PM
Re: How to run commands in command line from a script Ivan Shevanski Python 3 07-01-2005 01:42 PM
How to run commands in command line from a script Ivan Shevanski Python 1 07-01-2005 03:09 AM



Advertisments