tags:

views:

42

answers:

2

If I want to use a 3rd party module like a python s3 module (boto http://boto.s3.amazonaws.com/index.html) or http://developer.amazonwebservices.com/connect/entry.jspa?externalID=134.

Once I download the .py files, what do I do?

Where does the python interpreter look when I import a module?

Is there a 'lightweight' way of installing a module so it makes deploying to a server easier?

A: 

It looks in the directory that you are currently working from. For example, if you are trying to import it from inside /test/my_file.py, you can place the module in /test/ and simply import module_name

Shane Reustle
so the module_name is always the name of the file w/o the extension?
Blankman
Yep. Say you wanted to import the `calculate_salary()` function from the module_name.py file located in the same directory.`from module_name import calculate_salary`Then, you will be able to call `calculate_salary()`. If you want to import the whole file, you can do this`from module_name import *`or this`import module_name`The first one lets you call the function directly, like I showed above, and the second requires you to call it like this`module_name.calculate_salary()`
Shane Reustle
+1  A: 

Look in the import statement reference, a long and involved description. The simple method is to include the module's location in sys.path.

I'm quoting the starting paragraph only:

Once the name of the module is known (unless otherwise specified, the term “module” will refer to both packages and modules), searching for the module or package can begin. The first place checked is sys.modules, the cache of all modules that have been imported previously. If the module is found there then it is used in step (2) of import.

If the module is not found in the cache, then sys.meta_path is searched (the specification for sys.meta_path can be found in PEP 302). The object is a list of finder objects which are queried in order as to whether they know how to load the module by calling their find_module() method with the name of the module. If the module happens to be contained within a package (as denoted by the existence of a dot in the name), then a second argument to find_module() is given as the value of the path attribute from the parent package (everything up to the last dot in the name of the module being imported). If a finder can find the module it returns a loader (discussed later) or returns None.

If none of the finders on sys.meta_path are able to find the module then some implicitly defined finders are queried. Implementations of Python vary in what implicit meta path finders are defined. The one they all do define, though, is one that handles sys.path_hooks, sys.path_importer_cache, and sys.path.

gimel