Welcome to Python!

> At one time this would have generated a new file MiniLibrary.pyc on the 
> desktop
> and returned a new prompt >>>. No longer. Now the error message
> "ImportError: No module named MiniLibrary".

Python uses something called 'sys.path' to locate modules. The
'sys.path' is a list of directories that exist on the local computer.
When you import a module Python recursively searches the packages in
each directory listed in 'sys.path'.

>From your interactive interpreter, run this command:

>>> import sys
>>> print sys.path

You should see a list of directories printed to the terminal.
Let's say your 'sys.path' has '/foo/bar' as a item, Python will look
for packages/modules (directories containing the magic '__init__.py'
file) and can import anything it finds there.

What probably happened in your case is that Python automatically adds
the current working directory to 'sys.path' and if you were running
things from your terminal while in the Desktop directory everything
would have been fine. If you change directory and try to import Python
(because it has now added your current working directory to 'sys.path'
and not the Desktop) has no idea where to find your module.

There are a few ways you can fix this problem...

 1. You can install modules/packages into your core sys.path
directories, which is under /Library/Python/2.X/site-packages - this
requires root privileges though, so I don't recommend it for casual
development work.
 2. You can alter your PYTHONPATH environment variable.

>From a normal terminal type the following command:

$ man python

Move to the bottom of the man page and read up on the ENVIRONMENT VARIABLES.

To use PYTHONPATH you should follow these steps:

 1. Create a directory in your home folder, something like 'python'
 2. Edit your '.profile' (notice the full stop) file in your home folder
 3. Add the following line: 'export PYTHONPATH=~/python'
 4. Close your terminal

Now, if you open a new terminal and drop 'test.py' into your ~/python
directory you should be able to open another terminal and 'import
test' without any problems.

Let me know if you need any more pointers.

If you're just getting started I would highly recommend this book:

http://www.diveintopython.org/

Best,

--
Noah Slater <http://www.bytesexual.org/>

"Creativity can be a social contribution, but only in so
far as society is free to use the results." - R. Stallman

--~--~---------~--~----~------------~-------~--~----~
To post: [email protected]
To unsubscribe: [EMAIL PROTECTED]
Feeds available at http://groups.google.com/group/python-north-west/feeds
For more options: http://groups.google.com/group/python-north-west
-~----------~----~----~----~------~----~------~--~---

Reply via email to