[Tutor] HI, #Include like in python

A.T.Hofkamp a.t.hofkamp at tue.nl
Fri Mar 20 08:37:25 CET 2009


wesley chun wrote:
>>    import listen
>>
>> You can use the __import__ function if you want, but generally you
>> want the import statement as above.  The equivalent to 'import listen'
>> is:
>>
>>    listen = __import__('listen')
>>
>> See the tutorial here: http://docs.python.org/tutorial/modules.html
> 
> 
> you also have to make sure that your .py file is in one of the
> folders/directories listed in sys.path (to see it, import sys then do
> print sys.path). if the file is not in one of those folders, you will
> also get an import error.
> 
> you can either add the correct directory to your PYTHONPATH
> environment variable, or manually add it at run-time using
> sys.path.append/insert.

A simpler form is to put "listen.py" and "usbconnection.py" in the same directory.

In the latter you can do something like


import listen

listener = listen.ListenClass(param1, param2)
listener.begin()  # Call the 'begin' method of the ListenClass object.


You can use the ListenClass like normal, just state that Python should look 
for it in the "listen" file.

Albert


More information about the Tutor mailing list