Newby Python Programming Question

C

Coyote

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:
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
 
M

Maarten

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
 
C

Coyote

Maarten said:
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
 
C

CM

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()
 
C

Coyote

CM said:
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.
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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top