print()被忽略

1 投票
1 回答
505 浏览
提问于 2025-04-28 20:53

当我运行这个代码时:

from firebase import firebase
from sanction import Client

client_pin = ''
client_id = 'valid_id'
client_secret = 'valid_secret'
request_token = 'state'
access_token = ''

query_url_wss = 'wss://developer-api.nest.com'
query_url_https = 'https://developer-api.nest.com'
auth_url = 'https://home.nest.com/login/oauth2?client_id=%s&state=%s' %(client_id, request_token)
access_token_url = 'https://api.home.nest.com/oauth2/access_token'

print('Visit the below link in a web browser to get an access PIN:\n')
print(auth_url)
client_pin = input('Enter PIN: ')

c = Client(
    token_endpoint=access_token_url,
    client_id=client_id,
    client_secret=client_secret)

c.request_token(code = client_pin)

data = c.request('/devices')
print(data)

我得到这样的输出(这里的错误可以忽略 - 这只是证明客户端被创建并使用,且获得了有效的令牌。目前这是唯一能打印出access_token的方法):


Visit the below link in a web browser to get an access PIN:

https://home.nest.com/login/oauth2?client_id=VALID_ID&state=state
Enter PIN: [ENTERED_A_VALID_PIN]
Traceback (most recent call last):
  File "C:\py\nest_testing_sanction.py", line 29, in <module>
    data = c.request('/devices')
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 169, in request
  File "C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py", line 211, in transport_query
  File "C:\Python34\lib\urllib\request.py", line 258, in __init__
    self.full_url = url
  File "C:\Python34\lib\urllib\request.py", line 284, in full_url
    self._parse()
  File "C:\Python34\lib\urllib\request.py", line 313, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'None/devices?access_token=[VALID_TOKEN]'

在创建客户端类时,没有打印出“客户端已启动”。我对__init__.py所做的任何修改都没有效果。难道它被某个地方缓存了,使用的是原版吗?


__init__.py的源代码:http://pastebin.com/TksTyZT4


修改后的函数(如上面错误输出中提到的__init__.py所示):

class Client(object):

    def __init__(self, auth_endpoint=None, token_endpoint=None,
        resource_endpoint=None, client_id=None, client_secret=None,
        token_transport=None):

        assert token_transport is None or hasattr(token_transport, '__call__')

        self.auth_endpoint = auth_endpoint
        self.token_endpoint = token_endpoint
        self.resource_endpoint = resource_endpoint
        self.client_id = client_id
        self.client_secret = client_secret
        self.access_token = None
        self.token_transport = token_transport or transport_query
        self.token_expires = -1
        self.refresh_token = None

        print('Client initiated') # Added this line - not getting executed

我还修改了这个函数(可能和上面的问题相同或类似):

def request_token(self, parser=None, redirect_uri=None, **kwargs):

    kwargs = kwargs and kwargs or {}

    parser = parser or _default_parser
    kwargs.update({
        'client_id': self.client_id,
        'client_secret': self.client_secret,
        'grant_type': 'grant_type' in kwargs and kwargs['grant_type'] or \
            'authorization_code'
    })
    if redirect_uri is not None:
        kwargs.update({'redirect_uri': redirect_uri})

    msg = urlopen(self.token_endpoint, urlencode(kwargs).encode(
        'utf-8'))
    data = parser(msg.read().decode(msg.info().get_content_charset() or
        'utf-8'))

    for key in data:
        setattr(self, key, data[key])

    if hasattr(self, 'expires_in'):
        try:
            # python3 dosn't support long
            seconds = long(self.expires_in)
        except:
            seconds = int(self.expires_in)
        self.token_expires = mktime((datetime.utcnow() + timedelta(
            seconds=seconds)).timetuple())

    #
    # I added all these prints and the return - no dice
    #
    print('***************************************************')
    print('Access Token: %s' %self.access_token)
    print('Token Life: %s' %self.token_expires)
    print('***************************************************')

    return self.access_token
  1. 我添加的代码行为什么会被忽略?
  2. 我需要做什么才能让它们被执行?

我唯一能想到的就是可能有另一个版本的文件在被使用,但我不知道它在哪里,或者是否真的是这样。

暂无标签

1 个回答

1

可能是有一个过时的 .pyc 文件需要删除。根据你运行代码的方式,代码可能会被编译成字节码,这些字节码会存储在 .pyc 文件里。但是,当你在 .py 文件中做了修改后,再次运行代码时,并不会覆盖掉已有的 .pyc 文件。这种情况通常发生在你导入的模块中。你可以找找 __init__.pyc 文件,把它删掉。

编辑:在聊天后我们发现:

被导入的 __init__.py 文件位于 C:\Python34\lib\site-packages\sanction-0.4.1-py3.4.egg\sanction\__init__.py,而你正在编辑的 __init__.py 文件则位于 C:\Python34\lib\site-packages\sanction\__init__.py。

sanction-0.4.1-py3.4.egg 实际上并不是一个文件夹,而是一个文件,出于某种原因(具体原因不明,可能是因为安装不当),它在导入时优先级更高。

撰写回答