Importing modules from miscellaneous folders

Jshgwave jshgwave at yahoo.com
Wed Jan 5 20:08:14 EST 2011


On a Windows PC, I would like to be able to store modules in 
topic-specific foldersinstead of in Python26/Lib/site-packages, 
and then import into an IPython session those modules and the 
functions in them.

To test this, I have made a toy module:

---

"""
 toy_module.py 
 
 This is for testing the importing of modules from folders 
 other than "Lib".

 The first statement below is from Langtangen, Primer, p.143.
 At allows running the module as a program, as well as 
 importing it as a module.
"""


if __name__ == '__main__' :

  def double_it (a) :
    b = 2.*a
    print 'double_it in toy_module: a = ', a, ', b = ', b
    return b


  def triple_it (a) :
    b = 3.*a
    print 'triple_it in toy_module: a = ', a, ', b = ', b
    return b
---

I have tried many ways of importing this module and its functions, but 
all of them have failed.  In the IPython session below, the failures 
have been flagged for easy identification by "<<<".

---

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 0.10.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: # Test importing from other than Lib.

In [2]: # 2011-01-05

In [3]: function_dir = 'G:\\Python_2010-12\\JH_Python_Functions'

In [4]: # That is for the PC at work.

In [5]: import os

In [6]: os.getcwd()
Out[6]: 'C:\\Documents and Settings\\Hornstein'

In [7]: os.chdir(function_dir)

In [8]: os.getcwd()
Out[8]: 'G:\\Python_2010-12\\JH_Python_Functions'

In [9]: import toy_module

In [10]: result1 = toy_module.double_it(3.)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

G:\Python_2010-12\JH_Python_Functions\<ipython console> in <module>()

AttributeError: 'module' object has no attribute 'double_it'  <<< 1

In [11]: from toy_module import double_it as twox
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)

G:\Python_2010-12\JH_Python_Functions\<ipython console> in <module>()

ImportError: cannot import name double_it                     <<< 2

In [12]: IsFileThere = os.isfile('toy_module.py')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

G:\Python_2010-12\JH_Python_Functions\<ipython console> in <module>()

AttributeError: 'module' object has no attribute 'isfile'

In [13]: IsFileThere = os.path.isfile('toy_module.py')

In [14]: IsFileThere
Out[14]: True

In [15]: filelist = os.listdir(function_dir)

In [16]: filelist
Out[16]:
['arc_to_-pitopi.py',
 'arc_to_0to2pi.py',
 'ClustersOfGalaxiesUtils.py',
 'ClustersOfGalaxiesUtils.py.txt',
 'ClustersOfGalaxiesUtils.Test.2010-08-04.1.txt',
 'CosmolFns.py.txt',
 'CosmolGeom_SmoothedMatter_CL.py.txt',
 'Distances_z.py.txt',
 'extract_text_line.py',
 'JH.PythonExperimentsOnWindows.2011-01-03.txt',
 'LAMBDA_calc.py',
 'loop_to_sum.example.py',
 'number_theory.py',
 'omega_plasma_radHz.py',
 'README.txt',
 'Sampletxt.IPython.txt',
 'Sampletxt.txt',
 'script2_1.py',
 'synchRadn.py.txt',
 'toy_module.py',
 'toy_module.pyc',
 'uv2D.Feb14-06.py',
 'VariablesInFile.py',
 'VariablesInFile.pyc',
 'VLA_beamwidths.py',
 'z_cosmol.py']

In [17]: import glob

In [18]: pyfilelist = glob.glob('*.py')

In [19]: pyfilelist
Out[19]:
['arc_to_-pitopi.py',
 'arc_to_0to2pi.py',
 'ClustersOfGalaxiesUtils.py',
 'extract_text_line.py',
 'LAMBDA_calc.py',
 'loop_to_sum.example.py',
 'number_theory.py',
 'omega_plasma_radHz.py',
 'script2_1.py',
 'toy_module.py',
 'uv2D.Feb14-06.py',
 'VariablesInFile.py',
 'VLA_beamwidths.py',
 'z_cosmol.py']

In [20]: # Try changing the Python search path.

In [21]: import sys

In [22]: sys.path
Out[22]:
['',
 'C:\\Python26\\scripts',
 'C:\\WINDOWS\\system32\\python26.zip',
 'C:\\Python26\\DLLs',
 'C:\\Python26\\lib',
 'C:\\Python26\\lib\\plat-win',
 'C:\\Python26\\lib\\lib-tk',
 'C:\\Python26',
 'C:\\Python26\\lib\\site-packages',
 'C:\\Python26\\lib\\site-packages\\IPython/Extensions',
 u'C:\\Documents and Settings\\Hornstein\\_ipython']

In [23]: sys.path.append(function_dir)

In [24]: sys.path
Out[24]:
['',
 'C:\\Python26\\scripts',
 'C:\\WINDOWS\\system32\\python26.zip',
 'C:\\Python26\\DLLs',
 'C:\\Python26\\lib',
 'C:\\Python26\\lib\\plat-win',
 'C:\\Python26\\lib\\lib-tk',
 'C:\\Python26',
 'C:\\Python26\\lib\\site-packages',
 'C:\\Python26\\lib\\site-packages\\IPython/Extensions',
 u'C:\\Documents and Settings\\Hornstein\\_ipython',
 'G:\\Python_2010-12\\JH_Python_Functions']

In [25]: import toy_module

In [26]: result1 = toy_module.double_it(3.)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

G:\Python_2010-12\JH_Python_Functions\<ipython console> in <module>()

AttributeError: 'module' object has no attribute 'double_it'   <<< 3

In [27]: exit()
Do you really want to exit ([y]/n)?


Any insight would be appreciated.

Also, it is unfortunate that no warning is issued when an attempt at importing fails.

John Hornstein


(By the way, I am stuck with Python2.6 because the binary installer for 
NumPy on Windows still refuses to accept any later version of Python.)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110105/37cd8d9c/attachment.html>


More information about the Python-list mailing list