Re: [Tutor] Runing a Python program
> On my computer (Win2K) Python puts the current working directory in > sys.path. (I'm not sure this happens on Linux.) yes it does, on any unix-flavored system (Linux, FreeBSD, MacOS X, Solaris, etc.). since we're on the topic, there is another attribute in the sys module, sys.modules that shows you all the imported (and loaded) modules and where they live in the filesystem. it's a good way to figure out path problems too. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Runing a Python program
Hi Henry, > As a new python programmer, I created a directory in > 'C:\python24\myPythonFiles', and added a simple python > under the myPythonFiles directory; but when I tried > running it on the Python Shell, I got the following error. > >>> import myPythonFiles.readOut > ImportError: No module named myPythonFiles.readOut Ok, the first thing to say is that you are not running the program but importing it, there is a difference. To run a Python program in Windows either double click it in Windows Explorer (it should have a snake icon to show that the association is set up correctly) or at a DOS command prompt (or the Start->Run dialog) type C:\> python C:\python24\myPythonFiles\readOut.py In either case you may find the program runs so fast you can't see the output. In that case add a final line like: raw_input("Hit ENTER to quit") When you import a module it does indeed execute the code in that module and so might appear to run the program, but there is one important distinction (which you may not have come across yet!) You can put a clause into the module like: if __name__ == "__main__": # some code here and the code under the if will only be executed when the file is run as a program, it will NOT be executed when the file is imported as a module. (This is a powerful feature that greatly eases the task of writing reusable code in Python.) Now the second thing to note is that your import syntax is wrong. To make the files in your directory visible toi the import command you need to modify the value in sys.path. There are a number of ways to do this, the one that I use is to create a PYTHONPATH environment variable (MyComputer->Properties->Advanced->Environment Variables) which points to all the folders where I keep Python code, in the same way that the DOS PATH variable points to my executable files. HTH, Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Runing a Python program
Henry Dominik wrote: > Hello people, > > As a new python programmer, I created a directory in > 'C:\python24\myPythonFiles', > and added a simple python under the myPythonFiles directory; but when I > tried running it on the Python Shell, I got the following error. > > >>> import myPythonFiles.readOut To be able to import a module, the directory containing the module must be in sys.path. sys.path is just a list of directory paths. The Python runtime searches each of these directories for your module. So one thing you can do is make sure your module is in a directory that is in sys.path. A couple of possibilities are the current working directory and the site-packages directory. On my computer (Win2K) Python puts the current working directory in sys.path. (I'm not sure this happens on Linux.) You can see this if you print sys.path; it is the empty string that starts the list. So I often cd to the directory containing a program before starting Python. Then I can import modules from that directory. For modules you want to be able to use from several programs, you can put them in C:\Python24\Lib\site-packages. This directory is always added to sys.path and it is intended as a place to install extra modules and packages. Most third-party modules will install to site-packages. Alternately you can modify sys.path to include the dir you want. There are several ways to do this. One way, as Evans showed, is to change it at runtime by appending a new path. This is fine for temporary changes but not very convenient in the long run. Another possibility is to edit the environment variable PYTHONPATH and add your dir to it. You can also create a .pth file in site-packages that contains the path to the dir to add to sys.path. You can find more info here: http://docs.python.org/tut/node8.html#SECTION00811 http://docs.python.org/lib/module-site.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Runing a Python program
There's a simple way you can add your directory to the execution path. try this >>> sys.path.append(r'C:\python24\myPythonFiles') now, you can import your file with the import command >>> import yourFile Note: This is only a temporary solution, when you close the interpreter, it will need to be appended again. Hope that help. - Original Message - From: Henry Dominik To: Tutor Sent: Friday, May 12, 2006 9:24 PM Subject: [Tutor] Runing a Python program Hello people, As a new python programmer, I created a directory in 'C:\python24\myPythonFiles', and added a simple python under the myPythonFiles directory; but when I tried running it on the Python Shell, I got the following error. >>> import myPythonFiles.readOut Traceback (most recent call last): File "", line 1, in -toplevel- import mypythonFiles.readOut ImportError: No module named myPythonFiles.readOut >>> How do I run a program that is placed on its own folder/directory? Thanks Henry ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor