[mod_python] python path and 3.3

Clodoaldo clodoaldo.pinto.neto at gmail.com
Fri Dec 1 08:03:27 EST 2006


2006/12/1, Graham Dumpleton <grahamd at dscpl.com.au>:
>
> The third option is to not use a package. If you created the package merely
> out of convenience to provide a namespace for the modules, this should
> be okay. Thus, remove the empty __init__.py file so that it isn't actually a
> package anymore. Then, change:
>
>   from lib import HTMLBuilder
>
> to:
>
>   from mod_python import apache
>
>   HTMLBuilder = apache.import_module('~/lib/HTMLBuilder.py')
>
> This is using the mod_python module importer directly to import the module
> by path. The '~/' prefix to the path is special and is understood by the
> module importer to be the directory that the active Python*Handler directive
> was specified for.
>
>
> A fourth option is to again remove the package __init__.py file so it isn't
> really a package anymore, but also move the directory to somewhere outside
> of the document tree. In order to find the modules though, the PythonPath
> directive is NOT used and instead the new module importers own search
> path is set. For example:
>
>   PythonOption mod_python.importer.path "['/some/path/lib']"
>
> The module importer path SHOULD NOT mention sys.path, it should instead
> be a distinct list of directories where only the new module importer will look.
> Because the 'lib' directory is include and the contents of the directory will be
> searched, can just say:
>
>   import HTMLBuilder
>
> If for some reason you wanted to do it this way, but not move the directory
> outside of the document tree, you could also say:
>
>   PythonOption mod_python.importer.path "['~/lib']"
>
> Here the '~/' prefix is again used so you don't have to have an absolute path.
> When used it will be expanded to the directory the handler was specified for.
>
>
> Anyway, hope I haven't confused you too much.
>

Your message is not confusing. It came in a good time and it clears
some confusion i had. But something is not working for me. Using
mod_python 3.3 in FC6.

I'm trying to import a module with both the third and fourth
techniques you described but I'm having errors.

This is the Virtual host config:

    ServerName carconsumption.s0
    DocumentRoot /var/www/html/carconsumption.com
    SetHandler mod_python
    PythonHandler mod_python.publisher
    PythonDebug On
    PythonOption mod_python.importer.path "['~/mod']"
    PythonImport db_connection carconsumption.s0

The module "constants" is in the mod directory under the document root:

[cpn at s0 mod]$ pwd
/var/www/html/carconsumption.com/mod
[cpn at s0 mod]$ ll constants.py
-rwxr----- 1 cpn apache 28675 Nov 29 17:42 constants.py
[cpn at s0 mod]$ ll -d ../mod
drwxrws--- 2 cpn apache 4096 Dec  1 12:26 ../mod

Third technique. index.py contains:

from mod_python import apache
C = apache.import_module('~/mod/constants.py')

The error output:

MOD_PYTHON ERROR

ProcessId:      24982
Interpreter:    'carconsumption.s0'

URI:            '/'
Location:       None
Directory:      None
Filename:       '/var/www/html/carconsumption.com/index.py'
PathInfo:       ''

Phase:          'PythonHandler'
Handler:        'mod_python.publisher'

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1522, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1227, in _process_target
    result = _execute_target(config, req, object, arg)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1126, in _execute_target
    result = object(arg)

  File "/usr/lib/python2.4/site-packages/mod_python/publisher.py",
line 204, in handler
    module = page_cache[req]

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1057, in __getitem__
    return import_module(req.filename)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
294, in import_module
    log, import_path)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
678, in import_module
    execfile(file, module.__dict__)

  File "/var/www/html/carconsumption.com/index.py", line 4, in ?
    C = apache.import_module('~/mod/constants.py')

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
302, in import_module
    return __import__(module_name, {}, {}, ['*'])

ImportError: No module named ~/mod/constants.py


MODULE CACHE DETAILS

Accessed:       Fri Dec  1 12:33:24 2006
Generation:     0

_mp_96eec165c095033cec4187d8d6982912 {
  FileName:     '/var/www/html/carconsumption.com/index.py'
  Instance:     1 [IMPORT]
  Generation:   0 [ERROR]
  Modified:     Fri Dec  1 12:31:11 2006
}

When i give the absolute path it works:
from mod_python import apache
C = apache.import_module('/var/www/html/carconsumption.com/mod/constants.py')

Fourth technique. index.py contains:

import constants as C

The error:

MOD_PYTHON ERROR

ProcessId:      24983
Interpreter:    'carconsumption.s0'

URI:            '/'
Location:       None
Directory:      None
Filename:       '/var/www/html/carconsumption.com/index.py'
PathInfo:       ''

Phase:          'PythonHandler'
Handler:        'mod_python.publisher'

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1522, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1227, in _process_target
    result = _execute_target(config, req, object, arg)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1126, in _execute_target
    result = object(arg)

  File "/usr/lib/python2.4/site-packages/mod_python/publisher.py",
line 204, in handler
    module = page_cache[req]

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1057, in __getitem__
    return import_module(req.filename)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
294, in import_module
    log, import_path)

  File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
678, in import_module
    execfile(file, module.__dict__)

  File "/var/www/html/carconsumption.com/index.py", line 3, in ?
    import constants as C

ImportError: No module named constants


MODULE CACHE DETAILS

Accessed:       Fri Dec  1 12:34:55 2006
Generation:     0

_mp_96eec165c095033cec4187d8d6982912 {
  FileName:     '/var/www/html/carconsumption.com/index.py'
  Instance:     1 [IMPORT]
  Generation:   0 [ERROR]
  Modified:     Fri Dec  1 12:34:19 2006
}


Regards,
-- 
Clodoaldo Pinto Neto


More information about the Mod_python mailing list