Windows command line not displaying print commands

Discussion in 'Python' started by JonathanB, Mar 30, 2009.

  1. JonathanB

    JonathanB Guest

    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
     
    JonathanB, Mar 30, 2009
    #1
    1. Advertising

  2. 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
     
    Irmen de Jong, Mar 30, 2009
    #2
    1. Advertising

  3. JonathanB

    John Machin Guest

    On Mar 31, 8:37 am, Irmen de Jong <> 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?
     
    John Machin, Mar 30, 2009
    #3
  4. JonathanB

    JonathanB Guest

    On Mar 30, 6:28 pm, John Machin <> wrote:
    > On Mar 31, 8:37 am, Irmen de Jong <> 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.
     
    JonathanB, Mar 30, 2009
    #4
  5. JonathanB

    JonathanB Guest

    #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)
     
    JonathanB, Mar 31, 2009
    #5
  6. JonathanB

    Terry Reedy Guest

    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
     
    Terry Reedy, Mar 31, 2009
    #6
  7. JonathanB

    John Machin Guest

    On Mar 31, 11:42 am, Terry Reedy <> 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
     
    John Machin, Mar 31, 2009
    #7
  8. JonathanB

    John Machin Guest

    On Mar 31, 9:57 am, JonathanB <> wrote:
    > On Mar 30, 6:28 pm, John Machin <> wrote:
    >
    > > On Mar 31, 8:37 am, Irmen de Jong <> 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
     
    John Machin, Mar 31, 2009
    #8
  9. JonathanB

    JonathanB Guest

    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.
     
    JonathanB, Mar 31, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Shevanski
    Replies:
    3
    Views:
    405
    Mike Meyer
    Jul 1, 2005
  2. Ivan Shevanski
    Replies:
    1
    Views:
    398
    Peter Hansen
    Jul 1, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    335
    Hari Sekhon
    Oct 2, 2006
  4. Hari Sekhon
    Replies:
    1
    Views:
    872
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 2, 2006
  5. Fredrik Lundh
    Replies:
    0
    Views:
    439
    Fredrik Lundh
    Oct 2, 2006
Loading...

Share This Page