Python/Django导入错误os.path
我目前正在开发一个 Django 项目,该项目必须读取不同的文件格式。每种格式都有自己的阅读脚本。要使用的格式取决于选择,这就是为什么我的 admin_view.py 在运行时导入相应的脚本。这是通过 imp:
module = imp.load_source('lidardaten.datatypes.' + datatype, PATH + datatype + '.py')
现在,我的问题是,我在尝试导入这些脚本中的操作系统模块时收到一个 ImportError 错误,提示“没有名为路径的模块”。
Environment:
Request Method: POST
Request URL: http://lidardaten/django/lidardata/admin/upload/measurement/add/
Django Version: 1.2.5
Python Version: 2.4.3
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'lidardata.upload']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/s1lidar/u1/mathias/www/lidardata/upload/admin_views.py" in add_measurement
127. module = imp.load_source('lidardaten.datatypes.' + datatype, PATH + datatype + '.py')
File "/s1lidar/u1/mathias/www/lidardata/datatypes/polly.py" in ?
1. import os
File "/usr/lib64/python2.4/os.py" in ?
133. from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
Exception Type: ImportError at /admin/upload/measurement/add/
Exception Value: No module named path
我在 admin_view.py 中或通过解释器导入操作系统模块时都没有问题。在阅读脚本中导入其他模块也可以正常工作 - 只要这些模块不尝试导入 os.
是因为用 imp.load_source
导入脚本的原因吗?其他可能性?任何建议都将是摩天大楼-高度赞赏!
I'm currently working on a Django-Project that amongst others has to read different file-formats. Each format got its own reading-script. The formats to be used depend on a choice, that is why the corresponding scripts are imported at run-time by my admin_view.py. That's done via imp:
module = imp.load_source('lidardaten.datatypes.' + datatype, PATH + datatype + '.py')
Now, my problem is, that i get an ImportError while trying to import the os-module within these scripts saying 'No module named path'.
Environment:
Request Method: POST
Request URL: http://lidardaten/django/lidardata/admin/upload/measurement/add/
Django Version: 1.2.5
Python Version: 2.4.3
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'lidardata.upload']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/s1lidar/u1/mathias/www/lidardata/upload/admin_views.py" in add_measurement
127. module = imp.load_source('lidardaten.datatypes.' + datatype, PATH + datatype + '.py')
File "/s1lidar/u1/mathias/www/lidardata/datatypes/polly.py" in ?
1. import os
File "/usr/lib64/python2.4/os.py" in ?
133. from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
Exception Type: ImportError at /admin/upload/measurement/add/
Exception Value: No module named path
I don't have a problem importing the os-module neither whitin the admin_view.py nor via interpreter. Also importing other modules within the reading-scripts works fine - as long as those modules don't try to import os.
Is it because of importing the scripts with imp.load_source
? Other possibilities? Any suggestion would be skyscraper-highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其使用
imp
,不如看看神奇方法__import__
,可以不使用文件名导入模块。你的代码最终会是这样的:Rather than using
imp
, have a look at the magic method__import__
, which can import modules without using filenames. Your code will end up something like: