Python/Django导入错误os.path

发布于 2024-11-25 09:06:40 字数 1862 浏览 1 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-12-02 09:06:40

与其使用 imp,不如看看神奇方法 __import__,可以不使用文件名导入模块。你的代码最终会是这样的:

module_name = 'polly'
module = getattr(__import__('lidardaten.datatypes', globals(), locals(), [module_name]), module_name)

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:

module_name = 'polly'
module = getattr(__import__('lidardaten.datatypes', globals(), locals(), [module_name]), module_name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文