简体   繁体   English

Python/Django 导入错误 os.path

[英]Python/Django import error os.path

I'm currently working on a Django-Project that amongst others has to read different file-formats.我目前正在开发一个 Django 项目,其中包括必须读取不同的文件格式。 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.要使用的格式取决于选择,这就是我的 admin_view.py 在运行时导入相应脚本的原因。 That's done via imp:这是通过 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'.现在,我的问题是,我在尝试在这些脚本中导入 os-module 时遇到 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

I don't have a problem importing the os-module neither whitin the admin_view.py nor via interpreter.我在 admin_view.py 和解释器中导入 os-module 都没有问题。 Also importing other modules within the reading-scripts works fine - as long as those modules don't try to import os.在阅读脚本中导入其他模块也可以正常工作 - 只要这些模块不尝试导入 os.

Is it because of importing the scripts with imp.load_source ?是因为使用imp.load_source导入脚本吗? Other possibilities?其他可能性? Any suggestion would be skyscraper-highly appreciated!任何建议都会受到摩天大楼的高度赞赏!

Rather than using imp , have a look at the magic method __import__ , which can import modules without using filenames.与其使用imp ,不如看看魔术方法__import__ ,它可以在不使用文件名的情况下导入模块。 Your code will end up something like:您的代码最终将类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM