Windows command line not displaying print commands

J

JonathanB

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

Irmen de Jong

JonathanB said:
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
 
J

John Machin

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?
 
J

JonathanB

Yes, just typing python takes me to my interactive prompt

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.
 
J

JonathanB

#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)
 
T

Terry Reedy

JonathanB said:
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
 
J

John Machin

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.
 
J

John Machin

Yes, just typing python takes me to my interactive prompt


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.

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
 
J

JonathanB

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top