Hey everyone, I'm a new python user. I'm running SuSe linux development 10.

My problem is with importing files. Here is my file - it uses the myro library for robot programming.
/dev/rfcomm0 is the name of
#helloWorld.py

from myro import *
init("/dev/rfcomm0")

def hello():
forward(.5,.5)
stop()

hello()

---
From the command line::

me> python
Python 2.4.2 (#1, Aug 1 2008, 00:15:16)
[GCC 4.1.2 20070115 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloWorld
WARNING: xmpp was not found: chat and webservices will not be available
(c) 2006-2007 Institute for Personal Robots in Education
[See http://www.roboteducation.org/ for more information]
Myro version 2.8.2 is ready!
WARNING: sound did not load; need tkSnack?
You are using fluke firmware 2.7.4
You are using scribbler firmware 2.6.1
Hello, I'm Scribby!
>>> helloWorld.hello()
[does what it will]

NOW HERE IS THE PROBLEM. HOW DO I GO BACK AND EDIT MY FILE WITHOUT COMPLETELY RESTARTING PYTHON?

Recommended Answers

All 4 Replies

I think the easiest way is that you run python's built-in IDE which is called idle . The modules of idle should be a folder like /usr/lib/python2.4/idlelib . On my mandriva system, I do this by putting a small executable called $HOME/bin/idle and which contains the following (I found this somewhere on the web)

#!/usr/bin/env python
try:
    import idlelib.PyShell
except ImportError:
    # IDLE is not installed, but maybe PyShell is on sys.path:
    try:
        import PyShell
    except ImportError:
        raise
    else:
        import os
        idledir = os.path.dirname(os.path.abspath(PyShell.__file__))
        if idledir != os.getcwd():
            # We're not in the IDLE directory, help the subprocess find run.py
            pypath = os.environ.get('PYTHONPATH', '')
            if pypath:
                os.environ['PYTHONPATH'] = pypath + ':' + idledir
            else:
                os.environ['PYTHONPATH'] = idledir
        PyShell.main()
else:
    idlelib.PyShell.main()

I then chmod +x $HOME/bin/idle and then I just type idle when I want to run the IDE. Now, when you launch idle, it's starts a python shell window with the '>>>' prompt, as if you have typed 'python' in a console. In the File menu, you can choose to open a file, namely you open your program in a new (editor window). In this window, you can edit your file. When you want to run your program, you go to the Run menu of the editor window and select 'Run module'. This will refresh the shell and run the program in the python shell. You can the re-edit and re-launch ! :)

Assuming the name of your program is helloWorld.py, you would add the first line in the following example which tells the OS to use Python to run this program. Also add the "__main__" section at the bottom and make the file executable by your user name. You can then enter the program's name on the command line, or highlight it and hit Enter if you use MidnightCommander or something similiar, and the program will run. __main__ is usually for testing. It is only called when this program is executed so if you call the function hello() from another program, the __main__ section will be ignored. You can then use any text editor or word processor, or an IDE like Idle to modify and save the file, and then enter helloWorld.py on the command line to run if again. I think a good many of us do not use IDE's and instead have a text editor and terminal window open at the same time. So it's, change the code and save it, run it in the terminal, check the output, change the code and save it, etc.

#!/usr/bin/env python
#
# if that doesn't work then use call python directly as in the following line
##   #!/usr/bin/python2.5
#
#helloWorld.py

from myro import *
init("/dev/rfcomm0")

def hello():
     forward(.5,.5)
     stop()

if __name__ == "__main__":
   hello()

On certain linuxes and certain versions of idle, there is a problem with Idle's editor window: you cannot position the cursor in the editor window by clicking with the left mouse button. A solution to this is to hold the Ctrl key while clicking in the editor window with the left mouse button.

Just try Wing IDE from www.wingware.com use 101 version, also eclipse IDE here at www.eclipse.org and get pydev plugin for python support, also Netbeans IDE here at www.netbeans.org and get its python support at www.nbpython.dev.java.com. Or you can go for drpython, pyscripter, and all other IDE stuffs. They will help you run python shell right in the IDE, debug your code and do all the necessary. Some versions are for hobbyists so lack some features.
If you think of serious programming with wing IDE get either personal or professional

Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.