Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newby Python Programming Question

Reply
Thread Tools

Newby Python Programming Question

 
 
Coyote
Guest
Posts: n/a
 
      05-11-2012
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


 
Reply With Quote
 
 
 
 
Maarten
Guest
Posts: n/a
 
      05-11-2012
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...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
 
Reply With Quote
 
 
 
 
Coyote
Guest
Posts: n/a
 
      05-11-2012
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...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

 
Reply With Quote
 
CM
Guest
Posts: n/a
 
      05-11-2012
On May 11, 11:25*am, Coyote <(E-Mail Removed)> 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()

 
Reply With Quote
 
Coyote
Guest
Posts: n/a
 
      05-11-2012
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
 
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
Python class and variable issue(newby question!) Sam Berry Python 5 03-29-2013 10:56 PM
Python on the web - newby question Bruno Desthuilliers Python 2 09-03-2008 09:59 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
ASP.NET easy newby question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 6 11-23-2004 06:15 PM
Newby question Hermann W Ehlers ASP .Net 12 05-05-2004 06:03 AM



Advertisments