Daniel Sato wrote:


Hi,

Let me preface this by saying that I purchased O'Reilly's "Learn Python"
yesterday and have no programming experience (I am a photographer by trade)
except for a semester of what I think was BASIC on some old apple back in
elementary school (circa 1992).

I am not sure what details are relevant, so I will try and include as much
as possible.  I have a MacBook Pro running Mac OSX 10.5.6.  I recently dl'ed
MacPython 2.5, which installed Python 2.5.4 on my system.

When I am in the terminal, I can run a module by typing python
fullpath/to/script.py

However, when I enter Python from the terminal, by typing python, I can no
longer import items in this way unless the .py file is in my user folder
/Users/Me.  How can I change my settings so that I can import .py files from
a separate directory such as /Users/Me/Documents/PyMods?

Thank you.

-daniel sato

(Note, I'm using Python2.6 on Windows, so I may not get this quite right. But it'll be close) When Python does an import, it has a search path to use, very similar to the way the shell uses the 'path' variable. This search path may be examined and modified, as sys.path

The interpreter knows how to find the modules and packages it was installed with, but not how to find an arbitrary module you just wrote. So you can either put your module in one of the standard places, or add its actual location to the sys.path. Normally while you're experimenting with the interpreter, you want to do the latter.

Actually, the interpreter looks one other place for an import, the current working directory. So if you set the cwd to the location of the script.py, it should be able to find it and anything else in the same directory.

Naturally, if you have more than one interpreter, you'll want to be able to load the proper one. I keep shell scripts in my path called python26 and python31 for the versions of python I normally use. If I were running a system with a default version, I'd let the script python load that one.

So you'd want to add a shell script into your path called python25 (once per installation).

Then when you start an interactive session, you use

cd  /fullpath/to/
python25

>>>>import script
>>>>import sys
>>>>sys.path

::Advanced techniques, for special circumstances:::
Now, there are some differences between python25, python26, and python31. So the following may not be quite right, since I don't use 2.5 any more. But you can add your own directories to the intiial sys.path using the pythonpath variable. And you can modify that variable interactively, or with any python script you *can* load. So if you want to have several directories of python scripts to be available in a single session you can use either of those approaches.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to