导入不起作用?

时间:2012-05-10 12:43:36

标签: python unit-testing import

我正在使用python进行一些单元测试,一切都没问题,但是我遇到了一个很奇怪的问题,我觉得有一个导入,请关注这个问题:

我正在尝试在我的测试中创建一个对象GlanceApi,就像我已经完成的另一个测试一样,但是我收到了这个错误:

======================================================================
ERROR: setUpClass (__main__.TestGlance)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "glance_tests.py", line 22, in setUpClass
    self.glnce = glance.GlanceApi("")
AttributeError: 'module' object has no attribute 'GlanceApi'

这是我的代码:

import unittest
import json
import time
import sys
sys.path.append("../src")
import glance
import novaapiclient

class TestGlance(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        confFile = file('config.txt', 'r+w')
        configs = ""
        for line in confFile:
            if not (line.startswith('#')) and len(line) != 0:
                configs  = line.split(';')

        novaAPI = novaapiclient.NovaApiClient(str(configs[0]))
        novaAPI.make_auth(configs[1], configs[2], configs[3])

        self.glnce = glance.GlanceApi() # << HERE ERROR
        self.glnce.set_auth_obj(novaAPI.get_auth_obj())

看起来我已经完成了另一项测试,但对这种情况不起作用。 提前谢谢。

这是一瞥源头:

import pycurl
import cStringIO
import os

class GlanceApi:

    def __init__(self):
        self.auth = ""
        self.http_handler = ""

    def set_auth_obj(self, authenticate):
        self.auth = authenticate


    def list_images(self, is_public=False, with_details=False):

        if self.auth.is_authed() == False:
            return False

        self.http_handler = pycurl.Curl()
        printer = cStringIO.StringIO()

        if with_details == False:
            url_complement = "/images"
        else:
            url_complement = "/images/detail"

        if is_public == False:
            full_url = str(self.auth.get_image_URL() + url_complement)
        else:
            full_url = str(self.auth.get_image_URL()[:30] + url_complement)

        headers = ["X-Auth-Token:%s" % str(self.auth.get_auth_token())]

        self.http_handler.setopt(pycurl.URL, full_url)
        self.http_handler.setopt(pycurl.HTTPGET, 1)
        self.http_handler.setopt(pycurl.HTTPHEADER, headers)
        self.http_handler.setopt(pycurl.WRITEFUNCTION, printer.write)
        self.http_handler.perform()

        http_code = int(self.http_handler.getinfo(pycurl.HTTP_CODE))
        self.http_handler.close()

        return printer.getvalue()


    def get_image_metadata(self, id, is_public=False):

        if self.auth.is_authed() == False:
            return False

        self.http_handler = pycurl.Curl()
        printer = cStringIO.StringIO()

        url_complement = "/images/%s" % id

        # Setting the request url according the is_public parameter.
        if is_public == False:
            full_url = str(self.auth.get_image_URL() + url_complement)
        else:
            full_url = str(self.auth.get_image_URL()[:30] + url_complement)

        headers = ["X-Auth-Token:%s" % str(self.auth.get_auth_token())]

        self.http_handler.setopt(pycurl.URL, full_url)
        self.http_handler.setopt(pycurl.CUSTOMREQUEST, 'HEAD')
        self.http_handler.setopt(pycurl.HTTPHEADER, headers)
        self.http_handler.setopt(pycurl.WRITEFUNCTION, printer.write)
        self.http_handler.perform()

        http_code = int(self.http_handler.getinfo(pycurl.HTTP_CODE))
        self.http_handler.close()

        return printer.getvalue()


    def add_image(self,  file_path, name, is_public=False):
        # Verifying if the user is authenticated.
        if self.auth.is_authed() == False:
            return False

        self.http_handler = pycurl.Curl()
        printer = cStringIO.StringIO()

        url_complement = "/images"

        if is_public == False:
            full_url = str(self.auth.get_image_URL() + url_complement)
        else:
            full_url = str(self.auth.get_image_URL()[:30] + url_complement)

        size = os.path.getsize(file_path)

        image = [(str(name), (pycurl.FORM_FILE, str(file_path)))]

        headers = ["X-Auth-Token:%s" % str(self.auth.get_auth_token()), "x-image-meta-name:%s" % name, "x-image-meta-size:%s" % str(size)]
        if is_public == True:
            headers.append("x-image-meta-is-public:true")

        self.http_handler.setopt(pycurl.URL, full_url)
        self.http_handler.setopt(self.http_handler.HTTPPOST, image)
        self.http_handler.setopt(pycurl.HTTPHEADER, headers)
        self.http_handler.setopt(pycurl.WRITEFUNCTION, printer.write)
        self.http_handler.perform()

        http_code = int(self.http_handler.getinfo(pycurl.HTTP_CODE))
        self.http_handler.close()

        return printer.getvalue()

这是目录结构:

Project
   + src/
      - glance.py
      - ...
   + Tests/
      - glance_tests.py
      - ...

修改 已经解决了,怎么样?我不知道,但是我已经完成了这个,我创建了一个名为glanceapi.py的新文件,并将内容复制到glance.py文件中,在导入时更改名称并且它有效,我不知道是什么问题,可能是一些python bug,好吧,我想感谢那些试图帮助我的人。

1 个答案:

答案 0 :(得分:0)

转到IDLE,看看PYTHONPATH是什么。

import sys
sys.path

如果在路径中找不到Glance所在的位置,我想你必须使用sys.path.append()插入/附加它。在这里查看如何处理参数和内容:http://effbot.org/librarybook/sys.htm

编辑:如果这不起作用,请告诉我。