Newby Python Programming Question

Discussion in 'Python' started by Coyote, May 11, 2012.

  1. Coyote

    Coyote Guest

    Folks,

    I am migrating to Python after a 20+ year career writing IDL programs exclusively. I have a really simple question that I can't find the answer to in any of the books and tutorials I have been reading to get up to speed.

    I have two programs. The first is in a file I named file_utils.py:

    def pwd():
    import os
    print os.getcwd()

    The second is in a file I named pwd.py:

    import os
    print os.getcwd()

    Here is my question. I am using the Spyder IDE to run these programs. If I type these commands, I get exactly what I want, the name of my current directory:

    >>>from file_utils import pwd
    >>>pwd()

    C:\Users\coyote\pyscripts

    But, if I "run" the pwd.py script by selecting the "Run" option from the IDE menu, the directory is printed *twice* in the output window. Why is that?

    Thanks!

    Cheers,

    David
     
    Coyote, May 11, 2012
    #1
    1. Advertising

  2. Coyote

    Maarten Guest

    On Friday, May 11, 2012 5:25:20 PM UTC+2, Coyote wrote:

    > I am migrating to Python after a 20+ year career writing IDL programs exclusively. I have a really simple question that I can't find the answer to in any of the books and tutorials I have been reading to get up to speed.


    Welcome here.

    > I have two programs. The first is in a file I named file_utils.py:
    >
    > def pwd():
    > import os
    > print os.getcwd()


    Move the import tot the root level of the file:

    import os

    def pwd():
    print(os.getcwd())

    (and print() is a function these days)

    > The second is in a file I named pwd.py:
    >
    > import os
    > print os.getcwd()
    >
    > Here is my question. I am using the Spyder IDE to run these programs. If I type these commands, I get exactly what I want, the name of my current directory.
    >
    > But, if I "run" the pwd.py script by selecting the "Run" option from the IDE menu, the directory is printed *twice* in the output window. Why is that?


    I'd say this is a bug or feature of the IDE. If I use ipython I get:

    In [1]: %run mypwd
    /nobackup/users/maarten/tmp/coyote

    I tried to use Eclipse, but I don't think I'll do that again. I otherwise never use an IDE, it makes me confused. I don't know what the IDE is doing. Note that there is a standard module (at least on unix/linux) with the namepwd, but I guess that the IDE is not confused.

    I do recommend you read http://docs.python.org/howto/doanddont.html as a starting point to avoid learning some bad habits, especially on importing. You probably already found https://www.cfa.harvard.edu/~jbattat/computer/python/science/idl-numpy.html

    For scripts that are intended to be run from the command line, I use:

    #!/usr/bin/env python

    import os
    def pwd():
    return os.getcwd()

    if __name__ == "__main__":
    print(pwd())

    This will allow you to import the module (without side effects) and call the function, as well as 'running' the file. This increases the usefullness of the module. There are other advantages, especially when the scripts involves (many) variables.

    Maarten
     
    Maarten, May 11, 2012
    #2
    1. Advertising

  3. Coyote

    Coyote Guest

    Maarten writes:

    > I do recommend you read http://docs.python.org/howto/doanddont.html as a starting point to avoid learning some bad habits, especially on importing. You probably already found https://www.cfa.harvard.edu/~jbattat/computer/python/science/idl-numpy.html


    Yikes! I'm sure that first reference is in English, but I don't have much of an idea what it means yet. :)

    I have found the second reference very helpful. I've bookmarked both of these for future consideration.

    Thanks for your good advice.

    Cheers,

    David
     
    Coyote, May 11, 2012
    #3
  4. Coyote

    CM Guest

    On May 11, 11:25 am, Coyote <> wrote:
    > Folks,
    >
    > I am migrating to Python after a 20+ year career writing IDL programs exclusively. I have a really simple question that I can't find the answer to in any of the books and tutorials I have been reading to get up to speed.
    >
    > I have two programs. The first is in a file I named file_utils.py:
    >
    >    def pwd():
    >        import os
    >        print os.getcwd()
    >
    > The second is in a file I named pwd.py:
    >
    >    import os
    >    print os.getcwd()
    >
    > Here is my question. I am using the Spyder IDE to run these programs. If I type these commands, I get exactly what I want, the name of my current directory:
    >
    >    >>>from file_utils import pwd
    >    >>>pwd()
    >    C:\Users\coyote\pyscripts
    >
    > But, if I "run" the pwd.py script by selecting the "Run" option from the IDE menu, the directory is printed *twice* in the output window. Why is that?


    I don't know Spyder IDE, but I don't think this should happen; could
    there just be a simple mistake? Because you first refer to the .py
    file as 'file_utils.py' but then you refer to the file as
    'pwd.py'...which is also the name of your function. Room for
    confusion...so could you test this by saving only your one function
    (below), give the .py a new name to avoid confusion (like test_pwd.py)
    and then running *that* through Spyder IDE?

    def pwd():
    import os
    print os.getcwd()
     
    CM, May 11, 2012
    #4
  5. Coyote

    Coyote Guest

    CM writes:

    > I don't know Spyder IDE, but I don't think this should happen; could
    > there just be a simple mistake? Because you first refer to the .py
    > file as 'file_utils.py' but then you refer to the file as
    > 'pwd.py'...which is also the name of your function. Room for
    > confusion...so could you test this by saving only your one function
    > (below), give the .py a new name to avoid confusion (like test_pwd.py)
    > and then running *that* through Spyder IDE?
    >
    > def pwd():
    > import os
    > print os.getcwd()


    I probably explained the situation badly. I have a file pwd.py with these two lines of code in it:

    import os
    print os.getcwd()

    If I start a new Spyder IDL session and "run" this file by choosing RUN from the menu bar, the directory is printed twice. This appears to me now to be an IDE error, because if I use a runfile command, the directory is printed only once, as I expect.

    >>>runfile('pwd.py')

    C:\Users\coyote\pyscripts

    I've been playing around with a couple of IDEs because I liked the one I used with IDL and I wanted to use something similar for Python. The IDLDE wasan Eclipse variant, but I've tried installing Eclipse before for somethingelse and I'm pretty sure I don't need *that* kind of headache on a Friday afternoon. Unless, of course, I need a good excuse to head over to the Rio for the margaritas. :)

    Cheers,

    David
     
    Coyote, May 11, 2012
    #5
    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. Hermann W Ehlers

    Newby question

    Hermann W Ehlers, May 2, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    736
    Matt Berther
    May 5, 2004
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    ASP.NET easy newby question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    498
    Kevin Spencer
    Nov 23, 2004
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    697
    Iain Norman
    Feb 16, 2005
  4. Bruno Desthuilliers

    Python on the web - newby question

    Bruno Desthuilliers, Sep 3, 2008, in forum: Python
    Replies:
    2
    Views:
    260
    SimonPalmer
    Sep 3, 2008
  5. Sam Berry
    Replies:
    5
    Views:
    154
    Dave Angel
    Mar 29, 2013
Loading...

Share This Page